Simple photo collage using Python and PIL

Discussion in 'Python' started by Callum Prentice, Nov 23, 2005.

  1. 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
    Callum Prentice, Nov 23, 2005
    #1
    1. Advertising

  2. 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:
    Thomas Guettler, Nov 23, 2005
    #2
    1. Advertising

  3. 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>
    Fredrik Lundh, Nov 23, 2005
    #3
  4. Thank you Thomas - I've heard good things about Image Magick - I'll go
    read up on it.

    Cheers.

    Cal.
    Callum Prentice, Nov 24, 2005
    #4
  5. Thanks very much Fredrik - just what I was looking for - that gives me
    a good place to start from. Much appreciated.

    Cal.
    Callum Prentice, Nov 24, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. RC
    Replies:
    2
    Views:
    465
    John M Deal
    Nov 24, 2004
  2. blather

    Web Collage generator

    blather, Jan 23, 2007, in forum: Java
    Replies:
    0
    Views:
    339
    blather
    Jan 23, 2007
  3. SPE - Stani's Python Editor

    ANN: Phatch = PHoto bATCH processor and renamer based on PIL

    SPE - Stani's Python Editor, Feb 18, 2008, in forum: Python
    Replies:
    26
    Views:
    2,085
    SPE - Stani's Python Editor
    Apr 7, 2008
  4. Sverre
    Replies:
    2
    Views:
    3,541
    Sverre
    Dec 17, 2009
  5. Francisco Soto
    Replies:
    0
    Views:
    402
    Francisco Soto
    Sep 3, 2012
Loading...

Share This Page