Kivy: export_to_png () с произвольным масштабом - PullRequest
0 голосов
/ 23 июня 2018

Есть ли способ экспортировать виджет в png с произвольным масштабом в Kivy?

Насколько я понимаю, теперь по умолчанию масштаб экспортируемого изображения составляет 1: 1 относительно размера виджета, который я вижу на экране.

Но что, если я хочу экспортировать в 1:10 (и получить изображение в 10 раз больше)?

Интересуют любые идеи, как это можно сделать.

1 Ответ

0 голосов
/ 26 июня 2018

Я не знаю прямой метод, чтобы сделать то же самое, но вы можете форк export_to_png метод класса Widget

Вот рабочий пример:

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. И инструкция " Scale (image_scale, -image_scale, image_scale) " внутри метода export_scaled_png.

...