kivy:可以使用缓冲区作为图像源吗? [英] kivy: possible to use buffer as image source?

查看:263
本文介绍了kivy:可以使用缓冲区作为图像源吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,从一些现有图像中生成一个新图像。

I've got code along the lines of the following which generates a new image out of some existing images.

from PIL import Image as pyImage

def create_compound_image(back_image_path, fore_image_path, fore_x_position):

    back_image_size = get_image_size(back_image_path)
    fore_image_size = get_image_size(fore_image_path)

    new_image_width = (fore_image_size[0] / 2) + back_image_size[0]
    new_image_height = fore_image_size[1] + back_image_size[1]

    new_image = create_new_image_canvas(new_image_width, new_image_height)

    back_image = pyImage.open(back_image_path)
    fore_image = pyImage.open(fore_image_path)

    new_image.paste(back_image, (0, 0), mask = None) 
    new_image.paste(fore_image, (fore_x_position, back_image_size[1]), mask = None)

    return new_image

在代码的后面,我有类似的东西:

Later in the code, I've got something like this:

from kivy.uix.image import Image
img = Image(source = create_compound_image(...))

如果我执行上述操作,则会收到 Image.source仅接受字符串/ unicode <的消息/ code>。

If I do the above, I get the message that Image.source only accepts string/unicode.

如果我从新图像创建 StringIO.StringIO()对象,并尝试使用它作为源,错误消息与上面相同。如果我使用StringIO对象的getvalue()方法的输出作为源,则消息是源必须是没有NULL字节的编码字符串,而不是str

If I create a StringIO.StringIO() object from the new image, and try to use that as the source, the error message is the same as above. If I use the output of the StringIO object's getvalue() method as the source, the message is that the source must be encoded string without NULL bytes, not str.

在创建kivy Image对象时,使用 create_compound_image()函数的输出作为源的正确方法是什么?

What is the proper way to use the output of the create_compound_image() function as the source when creating a kivy Image object?

推荐答案

source 是一个 StringProperty 并期待一个文件路径。这就是为什么当你试图传递 PIL.Image 对象, StringIO 对象或图像的字符串表示时出现错误的原因。这不是框架想要的。至于从 StringIO 获取图像,之前讨论过:

source is a StringProperty and is expecting a path to file. That's why you got errors when you tried to pass PIL.Image object, StringIO object or string representation of image. It's not what framework wants. As for getting image from StringIO, it was discussed before here:

  • https://groups.google.com/forum/#!topic/kivy-users/l-3FJ2mA3qI
  • https://github.com/kivy/kivy/issues/684

你也可以尝试更简单,快捷和肮脏方法 - 只需将图像保存为tmp文件并以正常方式读取。

You can also try much simpler, quick and dirty method - just save your image as a tmp file and read it normal way.

这篇关于kivy:可以使用缓冲区作为图像源吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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