[Image-SIG] combine jpeg's

Fredrik Lundh fredrik at pythonware.com
Wed Nov 12 10:28:13 CET 2008


Nils Wagner wrote:

> Is it possible to combine 4 jpegs into 1 jpeg ?
> A small example would be appreciated.

The correct way to do this depends on your definition of "combine", of 
course, but assuming that you have four smaller JPEG files, all having 
the same size, you can simply do:

     im1 = Image.open("...")
     im2 = Image.open("...")
     im3 = Image.open("...")
     im4 = Image.open("...")

     # assume all input images have the same size
     x = im1.size[0]
     y = im1.size[1]

     out = Image.new("RGB", (x+x, y+y), None)

     out.paste((0, 0), im1)
     out.paste((x, 0), im2)
     out.paste((0, y), im3)
     out.paste((x, y), im4)

     out.save("out.jpg")

Note that recompressing JPEG images may introduce artifacts, but if you 
avoid doing that over and over, that shouldn't be much of a problem for 
this specific case.

</F>



More information about the Image-SIG mailing list