我一直在嘗試將影像合并成一張長影像。這些影像都在一個檔案夾中,并且具有相同的檔案名格式group1.X.jpg,X都是從 0 開始的數字。我嘗試使用img.paste將影像合并在一起。這基本上是我一直在嘗試的:
img1 = Image.open(directory filePrefix str(fileFirst) fileExtension)
w, h = img1.size
h = h * fileCount
img = Image.new("RGB", (w, h))
tmpImg = img.load()
console.log("Setup Complete")
number = 0
for number in track(range(fileCount - 1)):
imgPaste = Image.open(dir prefix str(number) extension)
if not number >= fileCount:
Image.Image.paste(tmpImg, imgPaste, (0, h * (number 1)))
number = 1
img.save(file)
stitch(directory, filePrefix, fileExtension)
上面的代碼在運行時會輸出以下內容:
Working... ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 0% -:--:--
Traceback (most recent call last):
File "c:\Users\marcus\Desktop\Stuff\Files\code\webtoonstitcher.py", line 36, in <module>
stitch(directory, filePrefix, fileExtension)
File "c:\Users\marcus\Desktop\Stuff\Files\code\webtoonstitcher.py", line 32, in stitch
Image.Image.paste(tmpImg, imgPaste, (0, h * (number 1)))
File "C:\Users\marcus\AppData\Roaming\Python\Python310\site-packages\PIL\Image.py", line 1619, in paste
if self.mode != im.mode:
AttributeError: 'PixelAccess' object has no attribute 'mode'```
uj5u.com熱心網友回復:
您可以使用獲取所有影像的串列,glob然后遍歷該串列:
import glob
from PIL import Image
def stitch(directory, file_prefix, file_extension):
files = glob.glob(directory f'{file_prefix}*.{file_extension}')
images = [Image.open(file) for file in files]
background_width = max([image.width for image in images])
background_height = sum([image.height for image in images])
background = Image.new('RGBA', (background_width, background_height), (255, 255, 255, 255))
y = 0
for image in images:
background.paste(image, (0, y))
y = image.height
background.save('image.png')
stitch('', 'group1', 'png')
樣本輸出:

uj5u.com熱心網友回復:
我認為問題在這里:
h = h * fileCount
img = Image.new("RGB", (w, h))
您應該洗掉第一行并將第二行更改為:
img = Image.new("RGB", (w, h * fileCount))
否則h,當您稍后使用它來計算paste().
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/497130.html
