1,105,447 Community Members

Paste a image into another

Member Avatar
flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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:

from PIL import Image
import ImageDraw

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

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

del draw
im.save("test.jpg", "JPEG")

and the message:

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
Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Make sure you give it the correct size of the icon ...

from PIL import Image
import ImageDraw
 
im = Image.new("RGB", (500,500), "white")
draw = ImageDraw.Draw(im)
 
icon = Image.open("icon1.jpg")
# get the correct size
x, y = icon.size
im.paste(icon, (0,0,x,y))
 
del draw
im.save("test.jpg", "JPEG")
 
# optional, show the saved image in the default viewer (works in Windows)
import webbrowser
webbrowser.open("test.jpg")
Member Avatar
flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Are you really sure? Because I know (86,62) is the right height and width. I can't test your code but I'll do first thing in the morning.

If I remember right I changed that line to icon.getbbox(), like this, before I went home from work:

im.paste(icon, icon.getbbox())

and I got a white image, nothing in there when I opened the image.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Strange, either method works fine for me!

I have Windows XP. What OS are you using? Shouldn't make a difference. Is your icon file a true JPEG?

Use ...

print icon.mode

this should show "RGB"

Member Avatar
flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Okej.. When I ran this script I was in, I think, windows 2000 - but in the end my script have to work on unix..

Do you get it working, a image paste into another? This is very strange.. I'll test the code you wrote tomorrow and then we'll see. I'll get back if it somehow doesn't work!

Member Avatar
flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Ahh, your code is working! But I now know why my script didn't work: I'm using the aggdraw module with PIL. my code look like this:

from PIL import Image
import ImageDraw
import aggdraw
 
im = Image.new("RGB", (500,500), "white")
draw = aggdraw.Draw(im)
 
icon = Image.open("icon.jpg")
# get the correct size
x, y = icon.size
im.paste(icon, (0,0,x,y))
 
draw.flush()
im.save("test.jpg", "JPEG")

and it's mostly the same and the only thing, that i can see, that isn't the same is:

draw = aggdraw.Draw(im)

and

draw.flush()

Question Answered as of 4 Years Ago by vegaseat
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article