我有如下代码,它可以从一些现有图像中生成新图像。
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
后来在代码中,我得到了这样的东西:
from kivy.uix.image import Image
img = Image(source = create_compound_image(...))
如果我执行上述操作,我会收到以下消息Image.source only accepts string/unicode
.
如果我创建一个StringIO.StringIO()
对象从新图像,并尝试使用它作为源,错误消息与上面相同。如果我使用 StringIO 对象的 getvalue() 方法的输出作为源,则消息是source must be encoded string without NULL bytes, not str
.
使用输出的正确方法是什么create_compound_image()
创建 kivy Image 对象时作为源函数?
看来你只想将两个图像组合成一个,实际上你可以使用创建一个纹理纹理创建 http://kivy.org/docs/api-kivy.graphics.texture.html#kivy.graphics.texture.Texture.create并将数据 blit 到特定的 pos 使用纹理.blit_buffer http://kivy.org/docs/api-kivy.graphics.texture.html#kivy.graphics.texture.Texture.blit_buffer .
from kivy.core.image import Image
from kivy.graphics import Texture
bkimg = Image(bk_img_path)
frimg = Image(fr_img_path)
new_size = ((frimg.texture.size[0]/2) + bkimg.texture.size[0],
frimg.texture.size[1] + bkimg.texture.size[1])
tex = Texture.create(size=new_size)
tex.blit_buffer(pbuffer=bkimg.texture.pixels, pos=(0, 0), size=bkimg.texture.size)
tex.blit_buffer(pbuffer=frimg.texture.pixels, pos=(fore_x_position, bkimg.texture.size[1]), size=frimg.texture.size)
现在您可以直接在任何地方使用此纹理,例如:
from kivy.uix.image import Image
image = Image()
image.texture = tex
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)