Advertisement
here2share

# tk_text_zoom.py

Mar 15th, 2025
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.74 KB | None | 0 0
  1. # tk_text_zoom.py
  2.  
  3. import tkinter as tk
  4. from PIL import Image, ImageDraw, ImageFont, ImageTk
  5.  
  6. WW, HH = 640, 640
  7. CX, CY = WW // 2, HH // 2
  8.  
  9. root = tk.Tk()
  10. root.title("# tk_text_zoom.py")
  11. root.geometry("+0+0")
  12. canvas = tk.Canvas(root, width=WW, height=HH)
  13. canvas.pack()
  14.  
  15. def get_font(font_size):
  16.     return ImageFont.truetype("times.ttf", font_size)
  17.  
  18. def create_txt():
  19.     font_size = 400
  20.     font = get_font(font_size)
  21.     while True:
  22.         bbox = font.getbbox("a")
  23.         text_width = bbox[2] - bbox[0]
  24.         text_height = bbox[3] - bbox[1]
  25.         if text_width > WW - 8 or text_height > HH - 8:
  26.             break
  27.         font_size += 1
  28.         font = get_font(font_size)
  29.        
  30.     def blank_check():
  31.         for x in range(WW):
  32.             if pixels[x, HH - 1][0]:
  33.                 return False
  34.         return True
  35.    
  36.     for y_offset in range(HH):
  37.         img = Image.new("RGBA", (WW, HH), (0, 0, 0, 0))
  38.         draw = ImageDraw.Draw(img)
  39.         draw.text((CX, CY - y_offset), "a", fill="red", font=font, anchor="mm")
  40.         pixels = img.load()
  41.         if blank_check():
  42.             break
  43.     return img
  44.  
  45. img_txt_default = create_txt()
  46. img_txt = img_txt_default.copy()
  47.  
  48. current_scale = 1
  49. while True:
  50.     t = int(current_scale)
  51.     scaled_img = img_txt.resize((t, t), Image.ANTIALIAS)
  52.    
  53.     current_scale *= 1.05
  54.     if t > WW:
  55.         left = (t - WW) // 2
  56.         top = (t - HH) // 2
  57.         right = left + WW
  58.         bottom = top + HH
  59.         img_txt = scaled_img.crop((left, top, right, bottom))
  60.         if current_scale > 1000:
  61.             img_txt = img_txt_default.copy()
  62.             current_scale = 1
  63.  
  64.     scaled_tk_img = ImageTk.PhotoImage(scaled_img)
  65.  
  66.     canvas.delete("all")
  67.     canvas.create_image(CX, CY, image=scaled_tk_img, anchor="center")
  68.  
  69.     canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement