By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,743 Members | 1,072 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,743 IT Pros & Developers. It's quick & easy.

Simple photo collage using Python and PIL

P: n/a
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

Nov 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Thomas GŁttler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
Spam Catcher: ni**************@thomas-guettler.de

Nov 23 '05 #2

P: n/a
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/pi...troduction.htm

</F>

Nov 23 '05 #3

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

Cheers.

Cal.

Nov 24 '05 #4

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

Cal.

Nov 24 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.