Paste a image into another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • flaerpen
    New Member
    • Jul 2007
    • 5

    Paste a image into another

    Hi, I'm trying to use the function paste() to paste a image into another image but I can't get it work! Is it the right function to use or is there any other function that's better?

    Here's my very simple code:
    [CODE=python]
    from PIL import Image
    import ImageDraw

    im = Image.new("RGB" , (500,500), "white")
    draw = ImageDraw.Draw( im)

    icon = Image.open("ico n1.jpg")
    im.paste(icon, (0,0,86,62))

    del draw
    im.save("test.j pg", "JPEG")[/CODE]

    and the message:
    Code:
    Traceback (most recent call last):
      File "C:\Program Files\Eclipse 3.2.2\plugins\org.python.pydev.debug_1.3.4\pysrc\pydevd.py", line 754, in <module>
        debugger.run(setup['file'], None, None)
      File "C:\Program Files\Eclipse 3.2.2\plugins\org.python.pydev.debug_1.3.4\pysrc\pydevd.py", line 597, in run
        execfile(file, globals, locals) #execute the script
      File "C:\Documents and Settings\eviceng\My Documents\Python\cellTestare\src\paste.py", line 8, in <module>
        im.paste(icon, (0,0,86,62))
      File "C:\Python25\Lib\site-packages\PIL\Image.py", line 1076, in paste
        self.im.paste(im, box)
    ValueError: images do not match
    /flaerpen
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by flaerpen
    Hi, I'm trying to use the function paste() to paste a image into another image but I can't get it work! Is it the right function to use or is there any other function that's better?

    Here's my very simple code:
    [CODE=python]
    from PIL import Image
    import ImageDraw

    im = Image.new("RGB" , (500,500), "white")
    draw = ImageDraw.Draw( im)

    icon = Image.open("ico n1.jpg")
    im.paste(icon, (0,0,86,62))

    del draw
    im.save("test.j pg", "JPEG")[/CODE]

    and the message:
    Code:
    Traceback (most recent call last):
      File "C:\Program Files\Eclipse 3.2.2\plugins\org.python.pydev.debug_1.3.4\pysrc\pydevd.py", line 754, in <module>
        debugger.run(setup['file'], None, None)
      File "C:\Program Files\Eclipse 3.2.2\plugins\org.python.pydev.debug_1.3.4\pysrc\pydevd.py", line 597, in run
        execfile(file, globals, locals) #execute the script
      File "C:\Documents and Settings\eviceng\My Documents\Python\cellTestare\src\paste.py", line 8, in <module>
        im.paste(icon, (0,0,86,62))
      File "C:\Python25\Lib\site-packages\PIL\Image.py", line 1076, in paste
        self.im.paste(im, box)
    ValueError: images do not match
    /flaerpen
    I got this from the PIL website:
    Code:
    im.paste(image, box)
    
    Pastes another image into this image. The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, right, and lower pixel coordinate, or None (same as (0, 0)). [B]If a 4-tuple is given, the size of the pasted image must match the size of the region.[/B]
    So either give a 2-tuple or make the coordinates match icon.jpg's.

    Comment

    Working...