python - 如何将 PIL 图像绘制到 DearPyGui 画布?
问题描述
我想在 DearPyGui 中将图像绘制到画布上:
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()
然后将该列表添加为纹理,然后可以像常规图像文件一样对其进行访问。
推荐阅读
- jquery - 如何在数据属性标签内达到午夜后将数字加一?
- c# - WPF 应用程序:System.TimeChanged 事件在您进行远程连接时触发?为什么?以及如何避免?
- java - 有条件地将数据发送到spring批处理中的多个写入器
- angular - 在 MEAN 堆栈中使用角度发布数据时出错
- android - 运行时 SVG 错误,但在 Android Studio 中没有
- javascript - 从 Jquery/Javascript 后面的代码访问静态属性值
- javascript - Angular2动态获取组件引用
- xml - 移除 xml 的命名空间
- git - git push faid 告诉我用户名是错误的
- c# - 在 UWP 的日历视图中单击日期时的事件