d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Python Coding -- Image Drawing
Add Reply New Topic New Poll
Member
Posts: 6,876
Joined: Jun 18 2005
Gold: 1,341.66
Jul 11 2013 02:38pm
Code
#!/usr/bin/env python
import os.path
import sys
from time import strftime
import Image

row_size = 4
margin = 3

def generate_montage(filenames, output_fn):
   images = [Image.open(filename) for filename in filenames]

   width = max(image.size[0] + margin for image in images)*row_size
   height = sum(image.size[1] + margin for image in images)
   montage = Image.new(mode='RGBA', size=(width, height), color=(0,0,0,0))

   max_x = 0
   max_y = 0
   offset_x = 0
   offset_y = 0
   for i,image in enumerate(images):
       montage.paste(image, (offset_x, offset_y))

       max_x = max(max_x, offset_x + image.size[0])
       max_y = max(max_y, offset_y + image.size[1])

       if i % row_size == row_size-1:
           offset_y = max_y + margin
           offset_x = 0
       else:
           offset_x += margin + image.size[0]

   montage = montage.crop((0, 0, max_x, max_y))
   montage.save(output_fn)

if __name__ == '__main__':
   basename = strftime("Montage %Y-%m-%d at %H.%M.%S.png")
   exedir = os.path.dirname(os.path.abspath(sys.argv[0]))
   filename = os.path.join(exedir, basename)
   generate_montage(sys.argv[1:], filename)


This code outputs something such as this



The code used to draw in numbers on each picture in i increments.

such as this



If you want the original code for this last pictures let me know (as you can see it cropped the pictures wrong and cut them off) hence the reworking of the code.

I need to readd the text of numbers to each picture.

My question is... How would I go about doing that? I have no knowledge of python, and received help to rework the code to get it to the top picture


Any help is appreciated. The bottom used PIL I believe to draw the text.



Here is the 2nd picture's code:
Code
#!/usr/bin/env python
import os
import sys
from time import strftime
import Image
import ImageDraw
import ImageFont

# parameters
row_size = 4
margin = 3

def generate_montage(filenames):
   images = [Image.open(filename) for filename in filenames]

   width = 0
   height = 0
   i = 0
   sum_x = max_y = 0
   width = max(image.size[1]+margin for image in images)*row_size
   height = sum(image.size[0]+margin for image in images)

   montage = Image.new(mode='RGBA', size=(width, height), color=(0,0,0,0))
   try:
       image_font = ImageFont.truetype('font/Helvetica.ttf', 18)
   except:
       try:
           image_font = ImageFont.load('font/Helvetica-18.pil')
       except:
           image_font = ImageFont.load_default()
   draw = ImageDraw.Draw(montage)
   offset_x = offset_y = 0

   i = 0
   max_y = 0
   max_x = 0
   offset_x = 0
   for image in images:
       montage.paste(image, (offset_x, offset_y))

       text_coords = offset_x + image.size[0] - 45, offset_y + 120
       draw.text(text_coords, '#{0}'.format(i+1), font=image_font)

       max_x = max(max_x, offset_x+image.size[0])
       if i % row_size == row_size-1:
           offset_y += max_y+margin
           max_y = 0
           offset_x = 0
       else:
           offset_x += image.size[0]+margin
           max_y = max(max_y, image.size[1])

       i += 1

   if i % row_size:
       offset_y += max_y

   filename = strftime("Montage %Y-%m-%d at %H.%M.%S.png")
   montage = montage.crop((0, 0, max_x, offset_y))
   montage.save(filename)

if __name__ == '__main__':
   old_cwd = os.getcwd()

   os.chdir(os.path.dirname(sys.argv[0]))
   try:
       if len(sys.argv) > 1:
           generate_montage(sys.argv[1:])
   finally:
       os.chdir(old_cwd)


I apologize for the long post, but I am frustrated because I cannot get the numbers back. :(

I appreciate all and any help!

Thank you!

Anthony

EDIT: Made pictures smaller.

This post was edited by Th1rt33n on Jul 11 2013 02:57pm
Member
Posts: 6,876
Joined: Jun 18 2005
Gold: 1,341.66
Jul 11 2013 04:06pm
I got it all figured out.

Lots of reworking, and googling, and regoogling, and asking questions on stackoverflow.

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll