Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_correction_model_cache.py
- # a test to see what it will look like which, funny enough... I thought the resize(...) would be significantly less predictable
- import tkinter as tk
- from PIL import Image, ImageDraw, ImageTk
- import os
- import json
- lg_size = 650
- sm_size = 640
- resize_to = (sm_size, sm_size)
- image_size = (lg_size, lg_size)
- threshold = 26
- margin = 50
- cache = {}
- truth_img = Image.new('L', image_size, 255)
- progress_img = Image.new('L', resize_to, 0)
- draw_progress = ImageDraw.Draw(progress_img)
- CACHE_FILENAME = "correction_model_cache.txt"
- SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
- def save_data(data, filename, directory):
- path = os.path.join(directory, filename)
- sorted_items = sorted(data.items())
- processed = [coords for _, coords in sorted_items]
- with open(path, 'w') as f:
- json.dump(processed, f, indent=4)
- root = tk.Tk()
- root.title("Processing")
- root.geometry(f"{sm_size}x{sm_size}+0+0")
- root.resizable(False, False)
- photo = ImageTk.PhotoImage(image=progress_img)
- label = tk.Label(root, image=photo)
- label.pack()
- root.update()
- for x in range(margin, lg_size - margin):
- for y in range(margin, lg_size - margin):
- img_copy = truth_img.copy()
- draw = ImageDraw.Draw(img_copy)
- draw.point((x, y), fill=0)
- resized = img_copy.resize(resize_to)
- px = resized.load()
- nx, ny = x * sm_size // lg_size, y * sm_size // lg_size
- val = px[nx, ny]
- if val < threshold:
- cache.setdefault(val, []).append((x, y))
- draw_progress.point((nx, ny), fill=val*10)
- photo = ImageTk.PhotoImage(image=progress_img)
- label.config(image=photo)
- label.image = photo
- root.update()
- root.title("Done")
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement