Advertisement
here2share

# tk_correction_model_cache.py -- just a test

Jul 7th, 2025 (edited)
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. # tk_correction_model_cache.py
  2. # a test to see what it will look like which, funny enough... I thought the resize(...) would be significantly less predictable
  3.  
  4. import tkinter as tk
  5. from PIL import Image, ImageDraw, ImageTk
  6. import os
  7. import json
  8.  
  9. lg_size = 650
  10. sm_size = 640
  11.  
  12. resize_to = (sm_size, sm_size)
  13. image_size = (lg_size, lg_size)
  14. threshold = 26
  15. margin = 50
  16. cache = {}
  17.  
  18. truth_img = Image.new('L', image_size, 255)
  19. progress_img = Image.new('L', resize_to, 0)
  20. draw_progress = ImageDraw.Draw(progress_img)
  21.  
  22. CACHE_FILENAME = "correction_model_cache.txt"
  23. SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
  24.  
  25. def save_data(data, filename, directory):
  26.     path = os.path.join(directory, filename)
  27.     sorted_items = sorted(data.items())
  28.     processed = [coords for _, coords in sorted_items]
  29.     with open(path, 'w') as f:
  30.         json.dump(processed, f, indent=4)
  31.  
  32. root = tk.Tk()
  33. root.title("Processing")
  34. root.geometry(f"{sm_size}x{sm_size}+0+0")
  35. root.resizable(False, False)
  36. photo = ImageTk.PhotoImage(image=progress_img)
  37. label = tk.Label(root, image=photo)
  38. label.pack()
  39. root.update()
  40.  
  41. for x in range(margin, lg_size - margin):
  42.     for y in range(margin, lg_size - margin):
  43.         img_copy = truth_img.copy()
  44.         draw = ImageDraw.Draw(img_copy)
  45.         draw.point((x, y), fill=0)
  46.         resized = img_copy.resize(resize_to)
  47.         px = resized.load()
  48.         nx, ny = x * sm_size // lg_size, y * sm_size // lg_size
  49.         val = px[nx, ny]
  50.         if val < threshold:
  51.             cache.setdefault(val, []).append((x, y))
  52.             draw_progress.point((nx, ny), fill=val*10)
  53.             photo = ImageTk.PhotoImage(image=progress_img)
  54.             label.config(image=photo)
  55.             label.image = photo
  56.             root.update()
  57.  
  58. root.title("Done")
  59. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement