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

前端 未结 5 991
情深已故
情深已故 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:02

    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")
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-08 15:15

    Tiling figures in a 2-by-2 grid would be easy to achieve with the append_images function defined in this reply 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")
    
    0 讨论(0)
  • 2020-12-08 15:22

    Read the error message:

    AttributeError: 'NoneType' object has no attribute 'paste'
    

    This means you tried to call .paste on something that was of type NoneType, i.e. on the None object.

    Image.paste returns None. You can't "chain" together calls like that except when the functions are specifically designed to support it, and Image.paste is not. (Support for this sort of thing is accomplished by having the function return self. You get an error that talks about NoneType because the function is written not to return anything, and everything in Python returns None by default if nothing else is returned explicitly.) This is considered Pythonic: methods either return a new value, or modify self and return None. Thus, so-called "fluent interfaces" are not used when the functions have side effects - Pythonistas consider that harmful. Returning None is a warning that the function has side effects. :)

    Just do four separate .paste calls.

    0 讨论(0)
  • 2020-12-08 15:25

    Unlike PIL APIs copy, crop, resize or rotate which return an Image object, paste returns None which prevents chained method calls. Not so convenient API design.

    0 讨论(0)
提交回复
热议问题