Advertisement
here2share

# tk_infinite_interpolation3.py

Jan 11th, 2023
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. # tk_infinite_interpolation.py
  2.  
  3. import random
  4. import tkinter as tk
  5. from PIL import Image,ImageTk,ImageEnhance
  6.  
  7. root = tk.Tk()
  8. canvas = tk.Canvas(root, width=600, height=600)
  9. root.title("tk_infinite_interpolation.py")
  10. canvas.pack()
  11.  
  12. pixels = [(200, 200, 200)] * 600 * 600
  13. rnd = 50
  14.  
  15. NEW_SCALE_CONSTANT = [(0)] * 1200 * 1200
  16. new_scale = []
  17. def interpolate():
  18.     global new_scale
  19.     new_scale = NEW_SCALE_CONSTANT[:]
  20.     for iii in range(600):
  21.         for jjj in range(600):
  22.             r, g, b = pixels[iii * 600 + jjj]
  23.             if 6 < iii < 580 and 6 < iii < 580:
  24.                 i = random.randint(-15, 15) + iii
  25.                 j = random.randint(-15, 15) + jjj
  26.                 r2, g2, b2 = pixels[i * 600 + j]
  27.                 r = (r*49 + r2) // 50
  28.                 g = (g*49 + g2) // 50
  29.                 b = (b*49 + b2) // 50
  30.             i = iii * 2
  31.             j = jjj * 2
  32.             new_scale[i * 1200 + j] = (r, g, b)
  33.             for i2, j2 in [(i+1,j),(i,j+1),(i+1,j+1)]:
  34.                 rrr = min(255, max(0, r + random.randint(-rnd, rnd)))
  35.                 ggg = min(255, max(0, g + random.randint(-rnd, rnd)))
  36.                 bbb = min(255, max(0, b + random.randint(-rnd, rnd)))
  37.                 new_scale[i2 * 1200 + j2] = (rrr, ggg, bbb)
  38.  
  39. img = Image.new("RGB", (1200, 1200))
  40. while 1:
  41.     interpolate()
  42.  
  43.     img.putdata(new_scale)
  44.     # Resize the image using bi-cubic interpolation
  45.     img = img.resize((1200, 1200), Image.BICUBIC)
  46.  
  47.     # Apply image enhancement for better color representation
  48.     img = ImageEnhance.Sharpness(img).enhance(0.0001)
  49.  
  50.     zoom = 10
  51.     current_size = 600
  52.  
  53.     while current_size < 1200:
  54.         canvas.delete(tk.ALL)
  55.         sz = int(current_size)
  56.         scaled_image = img.resize((sz, sz), Image.ANTIALIAS)
  57.         scaled_image = ImageTk.PhotoImage(scaled_image)
  58.         canvas.create_image(300, 300, image=scaled_image)
  59.         canvas.update()
  60.         current_size += zoom
  61.         zoom += 2
  62.     new_scale = new_scale[300 * 1200: 900 * 1200]
  63.     pixels = []
  64.     for i in range(0, len(new_scale), 1200):
  65.         pixels.extend(new_scale[i + 300: i + 900])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement