Combine images using Pillow and Python - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Combine images using Pillow and Python (/thread-10767.html) |
Combine images using Pillow and Python - GMA - Jun-05-2018 Hello Python community :) I'm trying to write a python script using Pillow : count files ( png images ) number create an image for every 64 images. example: if the folder containing 640 images, I will get 10 images as a thumbnail of 64 images/outputimage just, before adding "for" instruction, I can get a result, but manually and only with the same image ( a duplication of image). any idea? I started by this step : import os import os.path from PIL import Image def drange(start, stop, step): while start < 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'))someone suggest me another solution : import os.path from PIL import Image image_files = os.listdir(".") n_files = len(image_files) target_img = None n_targets = 0 collage_saved = False for n in range(n_files): img = Image.open(image_files[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))so I modified his solution in order to run the script outside png folder : 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))when I tested this script, I got : Quote:Traceback (most recent call last): by the way, the png folder contains images named from 0.png to 640.png I appreciate if you could take a look at this issue. Thank you in advance. How combine images using Pillow and Python ? - GMA - Jun-06-2018 Hello, I'm trying to write a python script using Pillow : count files ( png images ) number create an image for every 64 images. example: if the folder containing 640 images ( or any other number ), I will get 10 images as a thumbnail of 64 images/outputimage any idea? I started by this step : import os import os.path from PIL import Image image_dir = os.path.abspath("path_to_png_folder") # list all files in directory files = os.listdir(image_dir) # get all PNGs png_files = filter(lambda x: x.endswith(".png"), files) # make file paths absolute image_files = map(lambda x: os.sep.join([image_dir, x]), png_files) print (image_files) n_files = len(image_files) # print (n_files) target_img = None n_targets = 0 collage_saved = False for n in range(n_files): img = Image.open(image_files[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))I got 10 images, the first one contain 8x8 images, the others are empty ( black ). also, since my png files are a sequence from 0.png to 640.png; have any idea how to let the paste operation take images by order. example: out1.pan will contain 0.png to 63.png Thank you RE: Combine images using Pillow and Python - killerrex - Jun-06-2018 I will split the script in 3 different steps:
For the 1st point you can do something like: def create_collage(files, out): # You can add a check to guarantee that the list of images is not bigger than 64... target_img = Image.new("RGB", (800, 800)) for k, png in enumerate(files): row, col = divmod(k, 8) img = Image.open(png) img.thumbnail((100, 100)) target_img.paste(img, (100*row, 100*col)) target_img.save(out)To list all the images in a directory you can use the glob module that will filter them by extension for you: import glob files = glob.glob('./*.png')And to group every 64 elements might be: groups = [files[k:k+64] for k in range(0, len(files), 64)] |