Simple photo collage using Python and PIL

Fredrik Lundh fredrik at pythonware.com
Wed Nov 23 12:59:34 EST 2005


Callum Prentice wrote:

> i need a "script" that i can use locally as well as online that will:
>
> * create a large (maybe something like 2k x 2k) master image in memory
> * open a text file and read all the lines from it (maybe 1000 lines
> max)
> * each line is composed of an x, y, name and a png image filename
> * for each line,  open the png image and position it in the master
> image at the location given by x & y
> * save off the master image to a png at the end
>
> i've been told python and the python image library can help me - i
> haven't used either before so can anyone give me some pointers to get
> me started please - it feels like it's probably just a few lines of
> code for an expert (no validation required - i'll be the only one using
> it)
>
> any help much appreciated.

the three first sections in the PIL handbook discusses how to create,
load, save, and cut/paste images.

here's an outline:

    import Image

    out = Image.new("RGB", (2048, 2048), "white")

    for line in open("myfile.txt"):
        x, y, name, pngfile = line.split()
        out.paste(Image.open(pngfile), (int(x), int(y)))

    out.save("out.png")

this assumes that the text file is named "myfile.txt", and contains white-
space separated items.

if you're new to both tools, skimming the tutorials before you start tinkering
will most likely save you some time later on:

    http://docs.python.org/tut/tut.html
    http://www.pythonware.com/library/pil/handbook/introduction.htm

</F> 






More information about the Python-list mailing list