Advertisement
Guest User

dearpygui texture resize

a guest
Feb 25th, 2022
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.53 KB | None | 0 0
  1. import dearpygui.dearpygui as dpg
  2. import cv2 as cv
  3. import numpy as np
  4.  
  5. def flat_img(mat):
  6.     return np.true_divide(np.asfarray(np.ravel(np.flip(mat,2)), dtype='f'), 255.0)
  7.  
  8. #full size image
  9. _img = cv.imread('./test.jpg')
  10. _imgdata = flat_img(_img)
  11. #window scaled image
  12. img = _img.copy()
  13. imgdata = _imgdata.copy()
  14.  
  15. win_dimensions = [800,500]
  16. tag_n = 0
  17.  
  18. #updating a texture with new dimension
  19. def resize_window_img(tag, dimensions, _img, flat):
  20.     img_dim = np.flip(_img.shape[:-1])
  21.     scale = 1
  22.     if (dimensions[0] >= dimensions[1]):
  23.         scale = dimensions[0]/img_dim[0]
  24.     else: scale = dimensions[1]/img_dim[1]
  25.     img_dim[0]*=scale
  26.     img_dim[1]*=scale
  27.  
  28.     global img
  29.     global imgdata
  30.     img = cv.resize(_img, img_dim)
  31.     imgdata = flat_img(img)
  32.     # delete window, add new texture+tag to registry, re-add window
  33.     dpg.delete_item(tag)
  34.     with dpg.texture_registry(show=False):
  35.         global tag_n   
  36.         tag_n +=1
  37.         tex_tag = "texture_tag"+str(tag_n)
  38.         dpg.add_raw_texture(img.shape[1], img.shape[0], imgdata, tag=tex_tag, format=dpg.mvFormat_Float_rgb)
  39.     with dpg.window(tag="imgwindow"):
  40.         dpg.add_image(tex_tag)
  41.         dpg.set_primary_window("imgwindow", True)
  42.  
  43. def handle_float(sender, data):
  44.     dpg.set_value("texture_tag"+str(tag_n), np.multiply(imgdata, data))
  45.  
  46. def afteredit_cb(sender, data):
  47.     f = dpg.get_value(data)
  48.     # print(f"{f}, {data}")
  49.  
  50. def viewport_resize_cb(sender, data):
  51.     win_dimensions[0] = data[2:][0]
  52.     win_dimensions[1] = data[2:][1]
  53.  
  54. def resize_button_cb(sender, data):
  55.     resize_window_img("imgwindow", win_dimensions, _img, _imgdata)
  56.  
  57. dpg.create_context()
  58. dpg.create_viewport(title='img gui', width=win_dimensions[0], height=win_dimensions[1])
  59.  
  60. with dpg.item_handler_registry(tag="float handler") as handler:
  61.     dpg.add_item_deactivated_after_edit_handler(callback=afteredit_cb)
  62.  
  63. with dpg.texture_registry(show=False)
  64.     dpg.add_raw_texture(img.shape[1], img.shape[0], imgdata, tag="texture_tag"+str(tag_n), format=dpg.mvFormat_Float_rgb)
  65.  
  66. with dpg.window(tag="ctlnwindow", label="img", no_close=True):
  67.     dpg.add_slider_float(label="float", tag="fbar", default_value=0., max_value=1, callback=handle_float)
  68.     dpg.add_button(label="fit image to window", tag="resizebutton", callback=resize_button_cb)
  69.  
  70. with dpg.window(tag="imgwindow"):
  71.     dpg.add_image("texture_tag"+str(tag_n))
  72.     dpg.set_primary_window("imgwindow", True)
  73.  
  74. dpg.setup_dearpygui()
  75. dpg.show_viewport()
  76. dpg.bind_item_handler_registry("fbar", "float handler")
  77. dpg.set_viewport_resize_callback(viewport_resize_cb)
  78. dpg.start_dearpygui()
  79. dpg.destroy_context()
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement