gpt4 book ai didi

python - 在网格中写入包含多个图像的文件

转载 作者:太空狗 更新时间:2023-10-30 01:08:40 26 4
gpt4 key购买 nike

我正在尝试在 10x10 网格中写入包含多个图像 (100) 的文件。我使用 3 进行迭代:

-打开文件-设置坐标(i,j)

问题是当我查看我的文件时,我只能多次看到最后一张图片。每次程序进入 for 循环时,文件可能都会被覆盖。直到现在我都找不到解决方案。

代码是:

    import Image

from os import listdir
from os.path import isfile, join
files = [ f for f in listdir("/mnt/hgfs/Documents/Notebooks/test1/") if isfile(join("/mnt/hgfs/Documents/Notebooks/test1/", f)) ]

new_im = Image.new('RGB', (3000,3000))

for i in xrange(0,3000,300):
for j in xrange(0,3000,300):
for ima in files:
#paste the image at location i,j:
im = Image.open(ima)
im.thumbnail((300,300))
new_im.paste(im, (i,j))

new_im.save("hola.png")

谢谢!

最佳答案

这是一个简单的错误修复。您只需要两个 for 循环,而不是三个。

import Image

from os import listdir
from os.path import isfile, join
files = [ f for f in listdir("/mnt/hgfs/Documents/Notebooks/test1/") if isfile(join("/mnt/hgfs/Documents/Notebooks/test1/", f)) ]

new_im = Image.new('RGB', (3000,3000))

index = 0
for i in xrange(0,3000,300):
for j in xrange(0,3000,300):
im = Image.open(files[index])
im.thumbnail((300,300))
new_im.paste(im, (i,j))
index += 1

new_im.save("hola.png")

关于python - 在网格中写入包含多个图像的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20038648/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com