title: Python - array of image layers - Pillow library
body: <p>I'm playing around with regenerative art using Python and the Pillow library.  I've got multiple directories with images, my goal is to use the Pillow library to create 'collages'.</p>
  <p>I'm loading images into arrays this like this:</p>
  <pre><code>#load img files into an array
  skin  = [file for file in os.listdir('skin') if file.endswith('.png')]
  hair  = [file for file in os.listdir('hair') if file.endswith('.png')]
  head  = [file for file in os.listdir('head') if file.endswith('.png')]
  mouth = [file for file in os.listdir('mouth') if file.endswith('.png')]
  nose  = [file for file in os.listdir('nose') if file.endswith('.png')]
  eyes  = [file for file in os.listdir('eyes') if file.endswith('.png')]
  
  #select a random image from each array
  skin_img       = random.choices(skin)[0]
  hair_img       = random.choices(hair)[0]
  head_img       = random.choices(head)[0]
  mouth_img      = random.choices(mouth)[0]
  nose_img       = random.choices(nose)[0]
  eyes_img       = random.choices(eyes)[0]
  </code></pre>
  <p>I want to layer/merge these in a specific order, I'm hoping I can accomplish this using an array, like this:</p>
  <pre><code>order = ['head','skin','mouth','eyes','nose','hair']
  </code></pre>
  <p>Using the Pillow library, one can merge. two images like this:</p>
  <pre><code>img1  = Image.open(head_img)
  img2  = Image.open(skin_img)
  comp1 = Image.alpha_composite(img1,img2)
  </code></pre>
  <p>My question: How can I loop through the order array and merge all of my images, ending up with a single composite?</p>