Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
chmod u+x label.py
Code Block
#!/usr/bin/env python

import sys

################################################################################
# image stuff and convenience functions

import Image

def width(im):
  return im.size[0]

def height(im):
  return im.size[1]

def aspect(im):
  return 1.0*width(im)/height(im)

################################################################################

if __name__ == "__main__":

  ########################################
  # handle command line args

  if len(sys.argv) != 1+2:
    print "usage: %s background.png outfile.png" % sys.argv[0]
    exit()

  bgFilename= sys.argv[1]
  outFilename= sys.argv[2]

  ########################################
  # load image and logo

  bg = Image.open(bgFilename)
  if bg.mode != "RGBA":
    bg = bg.convert("RGBA")

  lg = Image.open("SLAC_Logo_hires.png")
  if lg.mode != "RGBA":
    lg = lg.convert("RGBA")


  ########################################
  # make frame the desired size

  scale= 1280/float(width(bg))

  bg_width  = int(scale*width(bg))
  bg_height = int(scale*height(bg))
  bg = bg.resize((bg_width, bg_height), Image.ANTIALIAS)

  ########################################
  # apply the logo

  # make logo the desired size
  logo_height = int(200*scale)

  logo_width = int(aspect(lg)*logo_height)
  logo = lg.resize((logo_width, logo_height), Image.ANTIALIAS)

  # figure out pixel offsets
  xo= width(bg)-width(logo) -10
  yo= height(bg)-height(logo) -10

  # slap it on the logo onto the background
  bg.paste(logo, (xo, yo), logo)


  # uncomment the ## don't need alpha channel any more
  #bg= bg.convert("RGB")
show() to see image directly on the screen.
  # this is useful for debugging.  Be sure it's commented out
  # again before running a batch job from the makefile, because you'll
  # be drowned in images popping up!
  #bg.show()

  bg.save(outFilename)

When dealing with thousands of frames, it helps to be able to take advantage of multiple cores to do the work. It also helps to be able to interrupt and resume the conversion process without having to start over. A simple Makefile can handle this nicely, regardless of whether one uses ImageMagick or Python Imaging. As an example, if oversized unlabled filenames are of the form

...