#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11

    How do I use PIL with an existing image?


    I made one of those signature scripts, but, one of the things is that the background is black. I would like to change that to a picture, but, how would I do that with PIL? btw, the source for the script is at: http://nick125.com/~nick125/image.py
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Instead of:

    Code:
    img = Image.new("RGB", (575,55));
    Try:

    Code:
    img = Image.open("background.gif")
    (The semi-colon at the end isn't needed (though Python wont complain about it)).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    cool.
    btw, about the ;, that was there when the script was one line, and when I cleaned it up, I forgot to remove it. maybe next time, I should do a better cleanup job
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    okay, another question: how do I change the font and make the text bigger?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    another thing: I want to change the text color, the PIL docs arent too helpful with this. any suggestions/examples/etc?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Now here's the pil Nano-HOW-TO:
    Code:
    import Image, ImageDraw, ImageFont
    
    # create an 100x100 RGB image with gray background
    im = Image.new("RGB", (100, 100), "#EEEEEE")
    
    # create a draw object
    dr  = ImageDraw.Draw(im)
    
    # create a font object with Arial Font Size 24
    # Note: this works for windows. On Linux you may need to specify
    # the full path of the font file
    fnt = ImageFont.truetype("arial.ttf", 24)
    
    # draw RED text beginning at position 10, 20
    # the 'fill' parameter sets the font color
    dr.text((10, 20), "Test", font = fnt, fill = "#FF0000")
    
    # save the image
    i.save("test.gif")
    Or you may want to set drawing color and font type and size globally for the whole image:
    Code:
    import Image, ImageDraw, ImageFont
    
    # create an 100x100 RGB image with gray background
    im = Image.new("RGB", (100, 100), "#EEEEEE")
    
    # create a draw object
    dr  = ImageDraw.Draw(im)
    
    # set Arial Font Size 24
    dr.setfont(ImageFont.truetype("arial.ttf", 24))
    
    # set drawing color
    dr.setink("#FF0000")
    
    # draw text
    dr.text((10, 20), "Test")
    
    # save the image
    i.save("test.gif")

    Comments on this post

    • whitelines agrees
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11

IMN logo majestic logo threadwatch logo seochat tools logo