Python Forum
Combine images using Pillow and Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combine images using Pillow and Python
#1
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):
File "combine.py", line 71, in <module>
img = Image.open(fileList[n])
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 2258, in open
fp = builtins.open(filename, "rb")
IOError: [Errno 2] No such file or directory: '28.png'

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.
Reply


Messages In This Thread
Combine images using Pillow and Python - by GMA - Jun-05-2018, 04:26 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to read multispectral images on python noorasim 0 1,792 Feb-28-2021, 03:54 PM
Last Post: noorasim
  How do I insert images in Python using gspread (or any other package)? ivansing23 0 2,275 Jul-27-2020, 01:26 PM
Last Post: ivansing23
  My journey with attaching file instead of images using Python numbnumb54 1 1,795 Jul-24-2020, 02:37 AM
Last Post: scidam
  A more efficient way of comparing two images in a python vukan 0 2,035 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to compare two different size images in python and find corresponding pixel value squidsirymchenry 1 4,595 Feb-03-2020, 06:48 AM
Last Post: michael1789
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,652 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax
  Compare two images with Python is possible? Delemir78 3 4,825 Dec-17-2019, 09:03 PM
Last Post: Larz60+
  Python Pillow I_Am_Groot 1 2,660 Oct-13-2018, 07:28 AM
Last Post: j.crater
  [split] Python Pillow - Photo Manipulation keegan_010 1 2,985 Oct-11-2018, 09:57 AM
Last Post: Larz60+
  Python Pillow - Photo Manipulation keegan_010 2 2,929 Oct-11-2018, 03:49 AM
Last Post: keegan_010

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020