Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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!

like image 972
Nope Avatar asked Dec 31 '10 01:12

Nope


People also ask

Which technique is using for combining multiple photos into a single image?

A layer mask helps you combine two photos when you want to merge part of one photo into another photo. Bring the two images you want to combine into Photoshop and place them as two layers. The main image (the hand) should be at the bottom and the image you want to merge (the bulb) should be on top.


2 Answers

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) 
like image 187
jsbueno Avatar answered Sep 19 '22 15:09

jsbueno


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") 
like image 36
David Hewitt Avatar answered Sep 19 '22 15:09

David Hewitt