Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Simple photo collage using Python and PIL

Reply
Thread Tools

Simple photo collage using Python and PIL

 
 
Callum Prentice
Guest
Posts: n/a
 
      11-23-2005
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

 
Reply With Quote
 
 
 
 
Thomas Guettler
Guest
Posts: n/a
 
      11-23-2005
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: http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      11-23-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, 204, "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>



 
Reply With Quote
 
Callum Prentice
Guest
Posts: n/a
 
      11-24-2005
Thank you Thomas - I've heard good things about Image Magick - I'll go
read up on it.

Cheers.

Cal.

 
Reply With Quote
 
Callum Prentice
Guest
Posts: n/a
 
      11-24-2005
Thanks very much Fredrik - just what I was looking for - that gives me
a good place to start from. Much appreciated.

Cal.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple app to do collage? Lukus Computer Support 4 03-03-2009 01:32 PM
How to make a photo collage? typing Digital Photography 3 08-25-2006 03:55 PM
Winners and Runners-up in March Collage and Panorama Competitions wayne Digital Photography 0 04-10-2006 07:43 AM
Photo Collage susan Digital Photography 6 01-26-2006 04:26 AM
photo collage salgud Digital Photography 9 12-17-2005 12:00 PM



Advertisments