Kivy:具有任意比例的export_to_png() [英] Kivy: export_to_png() with an arbitrary scale

查看:90
本文介绍了Kivy:具有任意比例的export_to_png()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Kivy中以任意比例将小部件导出为png?

Is there a way to export a widget to png with an arbitrary scale in Kivy?

据我了解,现在默认情况下,相对于我在屏幕上看到的小部件的大小,导出图像的比例为1:1.

As far as I understand, now by default the scale of an exported image is 1:1 relative to size of widget that I see on the screen.

但是,如果我想导出到1:10(并获得10倍大的图像)怎么办?

But what if I want to export to 1:10 (and get an image 10 times larger)?

对如何做到这一点感兴趣.

Interested in any ideas how this can be done.

推荐答案

我不知道直接进行此操作的直接方法,但是您可以分叉

I don't know a straight forward method to do the same, but you can fork export_to_png method of Widget class

这是一个有效的示例:

from kivy.uix.widget import Widget
from kivy.base import runTouchApp
from kivy.lang import Builder
from kivy.graphics import (Canvas, Translate, Fbo, ClearColor,
                           ClearBuffers, Scale)


kv='''
BoxLayout:
    orientation: 'vertical'
    MyWidget:
        id: wgt
    BoxLayout:
        size_hint_y: .1
        orientation: 'horizontal'
        Label:
            text: 'enter scale:'
        TextInput:
            id: txtinpt
            text: '2.5'
        Button:
            text: 'Export with new scale'
            on_release: wgt.export_scaled_png('kvwgt.png', image_scale=float(txtinpt.text))

<MyWidget>:
    canvas:
        PushMatrix:
        Color:
            rgba: (0, 0, 1, .75)
        Ellipse:
            pos: (self.x + self.width // 5, self.y + self.height // 5)
            size: (self.width * 3 // 5, self.height * 3 // 5)
        Color:
            rgba: (1, 0, 0, .5)
        Rectangle:
            pos: (self.x + self.width // 4, self.y + self.height // 4)
            size: (self.width // 2, self.height // 2)
        Rotate:
            origin:
                self.center
            angle:
                45
    Button:
        text: 'useless child widget\\njust for demonstration'
        center: root.center
        size: (root.width // 2, root.height // 8)
        canvas:
            PopMatrix:

'''

class MyWidget(Widget):

    def export_scaled_png(self, filename, image_scale=1):
        re_size = (self.width * image_scale, 
                self.height * image_scale)

        if self.parent is not None:
            canvas_parent_index = self.parent.canvas.indexof(self.canvas)
            if canvas_parent_index > -1:
                self.parent.canvas.remove(self.canvas)

        fbo = Fbo(size=re_size, with_stencilbuffer=True)

        with fbo:
            ClearColor(0, 0, 0, 0)
            ClearBuffers()
            Scale(image_scale, -image_scale, image_scale)
            Translate(-self.x, -self.y - self.height, 0)

        fbo.add(self.canvas)
        fbo.draw()
        fbo.texture.save(filename, flipped=False)
        fbo.remove(self.canvas)

        if self.parent is not None and canvas_parent_index > -1:
            self.parent.canvas.insert(canvas_parent_index, self.canvas)



runTouchApp(Builder.load_string(kv))

注意re_size变量,该变量随后传递给Fbo构造函数. 以及export_scaled_png方法中的" Scale(image_scale,-image_scale,image_scale) "指令.

Note re_size variable, which in turn passes to Fbo constructor. And the "Scale(image_scale, -image_scale, image_scale)" instruction inside export_scaled_png method.

这篇关于Kivy:具有任意比例的export_to_png()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆