I have to admit this is a pretty unimpressive border.
The border function can be expanded to make it possible to customize
borders a bit more. Instead of assuming a border has a width and a color, let's
describe a border as a list of width/color tuples.
Now that we've improved upon the border a bit, let's make a function which takes a list of images and displays them together neatly. To keep things simple, here are a few compromises we'll make:
we'll scale all images to have the same height,
we'll break pictures into chunks where the largest chunk is the size of the smallest original image,
we'll tile the pictures uniformly.
First we need to normalize all the height of all images such that they are
all the height of the shortest image.
defnormalize(imgs):"Normalize height for all images to shortest image."shortest=min([x.sizeforxinimgs])resized=forimginimgs:height_ratio=float(img.size)/shortestnew_width=img.size*height_ratioimg2=img.resize((new_width,shortest),Image.ANTIALIAS)resized.append(img2)returnresized
Next, we break wider images into chunks where each chunk is
as wide as the skinniest image.
defchunk(imgs):"Break images into chunks equal to size of smallest image."smallest=min([x.sizeforxinimgs])height=imgs.sizechunked_imgs=forimginimgs:parts=math.ceil((img.size*1.0)/smallest)foriinxrange(0,parts):box=(i*smallest,0,(i+1)*smallest,height)img2=img.crop(box)img2.load()chunked_imgs.append(img2)returnchunked_imgs
Third we need to tile the images into the album page.
(Note that merge assumes images have been
normalized and chunked.)
defmerge(imgs,per_row=4):"Format equally sized images into rows and columns."width=imgs.sizeheight=imgs.sizepage_width=width*per_rowpage_height=height*math.ceil((1.0*len(imgs))/4)page=Image.new("RGB",(page_width,page_height),"White")column=0row=0forimginimgs:ifcolumn!=0andcolumn%per_row==0:row=row+1column=0pos=(width*column,height*row)page.paste(img,pos)column=column+1returnpage
Finally, we wrap up these function calls into the album function
which uses them together and also adds a border.
Here I used all the images from the Kamioka article, which happened to already
be of the same sizes.
And here is the output applied against the original images plus the output of the first call to album.
So, that looks totally horrible, but with better choices of images it might actually look
decent. I certainly enjoyed getting to work with PIL, and I hope some of
the snippets from this meandering project serve as helpful examples.