Skip to content

Optimizes RAM consumption by unloading images from the DPG

License

Notifications You must be signed in to change notification settings

IvanNazaruk/DearPyGui-ImageController

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DearPyGui-ImageController

Optimizes RAM consumption by unloading images from the DPG.

It works quite simply: if the image is visible it is loaded, if not visible then after a certain amount of time is unloaded from the DPG

Demo video

0.mp4

1255 images with 1920×1080 resolution were used

You can try this example using this script: example.py

How to use it?

  1. Download the library(git clone https://github.com/IvanNazaruk/DearPyGui-ImageController) and move the DearPyGui_ImageController folder to your project/script

  2. Import the library:

import DearPyGui_ImageController as dpg_img
  1. Set DPG texture registry:
dpg_img.set_texture_registry(dpg.add_texture_registry())
  1. Add the image:
dpg_img.add_image("{IMAGE_PATH}")

Or use Pillow:

from PIL import Image
img = Image.open("{IMAGE_PATH}")
dpg_img.add_image(img)

Full example:

import dearpygui.dearpygui as dpg

import DearPyGui_ImageController as dpg_img

dpg.create_context()
dpg_img.set_texture_registry(dpg.add_texture_registry())

with dpg.window():
    dpg_img.add_image("{IMAGE_PATH}")

    # from PIL import Image
    # img = Image.open("{IMAGE_PATH}")
    # dpg_img.add_image(img)

dpg.create_viewport()
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()

Additional Information

  • How to resize an image after it has been created?

Use the internal functions set_width, set_height and set_size. If width or height is not specified, the other dimension will be decreased/increased proportionally. If the dimensions are not set, the image size will be used.

An example use case is found in example.py:
First we will save the created image viewer object:

image_viewer = dpg_img.add_image(file, height=100, parent=group) # noqa
all_image_viewers.append(image_viewer)
And after that they are resized:
for image_viewer in all_image_viewers:
image_viewer.set_size(width=50, height=50)

  • How do I bind the DPG handler to the image?

Use the internal function set_image_handler. Example usage:

with dpg.item_handler_registry() as image_handler:
    dpg.add_item_clicked_handler(callback=lambda _, data: print(f"Clicked: {data[0]}"))

image_viewer = dpg_img.add_image("{IMAGE_PATH}")
image_viewer.set_image_handler(image_handler)

TODO list

  • Increase documentation
  • GIFs support

About

Optimizes RAM consumption by unloading images from the DPG

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages