Advertisement
Guest User

/au/ proof maker

a guest
Dec 22nd, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. import os
  2. from PIL import Image, ImageFont, ImageDraw
  3.  
  4. proof_target_ratio = (4, 3)     #desired aspect ratio, images will be cropped to fit
  5. proof_big = (4000, 4000)        #max resulting image size
  6. proof_small_width = 490         #proof width
  7. proof_caption_height = 40       #caption height (can be 0)
  8. proof_box = 5                   #border size (can be 0)
  9. proof_bgcolor = (255, 255, 255) #rgb background color
  10. proof_textcolor = (0, 0, 0)     #rgb caption color
  11. proof_font = "timesbd.ttf"      #caption font file
  12. proof_font_size = 36            #caption font size
  13. caption_filename = "captions.txt"   #captions file
  14.  
  15. proof_small_height = int(proof_small_width / proof_target_ratio[0] * proof_target_ratio[1])
  16.  
  17. im_big = Image.new("RGB", proof_big, proof_bgcolor)
  18.  
  19. proof_target = proof_target_ratio[0] / proof_target_ratio[1]
  20. xpos = 0
  21. ypos = 0
  22. count = 0
  23.  
  24. caption_file = open(caption_filename, "r")
  25. caption_font = ImageFont.truetype(proof_font, proof_font_size)
  26.  
  27. for infile in sorted(os.listdir('.')):
  28.     f, e = os.path.splitext(infile)
  29.     if f == "proof":
  30.         continue   
  31.    
  32.     print("Opening ", infile)
  33.     try:
  34.         proof = Image.open(infile)
  35.     except IOError:
  36.         print(infile, "does not seem to be an image file")
  37.         continue
  38.    
  39.     if proof.size[0] / proof.size[1] != proof_target:
  40.         if proof.size[0] > proof.size[1]:
  41.             new_width = int(proof.size[1] * proof_target)
  42.             new_height = proof.size[1]
  43.             box = ( int((proof.size[0] - new_width)/2), 0, int((proof.size[0] - new_width)/2 + new_width), proof.size[1] )
  44.         else:
  45.             new_width = proof.size[0]
  46.             new_height = int(proof.size[0] / proof_target)
  47.             box = ( 0, int((proof.size[1] - new_height) / 2), proof.size[0], int((proof.size[1] - new_height) / 2 + new_height) )
  48.        
  49.         proof = proof.crop(box)
  50.        
  51.        
  52.     proof = proof.resize( (proof_small_width, proof_small_height), Image.LANCZOS )
  53.    
  54.     im_big.paste(proof, (xpos + proof_box, ypos) )
  55.     text = caption_file.readline().strip()
  56.    
  57.     cap_size = caption_font.getsize(text)
  58.     while cap_size[0] > proof_small_width:
  59.         text = text[:-1]
  60.         cap_size = caption_font.getsize(text)
  61.    
  62.     align = (proof_small_width - cap_size[0]) / 2
  63.    
  64.     topleft = (xpos + align, ypos + proof_small_height + 2)
  65.     print("Putting text to ", topleft) 
  66.     print(text)
  67.    
  68.     caption_draw = ImageDraw.Draw(im_big)
  69.     caption_draw.text(topleft, text, fill=proof_textcolor, font=caption_font)
  70.    
  71.     count = count + 1
  72.    
  73.     xpos = xpos + proof_small_width + 2*proof_box
  74.     if xpos + proof_small_width >= proof_big[0]:
  75.         xpos = 0
  76.         ypos = ypos + proof_small_height + proof_caption_height
  77.        
  78.         if ypos >= proof_big[1]:
  79.             break
  80.  
  81. print(count, "images processed, cropping")
  82. im_big = im_big.crop( (0, 0, 4000, ypos + proof_small_height + proof_caption_height))
  83. print("Saving")
  84. im_big.save("proof.jpg", "jpeg", quality=95)
  85. print("Done")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement