Advertisement
Guest User

Untitled

a guest
Jan 11th, 2022
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.31 KB | None | 0 0
  1. import string
  2. import os
  3. import math
  4. from PIL import Image
  5. from PIL import ImageDraw
  6. from PIL import ImageFont
  7.  
  8. path = r"D:\Arсane Art"
  9. save_path = r"D:\alph"
  10. res = 512
  11. d = {}
  12.  
  13. # find most rectangular images
  14. for file in os.listdir(path):
  15.  img_path = os.path.join(path, file)
  16.  img = Image.open(img_path)
  17.  ratio = abs(math.log(img.size[0] / img.size[1]))
  18.  d.update({img_path: ratio})
  19.  
  20. d = dict(sorted(d.items(), key=lambda item: item[1]))
  21.  
  22. alphabet = string.ascii_uppercase.replace('J', '')
  23.  
  24. n = len(alphabet)
  25. imgs = list(d.keys())[:n]
  26. imgs2 = []
  27.  
  28. font_size = int(res / 4)
  29. font = ImageFont.truetype("C:\WINDOWS\Fonts\IMPACT.TTF", font_size)
  30. pos = [font_size*0.4]*2
  31. stroke_width = int(font_size/8)
  32.  
  33. # slap letters to them
  34. for i in range(n):
  35.  letter = alphabet[i]
  36.  img = Image.open(imgs[i]).resize([res]*2)
  37.  draw = ImageDraw.Draw(img)
  38.  draw.text(pos, letter, font=font, fill=(255,255,255), stroke_width=stroke_width, stroke_fill=(0,0,0))
  39.  imgs2.append(img)
  40.  img.save(os.path.join(save_path, letter+'.jpg'))
  41.  
  42. # combine them
  43. n = 5
  44. size = res * n
  45. big_im = Image.new('RGB', [size]*2)
  46.  
  47. i=0
  48. x_offset = 0
  49. y_offset = 0
  50. for im in imgs2:
  51.  if i > 0 and i%n == 0:
  52.   x_offset = 0
  53.   y_offset+=res
  54.  big_im.paste(im, (x_offset,y_offset))
  55.  x_offset += res
  56.  i+=1
  57.  
  58. big_im.save(os.path.join(save_path, "5x5.jpg"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement