How to stack 50 images vertically

Viewed 51

I am having 50 images. I want to stack it vertically to get a single image. I can vertically stack 23 images. When I try to stack with 24 or above images it is showing error. I have used the below code.

import numpy as np
import os
from PIL import Image
list_im = []
path = (r"output_2")
for filename in os.listdir(path):
    list_im.append(filename)
imgs    = [ Image.open(i) for i in list_im ]
min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
imgs_comb = Image.fromarray( imgs_comb)
imgs_comb.save( 'Final.jpg' )

This is the error i get:

OSError                                   Traceback (most recent call last)
<ipython-input-2-72549b8f2351> in <module>
     15 imgs_comb = np.vstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )
     16 imgs_comb = Image.fromarray( imgs_comb)
---> 17 imgs_comb.save( 'hello.jpg' )

~\miniconda3\envs\PDF_RandD\lib\site-packages\PIL\Image.py in save(self, fp, format, **params)
   2238 
   2239         try:
-> 2240             save_handler(self, fp, filename)
   2241         finally:
   2242             # do what we can to clean up

~\miniconda3\envs\PDF_RandD\lib\site-packages\PIL\JpegImagePlugin.py in _save(im, fp, filename)
    780     bufsize = max(ImageFile.MAXBLOCK, bufsize, len(exif) + 5, len(extra) + 1)
    781 
--> 782     ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
    783 
    784 

~\miniconda3\envs\PDF_RandD\lib\site-packages\PIL\ImageFile.py in _save(im, fp, tile, bufsize)
    536                 s = e.encode_to_file(fh, bufsize)
    537             if s < 0:
--> 538                 raise OSError(f"encoder error {s} when writing image file")
    539             e.cleanup()
    540     if hasattr(fp, "flush"):

OSError: encoder error -2 when writing image file

The question is

  1. how to vertically stack 50 images?
1 Answers

How big are your images? Maybe you are exceeding JPEG pixel limit?

JPEG/JFIF supports a maximum image size of 65,535×65,535 pixels. Wikipedia

Related