我有50个图像。我想把它垂直堆叠,得到一个单独的图像。我可以垂直堆叠23个图像。当我尝试堆叠24个或以上的图像时,它显示错误。我使用了以下代码。
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' )
这是我得到的错误:
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' )
~miniconda3envsPDF_RandDlibsite-packagesPILImage.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
~miniconda3envsPDF_RandDlibsite-packagesPILJpegImagePlugin.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
~miniconda3envsPDF_RandDlibsite-packagesPILImageFile.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
问题是
- 如何垂直堆叠50个图像
您的图像有多大?也许您超出了JPEG像素限制?
JPEG/JFIF支持65535×65535像素的最大图像大小。维基百科