Simple photo collage using Python and PIL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Callum  Prentice

    #1

    Simple photo collage using Python and PIL

    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

  • Thomas Guettler

    #2
    Re: Simple photo collage using Python and PIL

    Am Tue, 22 Nov 2005 16:10:10 -0800 schrieb Callum Prentice:
    [color=blue]
    > 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[/color]

    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: niemand.leerman n@thomas-guettler.de

    Comment

    • Fredrik Lundh

      #3
      Re: Simple photo collage using Python and PIL

      Callum Prentice wrote:
      [color=blue]
      > 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.[/color]

      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.tx t"):
      x, y, name, pngfile = line.split()
      out.paste(Image .open(pngfile), (int(x), int(y)))

      out.save("out.p ng")

      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:

      Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python’s elegant syntax an...

      http://www.pythonware.com/library/pi...troduction.htm

      </F>



      Comment

      • Callum  Prentice

        #4
        Re: Simple photo collage using Python and PIL

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

        Cheers.

        Cal.

        Comment

        • Callum  Prentice

          #5
          Re: Simple photo collage using Python and PIL

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

          Cal.

          Comment

          Working...