gpt4 book ai didi

python - 在 PIL 中制作拼贴画

转载 作者:行者123 更新时间:2023-11-28 21:11:24 26 4
gpt4 key购买 nike

我。是。卡住。

我已经为此工作了一个多星期,但我似乎无法让我的代码正确运行。作为一个整体,我对 PIL 和 Python 相当陌生。我正在尝试制作一些图片的 2x3 拼贴画。下面列出了我的代码。我试图让我的照片适合新创建的拼贴画中没有任何访问空白的空间,但是当我运行我的代码时,我只能将 2 张图片放入拼贴画中,而不是我想要的 6 张。任何的意见都将会有帮助。

*代码已编辑

from PIL import Image
im= Image.open('Tulips.jpg')




out=im.convert("RGB", (
0.412453, 0.357580, 0.180423, 0,
0.212671, 0.715160, 0.072169, 0,
0.019334, 0.119193, 0.950227, 0 ))
out.save("Image2" + ".jpg")

out2=im.convert("RGB", (
0.9756324, 0.154789, 0.180423, 0,
0.212671, 0.715160, 0.254783, 0,
0.123456, 0.119193, 0.950227, 0 ))
out2.save("Image3" + ".jpg")



out3= im.convert("1")
out3.save("Image4"+".jpg")


out4=im.convert("RGB", (
0.986542, 0.154789, 0.756231, 0,
0.212671, 0.715160, 0.254783, 0,
0.123456, 0.119193, 0.112348, 0 ))
out4.save("Image5" + ".jpg")


out5=Image.blend(im, out4, 0.5)
out5.save("Image6" + ".jpg")

listofimages=['Tulips.jpg', 'Image2.jpg', 'Image3.jpg', 'Image4.jpg', 'Image5.jpg', 'Image6.jpg']

def create_collage(width, height, listofimages):
Picturewidth=width//3
Pictureheight=height//2
size=Picturewidth, Pictureheight
new_im=Image.new('RGB', (450, 300))
for p in listofimages:
Image.open(p)
for col in range(0,width):
for row in range(0, height):
image=Image.eval(p, lambda x: x+(col+row)/30)
new_im.paste(p, (col,row))
new_im.save("Collage"+".jpg")

create_collage(450,300,listofimages)

最佳答案

这是一些工作代码。

  1. 当您调用 Image.open(p) 时,它会返回一个 Image 对象,因此您需要将比存储在一个变量中:im = Image.open(p) .

  2. 我不确定 image=Image.eval(p, lambda x: x+(col+row)/30) 是什么意思,所以我删除了它。

  3. size 是缩略图的大小,但您没有使用该变量。打开图片后,应将其调整为大小

  4. 我将 Picturewidth 和 Pictureheight 重命名为 thumbnail_width 和 thumbnail_height 以明确它们的含义并遵循 Python 命名约定。

  5. 我还将列数和行数移动到变量中,这样它们就可以在没有魔数(Magic Number)的情况下重复使用。

  6. 第一个循环将每个图像打开到 im 中,将其缩略图并将其放入 ims 列表中。

  7. 在下一个循环之前,我们初始化 i、xy` 变量以跟踪我们正在查看的图像,以及 x 和y 坐标将缩略图粘贴到更大的 Canvas 中。它们将在下一个循环中更新。

  8. 第一个循环针对的是列 (cols),而不是像素 (width)。 (此外,range(0, thing)range(thing) 的作用相同。)

  9. 同样,第二个循环针对的是行而不是像素。在这个循环中,我们将 ims[i] 中的当前图像粘贴到 x, y 中的大 new_im 中。这些是像素位置,而不是行/列位置。

  10. 在内循环结束时,增加 i 计数器,并将 thumbnail_height 添加到 y

  11. 类似地,在外层循环的末尾,将thumnnail_width 添加到x 并将y 重置为零。

  12. 在这些循环完成后,您只需要保存一次 new_im

  13. 不需要连接 "Image2"+ ".jpg" 等,只需做 "Image2.jpg"。

结果是这样的:

Collage.jpg

此代码可以改进。例如,如果您不需要它们做任何其他事情,则无需保存中间的 ImageX.jpg 文件,与其将这些文件名放在 listofimages 中,不如将图像直接放在那里:listofimages = [im, out1, out2, etc...],然后将 for p in listofimages: 替换为 for im in listofimages: 并删除im = Image.open(p)

您还可以为图像计算一些填充,使黑色空间均匀。

from PIL import Image
im= Image.open('Tulips.jpg')

out=im.convert("RGB", (
0.412453, 0.357580, 0.180423, 0,
0.212671, 0.715160, 0.072169, 0,
0.019334, 0.119193, 0.950227, 0 ))
out.save("Image2.jpg")

out2=im.convert("RGB", (
0.9756324, 0.154789, 0.180423, 0,
0.212671, 0.715160, 0.254783, 0,
0.123456, 0.119193, 0.950227, 0 ))
out2.save("Image3.jpg")

out3= im.convert("1")
out3.save("Image4.jpg")

out4=im.convert("RGB", (
0.986542, 0.154789, 0.756231, 0,
0.212671, 0.715160, 0.254783, 0,
0.123456, 0.119193, 0.112348, 0 ))
out4.save("Image5.jpg")

out5=Image.blend(im, out4, 0.5)
out5.save("Image6.jpg")

listofimages=['Tulips.jpg', 'Image2.jpg', 'Image3.jpg', 'Image4.jpg', 'Image5.jpg', 'Image6.jpg']

def create_collage(width, height, listofimages):
cols = 3
rows = 2
thumbnail_width = width//cols
thumbnail_height = height//rows
size = thumbnail_width, thumbnail_height
new_im = Image.new('RGB', (width, height))
ims = []
for p in listofimages:
im = Image.open(p)
im.thumbnail(size)
ims.append(im)
i = 0
x = 0
y = 0
for col in range(cols):
for row in range(rows):
print(i, x, y)
new_im.paste(ims[i], (x, y))
i += 1
y += thumbnail_height
x += thumbnail_width
y = 0

new_im.save("Collage.jpg")

create_collage(450, 300, listofimages)

关于python - 在 PIL 中制作拼贴画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35438802/

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