Simple photo collage using Python and PIL

C

Callum Prentice

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.

-- cal
 
T

Thomas Guettler

Am Tue, 22 Nov 2005 16:10:10 -0800 schrieb Callum Prentice:
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

Hi,

I would convert[1] the image to binary pnm[2]. This is a very easy
uncompressed image format. You don't need PIL to parse it.

Read the file into memory. You could use a matrix:

pic[0][0] first pixel
pic[-1][-1] last pixel

Then you coping or changing pixels is easy:

for x in xrange(?, ?):
for y in xrange(?, ?):
...

At the end create png from pnm with convert.

I hope this helps.

Thomas

[1] convert: http://www.imagemagick.org
[2] pnm: http://netpbm.sourceforge.net/doc/pnm.html
 
F

Fredrik Lundh

Callum said:
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>
 
C

Callum Prentice

Thank you Thomas - I've heard good things about Image Magick - I'll go
read up on it.

Cheers.

Cal.
 
C

Callum Prentice

Thanks very much Fredrik - just what I was looking for - that gives me
a good place to start from. Much appreciated.

Cal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top