code_junkie

How to complete this python function to save in the same folder?

Nov 12th, 2011
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. import os
  2. import Image
  3.  
  4. def filmstripOfImages():
  5.  
  6. imgpath = '/path/here/'
  7. files = glob.glob(imgpath + '*.jpg')
  8.  
  9. imgwidth = files[0].size[0]
  10. imgheight = files[0].size[1]
  11. totalheight = imgheight * len(files)
  12.  
  13. filename = 'filmstrip.jpg'
  14. filmstrip_url = imgpath + filename
  15.  
  16. # Create the new image. The background doesn't have to be white
  17. white = (255,255,255)
  18. filmtripimage = Image.new('RGB',(imgwidth, totalheight),white)
  19. row = 0
  20. for file in files:
  21. img = Image.open(file)
  22.  
  23. left = 0
  24. right = left + imgwidth
  25. upper = row*imgheight
  26. lower = upper + imgheight
  27. box = (left,upper,right,lower)
  28. row += 1
  29.  
  30. filmstripimage.paste(img, box)
  31. try:
  32. filmstripimage.save(filename, 'jpg', quality=90, optimize=1)
  33. except:
  34. filmstripimage.save(miniature_filename, 'jpg', quality=90)")
  35.  
  36. #!/usr/bin/env python
  37. import Image
  38.  
  39. def makefilmstrip(images, mode='RGB', color='white'):
  40. """Return a combined (filmstripped, each on top of the other) image of the images.
  41.  
  42. """
  43. width = max(img.size[0] for img in images)
  44. height = sum(img.size[1] for img in images)
  45.  
  46. image = Image.new(mode, (width, height), color)
  47.  
  48. left, upper = 0, 0
  49. for img in images:
  50. image.paste(img, (left, upper))
  51. upper += img.size[1]
  52.  
  53. return image
  54.  
  55. if __name__=='__main__':
  56. # Here's how it could be used:
  57. from glob import glob
  58. from optparse import OptionParser
  59.  
  60. # process command-line args
  61. parser = OptionParser()
  62. parser.add_option("-o", "--output", dest="file",
  63. help="write combined image to OUTPUT")
  64.  
  65. options, filepatterns = parser.parse_args()
  66. outfilename = options.file
  67.  
  68. filenames = []
  69. for files in map(glob, filepatterns):
  70. if files:
  71. filenames += files
  72.  
  73. # construct image
  74. images = map(Image.open, filenames)
  75. img = makefilmstrip(images)
  76. img.save(outfilename)
  77.  
  78. $ python filmstrip.py -o output.jpg *.jpg
  79.  
  80. filmstripimage.save(filmstrip_url, 'jpg', quality=90, optimize=1)
Add Comment
Please, Sign In to add comment