Image.export_to_png和Image.export_as_image都不起作用 [英] Both Image.export_to_png and Image.export_as_image not working

查看:66
本文介绍了Image.export_to_png和Image.export_as_image都不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

from kivy.app import App
from kivy.uix.image import Image

class MyApp(App):

    def build(self):
        my_image = Image(source='test.png')
        with my_image.canvas:
            Triangle(point=(0, 0, 30, 30, 60, 0))
        my_image.export_to_png('test2.png')
        my_image.export_as_image().save('test3.png')
        return my_image

myapp = MyApp().run()

其执行的预期结果应该是两个名为"test2.png"和"test3.png"的"test.png"的副本,并在上面加上三角形.但是,将创建两个仅包含三角形且没有原始纹理的文件.什么是错误,如何将小部件导出为png?

The expected outcome of its execution should be two copies of the 'test.png' named 'test2.png' and 'test3.png with the added triangle on them. However, two files with triangles only, without original texture are created. What is the mistake and how do I export widgets to png?

我完成了研究,发现这是由于这些功能(我只能确定 image.export_to_png ,但 image.export_as_image 提供相同的结果)不导出窗口小部件本身,而是导出窗口小部件的画布.这使我想到两个问题:

I've done my research and found out that it is caused by the fact that these functions (I'm only sure about image.export_to_png but image.export_as_image provides the same result) export not the widget itself but rather canvas of the widget. That leads me to two question:

1)如何导出更改的图片,而不导出更改本身?

1)How do I export changed picture but not the changes themselves?

2)如果没有更改,如何导出原始图像?

2)How do I just export the original image if no changes occurred?

推荐答案

看起来您只需要等待下一帧,可能是因为OpenGL初始化的原因:

Looks like you just need to wait for the next frame, probably for OpenGL initialisation reasons:

from kivy.app import App
from kivy.uix.image import Image
from kivy.graphics import Triangle
from kivy.clock import Clock

class MyApp(App):

    def build(self):
        self.my_image = Image(source='test.png')
        with self.my_image.canvas:
            Triangle(point=(0, 0, 30, 30, 60, 0))

        Clock.schedule_once(self.export, 1)
        return self.my_image

    def export(self, dt):
        self.my_image.export_to_png('test2.png')
        self.my_image.export_as_image().save('test3.png')


myapp = MyApp().run()

我已经完成研究,发现这是由于这些函数(我只确定image.export_to_png,但image.export_as_image提供相同的结果)导致的,而不是小部件本身导出,而是导出了小部件的画布.

I've done my research and found out that it is caused by the fact that these functions (I'm only sure about image.export_to_png but image.export_as_image provides the same result) export not the widget itself but rather canvas of the widget.

小部件的外观没有什么,除了它在画布上绘制的内容.

There isn't anything to the widget's appearance except what it draws on its canvas.

我不明白您的最后问题.

I don't understand your final questions.

这篇关于Image.export_to_png和Image.export_as_image都不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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