We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,374 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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:

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
2
Contributors
5
Replies
21 Hours
Discussion Span
5 Years Ago
Last Updated
8
Views
Question
Answered
flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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")
vegaseat
DaniWeb's Hypocrite
Moderator
6,478 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36

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.

flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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"

vegaseat
DaniWeb's Hypocrite
Moderator
6,478 posts since Oct 2004
Reputation Points: 1,447
Solved Threads: 1,612
Skill Endorsements: 36

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!

flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 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()

flaerpen
Newbie Poster
23 posts since Jun 2007
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
Question Answered as of 3 Years Ago by vegaseat

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0677 seconds using 2.67MB