我想将 PIL.ImageTk.PhotoImage 保存到文件中。我的方法是创建一个“打开”文件并调用“写入”方法,但它不起作用,因为我不知道如何从对象获取字节数组。
def store_temp_image(data, image):
new_file_name = data.number + ".jpg"
with open(os.path.join("/tmp/myapp", new_file_name), mode='wb+') as output:
output.write(image)
错误信息如下:
TypeError: a bytes-like object is required, not 'PhotoImage'
我通常会找到将 ImageTk 对象转换为 PIL 对象的方法,但反之则不然。从文档中我也无法得到任何提示。
您可以先使用ImageTk.getimage() https://pillow.readthedocs.io/en/stable/_modules/PIL/ImageTk.html函数(向下滚动,接近末尾)获取 PIL Image,然后使用其 save() 方法:
def store_temp_image(data, imagetk):
# do sanity/validation checks here, if need be
new_file_name = data.number + ".jpg"
imgpil = ImageTk.getimage( imagetk )
imgpil.save( os.path.join("/tmp/myapp", new_file_name), "JPEG" )
imgpil.close() # just in case (not sure if save() also closes imgpil)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)