如何将 PIL 图像绘制到 DearPyGui canvas?
How to draw a PIL image to a DearPyGui canvas?
我想在 DearPyGui 中将图像绘制到 canvas :
import dearpygui.core as dpg
with sdpg.window("Main Window"):
dpg.set_main_window_size(800, 800)
dpg.set_main_window_title("Pixel selector")
dpg.add_drawing('drawing', width=400, height=350)
img = ImageGrab.grab(bbox=[0, 0, 100, 100])
# something like this would be great
dpg.draw_image('drawing', img, [0, 0], [100, 100])
您必须将图像转换为一维列表,如下所示:
import dearpygui.core as dpg
import dearpygui.simple as sdpg
from PIL import ImageGrab
with sdpg.window("Main Window"):
dpg.set_main_window_size(800, 800)
dpg.set_main_window_title("Pixel selector")
dpg.add_drawing('drawing', width=400, height=350)
img = ImageGrab.grab(bbox=[0, 0, 100, 100])
dpg_image = []
for i in range(0, img.height):
for j in range(0, img.width):
pixel = img.getpixel((j, i))
dpg_image.append(pixel[0])
dpg_image.append(pixel[1])
dpg_image.append(pixel[2])
dpg_image.append(255)
# something like this would be great
dpg.add_texture("texture id", dpg_image, img.width, img.height)
dpg.draw_image('drawing', "texture id", [0, 0], [100, 100])
dpg.start_dearpygui()
然后将列表添加为纹理,然后可以像访问常规图像文件一样访问它。
我之前的回答有问题,因为我是从磁盘加载 PIL 图像。几个小时后(由于缺乏文档和示例而恼火)我设法找到了一个适合我的解决方案,并且比使用循环更快:
image = PIL.Image.open("sample.png")
# We need RGBA, in my case loaded image was RGB, so I added alpha channel
image.putalpha(255)
# I had to divide values by 255 since PIL uses 0 to 255 and dearpygui seems
# to use 0.0 to 1.0
dpg_image = numpy.frombuffer(image.tobytes(), dtype=numpy.uint8) / 255.0
# After this you can use dpg_image with dearpygui and add to texture
我想在 DearPyGui 中将图像绘制到 canvas :
import dearpygui.core as dpg
with sdpg.window("Main Window"):
dpg.set_main_window_size(800, 800)
dpg.set_main_window_title("Pixel selector")
dpg.add_drawing('drawing', width=400, height=350)
img = ImageGrab.grab(bbox=[0, 0, 100, 100])
# something like this would be great
dpg.draw_image('drawing', img, [0, 0], [100, 100])
您必须将图像转换为一维列表,如下所示:
import dearpygui.core as dpg
import dearpygui.simple as sdpg
from PIL import ImageGrab
with sdpg.window("Main Window"):
dpg.set_main_window_size(800, 800)
dpg.set_main_window_title("Pixel selector")
dpg.add_drawing('drawing', width=400, height=350)
img = ImageGrab.grab(bbox=[0, 0, 100, 100])
dpg_image = []
for i in range(0, img.height):
for j in range(0, img.width):
pixel = img.getpixel((j, i))
dpg_image.append(pixel[0])
dpg_image.append(pixel[1])
dpg_image.append(pixel[2])
dpg_image.append(255)
# something like this would be great
dpg.add_texture("texture id", dpg_image, img.width, img.height)
dpg.draw_image('drawing', "texture id", [0, 0], [100, 100])
dpg.start_dearpygui()
然后将列表添加为纹理,然后可以像访问常规图像文件一样访问它。
我之前的回答有问题,因为我是从磁盘加载 PIL 图像。几个小时后(由于缺乏文档和示例而恼火)我设法找到了一个适合我的解决方案,并且比使用循环更快:
image = PIL.Image.open("sample.png")
# We need RGBA, in my case loaded image was RGB, so I added alpha channel
image.putalpha(255)
# I had to divide values by 255 since PIL uses 0 to 255 and dearpygui seems
# to use 0.0 to 1.0
dpg_image = numpy.frombuffer(image.tobytes(), dtype=numpy.uint8) / 255.0
# After this you can use dpg_image with dearpygui and add to texture