Python Image Library: How to combine 4 images into a 2 x 2 grid?

前端 未结 5 994
情深已故
情深已故 2020-12-08 14:38

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 fr

5条回答
  •  醉酒成梦
    2020-12-08 15:14

    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.

    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:

    image64 = Image.open(fluid64 + "%02d.jpg" % pic)
    

提交回复
热议问题