我正在“ RGBX”格式的字节字符串中创建一个512x512光栅图像(来自内存映射的设备),并且无法使其显示在标签图像中。使用show()可以很好地显示图像。我只需要快速将字节数据直接传输到按钮或标签的图像输入,可以吗?
我尝试用.convert转换图像,不执行RGB,PhotoImage只需要一个字符串变量,base64.b64encode()在我的机器上产生了一个不可杀死的僵尸。我试图在演示中使图像对象“静态”,我认为大多数类似问题的答案都指向使图像保持显示状态。我通过打开任何文件加载的图像都可以使用标签或按钮图像方法很好地显示。 io.BytesIO不支持将图像插入其中。
import tkinter as tk
from PIL import Image
#import base64
root=tk.Tk()
kimage_width = 512
kimage_height = 512
kimgSize = (kimage_width,kimage_height)
# Make a color 256K pixel photo of 'stuff', in bytes
# The image data is in RGBX order
ColorImage=b''
for i in range (0,int(kimage_width * kimage_height/8)):
ColorImage+=bytes([(i>>2) & 0xFF]) # Red
ColorImage+=bytes([(i>>7) & 0xFF]) # Green
ColorImage+=bytes([(i>>6) & 0xFF]) # Blue
ColorImage+=bytes([(i>>0) & 0xFF]) # Not used
# copy it 8 times
ColorImage+=ColorImage+ColorImage+ColorImage # 4
ColorImage+=ColorImage # total = 8 copies
# make a PIL image?
kimage = Image.frombytes('RGBX', kimgSize, ColorImage, 'raw')
kimage.show() # Display image if not a tkinter window
#######photo = base64.b64encode(ColorImage) # Crash - makes a zombie
b=tk.Button(root,justify = tk.LEFT)
b.config(image=kimage, width="512", height="512")
b.pack(side=tk.LEFT)
root.mainloop()
ImageMajick在1个窗口中显示512x512彩色图像,tk窗口应将相同图像放置到按钮图像中,但tk错误显示为: _tkinter.TclError:图像“”不存在
答案 0 :(得分:0)
我认为您正在尝试执行此操作,但是错过了一些地方:
#!/usr/bin/env python3
import tkinter as tk
from PIL import Image, ImageTk
root=tk.Tk()
kimage_width = 512
kimage_height = 512
kimgSize = (kimage_width,kimage_height)
# Make a color 256K pixel photo of 'stuff', in bytes
# The image data is in RGBX order
ColorImage=b''
for i in range (0,int(kimage_width * kimage_height/8)):
ColorImage+=bytes([(i>>2) & 0xFF]) # Red
ColorImage+=bytes([(i>>7) & 0xFF]) # Green
ColorImage+=bytes([(i>>6) & 0xFF]) # Blue
ColorImage+=bytes([(i>>0) & 0xFF]) # Not used
# copy it 8 times
ColorImage+=ColorImage+ColorImage+ColorImage # 4
ColorImage+=ColorImage # total = 8 copies
# make a PIL image?
kimage = Image.frombytes('RGBX', kimgSize, ColorImage, 'raw').convert('RGB')
kimage.show() # Display image if not a tkinter window
pI = ImageTk.PhotoImage(kimage)
b=tk.Button(root,justify = tk.LEFT)
b.config(image=pI, width=512, height=512)
b.pack(side=tk.LEFT)
root.mainloop()