Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

python - How to draw a PIL image to a DearPyGui canvas?

I would like to draw an image to a canvas in 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])
question from:https://stackoverflow.com/questions/65880271/how-to-draw-a-pil-image-to-a-dearpygui-canvas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You must convert the image to a 1D list, like so:

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()

The list is then added as a texture, which can then be accessed like a regular image file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...