使用PIL对目录进行分块并应用图像混合。无法使用Python正确保存图像

2024-10-02 08:31:08 发布

您现在位置:Python中文网/ 问答频道 /正文

抱歉,标题。。。因此,这个脚本的目标是获取一个文件夹,其中包含按特定顺序列出的图像。然后将图像分成3组。从那里,它采取了3个图像和融合他们在一起使用PIL。现在我的问题是下面的代码在做我想做的事情方面做得很好。我可以显示imgbld2它将在一个临时文件夹中创建4个图像。你知道吗

现在我的问题是,当我使用imgbld2.save()保存图像时,它只会将第一个创建的图像保存到4个图像文件中,而不是将4个创建的图像保存到4个单独的文件中。你知道吗

我可以通过使用glob.glob()指向另一个脚本从temp文件夹检索图像来解决这个问题。但这需要我确保在重新启动的计算机上运行脚本,但这对我来说似乎太混乱了。你知道吗

有没有更好的方法来实现我的目标?或者有一个保存方法我错过了?你知道吗

任何帮助都将不胜感激,代码如下:

from PIL import Image
import os.path
import glob

#Lists Directory
Dir = os.listdir('/path/to/Directory/of/Images')
#Glob all jpgs
im = glob.glob( '/path/to/Directory/of/Images/*.jpg')

#sort jpg according to name
imsort = sorted(im)

def chunker(imsort,size = 3):
    for i in range(0, len(imsort), size):
        yield imsort[i:i + size]

print('what does it look like?')

for j in chunker(imsort):
    print(j)

    img1 = Image.open(j[0])
    img2 = Image.open(j[1])
    img3 = Image.open(j[2])

    imgbld1 = Image.blend(img1, img2, 0.3)
    imgbld2 = Image.blend(imgbld1, img3, 0.3)

    imgbld2.show()
    imgbld2.save('path/to/new/folder/' + 'blended'  , 'JPEG') 

Tags: topath图像imageimport脚本文件夹目标

热门问题