如何使用 Pillow 模块将列表中的图像组粘贴到基本图像之上?
How to paste groups of images from a list on top of a base image with the Pillow module?
所有图片均为PNG格式。
from PIL import Image
首先,我有一个基本图像:
image_base = Image.open (image.png)
其次,我会列出我想要的组合(每个字符串都链接到一个图像路径)以粘贴 over 基本图像,这样组合 [0] [ 0]、组合 [0] [1] 和粘贴在基本图像顶部的组合 [0] [2] 将构成所需的 8 个最终图像(查看第一个索引,它上升到 7)。
list = ['combinations [0] [0]', 'combinations [0] [1]', 'combinations [0] [2]', 'combinations [1] [0]', 'combinations [1] [1 ] ',' combinations [1] [2] ',' combinations [2] [0] ',' combinations [2] [1] ',' combinations [2] [2] ',' combinations [3] [0 ] ',' combinations [3] [1] ',' combinations [3] [2] ',' combinations [4] [0] ',' combinations [4] [1] ',' combinations [4] [2 ] ',' combinations [5] [0] ',' combinations [5] [1] ',' combinations [5] [2] ',' combinations [6] [0] ',' combinations [6] [1 ] ',' combinations [6] [2] ',' combinations [7] [0] ',' combinations [7] [1] ',' combinations [7] [2] ']
我的目标是找出如何遍历列表并将每个组合与相同的第一个索引粘贴到基本图像上方。在这种情况下,列表中的每3个值,创建一个组合。
这是range(len())
有用的情况,因为它可以在range()
中使用step
for i in range(0, len(list), 3):
print(list[i:i+3])
它给出列表
['combinations [0] [0]', 'combinations [0] [1]', 'combinations [0] [2]']
['combinations [1] [0]', 'combinations [1] [1]', 'combinations [1] [2]']
['combinations [2] [0]', 'combinations [2] [1]', 'combinations [2] [2]']
['combinations [3] [0]', 'combinations [3] [1]', 'combinations [3] [2]']
['combinations [4] [0]', 'combinations [4] [1]', 'combinations [4] [2]']
['combinations [5] [0]', 'combinations [5] [1]', 'combinations [5] [2]']
['combinations [6] [0]', 'combinations [6] [1]', 'combinations [6] [2]']
['combinations [7] [0]', 'combinations [7] [1]', 'combinations [7] [2]']
现在您可以使用每个列表来创建图像
new = base.copy()
for path in list[i:i+3]:
image = Image.open(path)
new.paste(image, ....)
PIL 文档:Image.paste()
编辑:
嵌套循环。
for i in range(0, len(list), 3):
#print(list[i:i+3])
new = base.copy()
for path in list[i:i+3]:
image = Image.open(path)
new.paste(image, ....)
所有图片均为PNG格式。
from PIL import Image
首先,我有一个基本图像:
image_base = Image.open (image.png)
其次,我会列出我想要的组合(每个字符串都链接到一个图像路径)以粘贴 over 基本图像,这样组合 [0] [ 0]、组合 [0] [1] 和粘贴在基本图像顶部的组合 [0] [2] 将构成所需的 8 个最终图像(查看第一个索引,它上升到 7)。
list = ['combinations [0] [0]', 'combinations [0] [1]', 'combinations [0] [2]', 'combinations [1] [0]', 'combinations [1] [1 ] ',' combinations [1] [2] ',' combinations [2] [0] ',' combinations [2] [1] ',' combinations [2] [2] ',' combinations [3] [0 ] ',' combinations [3] [1] ',' combinations [3] [2] ',' combinations [4] [0] ',' combinations [4] [1] ',' combinations [4] [2 ] ',' combinations [5] [0] ',' combinations [5] [1] ',' combinations [5] [2] ',' combinations [6] [0] ',' combinations [6] [1 ] ',' combinations [6] [2] ',' combinations [7] [0] ',' combinations [7] [1] ',' combinations [7] [2] ']
我的目标是找出如何遍历列表并将每个组合与相同的第一个索引粘贴到基本图像上方。在这种情况下,列表中的每3个值,创建一个组合。
这是range(len())
有用的情况,因为它可以在range()
step
for i in range(0, len(list), 3):
print(list[i:i+3])
它给出列表
['combinations [0] [0]', 'combinations [0] [1]', 'combinations [0] [2]']
['combinations [1] [0]', 'combinations [1] [1]', 'combinations [1] [2]']
['combinations [2] [0]', 'combinations [2] [1]', 'combinations [2] [2]']
['combinations [3] [0]', 'combinations [3] [1]', 'combinations [3] [2]']
['combinations [4] [0]', 'combinations [4] [1]', 'combinations [4] [2]']
['combinations [5] [0]', 'combinations [5] [1]', 'combinations [5] [2]']
['combinations [6] [0]', 'combinations [6] [1]', 'combinations [6] [2]']
['combinations [7] [0]', 'combinations [7] [1]', 'combinations [7] [2]']
现在您可以使用每个列表来创建图像
new = base.copy()
for path in list[i:i+3]:
image = Image.open(path)
new.paste(image, ....)
PIL 文档:Image.paste()
编辑:
嵌套循环。
for i in range(0, len(list), 3):
#print(list[i:i+3])
new = base.copy()
for path in list[i:i+3]:
image = Image.open(path)
new.paste(image, ....)