title: Combine files using Pillow and Python
body: <ol>
  <li>I need to run a python script inside a folder.</li>
  <li>the script count    files    ( image ) number create an image for
  every 64 images.
  example: if the    folder containing 640 images, I will get 10
  images as a combination    of 64 images/outputimage</li>
  </ol>
  
  <p>just, before adding  "for" instruction, I can get a result, but manually and only with the same image ( a duplication of image ).  </p>
  
  <p>any idea?</p>
  
  <p>so I proceed as follows:</p>
  
  <pre><code>import os
  import os.path
  
  from PIL import Image
  
  list = os.listdir(".")  # current directory 
  number_files = len(list)
  print (number_files)
  
  for x in range(0, number_files):
  
      # files = ['x.png']  
  
          # opens an image: here I can't find how 
      im = Image.open("1.png") # here I tried to 
  
  # creates a new empty image, RGB mode, and size 800 by 800.
  new_im = Image.new('RGB', (800, 800))
  
  # Here I resize my opened image, so it is no bigger than 100,100
  im.thumbnail((100, 100))
  # Iterate through a 8 by 8 grid with 100 spacing, to place my image
  for i in xrange(0, 800, 100):
      for j in xrange(0, 800, 100):
          # paste the image at location i,j:
          new_im.paste(im, (i, j))
          new_im.save(os.path.expanduser('outputimage.png'))
  </code></pre>
  
  <p>update :</p>
  
  <pre><code>import os
  import os.path
  
  from PIL import Image
  
  
  def drange(start, stop, step):
      while start &lt; stop:
          yield start
          start += step
  
  
  list = os.listdir(".")  # directory path
  number_files = len(list)
  print (number_files)
  
  new_im = Image.new('RGB', (800, 800))
  
  for x in drange(0, number_files, 64):
  
      im = Image.open(list[x])
      im.thumbnail((100, 100))
  
      for i in xrange(0, 800, 100):
          for j in xrange(0, 800, 100):
  
              new_im.paste(im, (i, j))
  
  new_im.save(os.path.expanduser('out.png'))
  </code></pre>
  
  <p>other update based on Sven solution :</p>
  
  <pre><code>import os.path
  
  from PIL import Image
  
  fileList = [] where_to_look = "png/" 
    for f in os.listdir(where_to_look):
          if os.path.isfile(os.path.join(where_to_look, f)):
              fileList.append(f)
               print (len(fileList))
  
  
  target_img = None n_targets = 0 collage_saved = False
  
  for n in range(len(fileList)):
      img = Image.open(fileList[n])
      img.thumbnail((100, 100))
  
      if n % 64 == 0:
          # create an empty image for a collage
          target_img = Image.new("RGB", (800, 800))
          n_targets += 1
          collage_saved = False
  
      # paste the image at the correct position
      i = int(n / 8)
      j = n % 8
      target_img.paste(img, (100*i, 100*j))
  
      if (n + 1) % 64 == 0 and target_img is not None:
          # save a finished 8x8 collage
          target_img.save("{0:04}.png".format(n_targets))
          collage_saved = True
  
  # save the last collage if not collage_saved:
      target_img.save("{0:04}.png".format(n_targets))
  </code></pre>