title: Python how do I overlay random images over images being generated to end up with 2 images with text
body: <p>Here is my code that I have so far. What it does is grabs a random text line from the text file an generates a random color then creates the JPG image with the text on top. What I want to do is take a random picture and overlay it on top of the solid color at opacity of maybe 0.2 or something then put the text on top of that so I end up with > solid color > picture overlayed > text on top. This code generates multiple images. It would be nice to have the text centered vertically also, but I'm not clear on how to do that.</p>
  
  <pre><code>import Image, random, textwrap, ImageFont, ImageDraw, os, sys
  basepath = os.path.dirname(sys.argv[0]) + "/"
  
  for n in xrange(10):
  
      keywordlist = []
      imglist = []
      keyword = file(basepath + "text.txt", "r")
  
      for line in keyword:
          keywordlist.append(line.replace("\n", ""))
  
          def type(name):
              value = name[random.randint(0,len(name)-1)]
              return value
  
      def random_color():
          return (random.randint(0,155), random.randint(0,155), random.randint(0,155))
  
      astr = ('%s' %(type(keywordlist)))
      para = textwrap.wrap(astr,width=19)
  
      MAX_W,MAX_H=640,400
      im = Image.new('RGB', (MAX_W, MAX_H), (random_color()))
      draw = ImageDraw.Draw(im)
      font = ImageFont.truetype('C:/windows/fonts/Arialbd.ttf', 58)
  
      current_h=0
      for line in para:
          w,h=draw.textsize(line, font=font)
          draw.text(((MAX_W-w)/2, current_h), line, font=font,)
          current_h+=h
  
      im.save('img%000d.jpg' % n)
  </code></pre>
  
  <p>Here is a sample of the output:</p>
  
  <p><img src="https://i.stack.imgur.com/aZWfw.jpg" alt="Example"></p>