Python 图像库:如何将 4 个图像组合成 2 x 2 网格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4567409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Image Library: How to combine 4 images into a 2 x 2 grid?
提问by Nope
I have 4 directories with images for an animation. I would like to take the set of images and generate a single image with the 4 images arranged into a 2x2 grid for each frame of the animation.
我有 4 个带有动画图像的目录。我想获取一组图像并生成一个图像,其中 4 个图像为动画的每一帧排列成 2x2 网格。
My code so far is:
到目前为止我的代码是:
import Image
fluid64 = "Fluid64_half_size/00"
fluid128 = "Fluid128_half_size/00"
fluid512 = "Fluid512_half_size/00"
fluid1024 = "Fluid1024_half_size/00"
out_image = "Fluid_all/00"
for pic in range(1, 26):
blank_image = Image.open("blank.jpg")
if pic < 10:
image_num = "0"+str(pic)
else:
image_num = str(pic)
image64 = Image.open(fluid64+image_num+".jpg")
image128 = Image.open(fluid128+image_num+".jpg")
image512 = Image.open(fluid512+image_num+".jpg")
image1024 = Image.open(fluid1024+image_num+".jpg")
out = out_image + image_num + ".jpg"
blank_image.paste(image64, (0,0)).paste(fluid128, (400,0)).paste(fluid512, (0,300)).paste(fluid1024, (400,300)).save(out)
Not sure why it's not working. I'm getting the error:
不知道为什么它不起作用。我收到错误:
Traceback (most recent call last):
File "C:\Users\Casey\Desktop\Image_composite.py", line 24, in <module>
blank_image.paste(image64, (0,0)).paste(fluid128, (400,0)).paste(fluid512, (
ste(fluid1024, (400,300)).save(out)
AttributeError: 'NoneType' object has no attribute 'paste'
shell returned 1
Any help would be awesome. Thanks!
任何帮助都是极好的。谢谢!
采纳答案by jsbueno
The only problem there is that "paste" does not return an image object - it rather modifies the "blank" image inplace.
唯一的问题是“粘贴”不会返回图像对象——而是就地修改“空白”图像。
So, when the second paste is called (the one that uses the fuild128 image), it tries to be applied on "None" - which is the return value of the first image.
因此,当调用第二个粘贴时(使用 fuild128 图像的粘贴),它会尝试应用于“无”——这是第一张图像的返回值。
If that is the only problem you are having, just make one paste call per line, like this:
如果这是您遇到的唯一问题,只需每行进行一次粘贴调用,如下所示:
blank_image.paste(image64, (0,0))
blank_image.paste(fluid128, (400,0))
blank_image.paste(fluid512, (0,300))
blank_image.paste(fluid1024, (400,300))
blank_image.save(out)
Although it looks likely you'd need to scale each image so that their format match as well. And your code for the "image_num" variable is unecessary. Python is really good with strings - just do something like this:
尽管看起来您可能需要缩放每个图像,以便它们的格式也匹配。你的“image_num”变量代码是不必要的。Python 非常擅长处理字符串 - 只需执行以下操作:
image64 = Image.open(fluid64 + "%02d.jpg" % pic)
回答by David Hewitt
You may want to be using something along the lines of :
您可能希望使用以下内容:
blank_image = Image.new("RGB", (800, 600))
This will create a new area in memory in which you can generate your image. You should then be able to paste you images into that.
这将在内存中创建一个新区域,您可以在其中生成图像。然后,您应该能够将图像粘贴到其中。
Then you'll need to save it out again later on with:
然后你需要稍后再次保存它:
blank_image.save("blank.jpg")
回答by Karl Knechtel
Read the error message:
阅读错误信息:
AttributeError: 'NoneType' object has no attribute 'paste'
This means you tried to call .pasteon something that was of type NoneType, i.e. on the Noneobject.
这意味着您试图调用.paste类型为 的东西NoneType,即在None对象上。
Image.pastereturns None. You can't "chain" together calls like that except when the functions are specifically designed to support it, and Image.pasteis not. (Support for this sort of thing is accomplished by having the function return self. You get an error that talks about NoneTypebecause the function is written not to return anything, and everything in Python returns Noneby default if nothing else is returned explicitly.) This is considered Pythonic: methods either return a new value, or modify selfand return None. Thus, so-called "fluent interfaces" are not used when the functions have side effects - Pythonistas consider that harmful. Returning Noneis a warning that the function has side effects. :)
Image.paste返回无。你不能将这样的调用“链接”在一起,除非函数是专门设计来支持它的,而Image.paste不是。(对这种事情的支持是通过使用函数来完成的return self。你会得到一个错误,NoneType因为函数被写成不返回任何东西,None如果没有明确返回任何其他东西,Python 中的一切都默认返回。)这被认为是 Pythonic : 方法要么返回一个新值,要么修改self并返回None。因此,当函数有副作用时,不会使用所谓的“流畅接口”——Pythonistas 认为这是有害的。返回None是警告该函数有副作用。:)
Just do four separate .pastecalls.
只需进行四个单独的.paste调用。
回答by michaelliu
回答by teekarna
Tiling figures in a 2-by-2 grid would be easy to achieve with the append_imagesfunction defined in this reply https://stackoverflow.com/a/46623632/8738113
使用此回复中定义的append_images函数 可以轻松实现在 2×2 网格中平铺图形https://stackoverflow.com/a/46623632/8738113
For example:
例如:
img1 = append_images([image64, image128], direction='horizontal')
img2 = append_images([image512, image1024], direction='horizontal')
final = append_images([img1, img2], direction='vertical')
final.save("Fluid_all/00.jpg")

