944,091 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 8410
  • Python RSS
Jul 17th, 2007
0

Paste a image into another

Expand Post »
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:

python Syntax (Toggle Plain Text)
  1. from PIL import Image
  2. import ImageDraw
  3.  
  4. im = Image.new("RGB", (500,500), "white")
  5. draw = ImageDraw.Draw(im)
  6.  
  7. icon = Image.open("icon1.jpg")
  8. im.paste(icon, (0,0,86,62))
  9.  
  10. del draw
  11. im.save("test.jpg", "JPEG")

and the message:

Python Syntax (Toggle Plain Text)
  1. Traceback (most recent call last):
  2. File "C:\Program Files\Eclipse 3.2.2\plugins\org.python.pydev.debug_1.3.4\pysrc\pydevd.py", line 754, in <module>
  3. debugger.run(setup['file'], None, None)
  4. File "C:\Program Files\Eclipse 3.2.2\plugins\org.python.pydev.debug_1.3.4\pysrc\pydevd.py", line 597, in run
  5. execfile(file, globals, locals) #execute the script
  6. File "C:\Documents and Settings\eviceng\My Documents\Python\cellTestare\src\paste.py", line 8, in <module>
  7. im.paste(icon, (0,0,86,62))
  8. File "C:\Python25\Lib\site-packages\PIL\Image.py", line 1076, in paste
  9. self.im.paste(im, box)
  10. ValueError: images do not match
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
flaerpen is offline Offline
23 posts
since Jun 2007
Jul 17th, 2007
0

Re: Paste a image into another

Make sure you give it the correct size of the icon ...
python Syntax (Toggle Plain Text)
  1. from PIL import Image
  2. import ImageDraw
  3.  
  4. im = Image.new("RGB", (500,500), "white")
  5. draw = ImageDraw.Draw(im)
  6.  
  7. icon = Image.open("icon1.jpg")
  8. # get the correct size
  9. x, y = icon.size
  10. im.paste(icon, (0,0,x,y))
  11.  
  12. del draw
  13. im.save("test.jpg", "JPEG")
  14.  
  15. # optional, show the saved image in the default viewer (works in Windows)
  16. import webbrowser
  17. webbrowser.open("test.jpg")
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 17th, 2007
0

Re: Paste a image into another

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:

python Syntax (Toggle Plain Text)
  1. im.paste(icon, icon.getbbox())

and I got a white image, nothing in there when I opened the image.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
flaerpen is offline Offline
23 posts
since Jun 2007
Jul 17th, 2007
0

Re: Paste a image into another

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 ...
python Syntax (Toggle Plain Text)
  1. print icon.mode
this should show "RGB"
Last edited by vegaseat; Jul 17th, 2007 at 6:04 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 17th, 2007
0

Re: Paste a image into another

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!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
flaerpen is offline Offline
23 posts
since Jun 2007
Jul 18th, 2007
0

Re: Paste a image into another

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:

python Syntax (Toggle Plain Text)
  1. from PIL import Image
  2. import ImageDraw
  3. import aggdraw
  4.  
  5. im = Image.new("RGB", (500,500), "white")
  6. draw = aggdraw.Draw(im)
  7.  
  8. icon = Image.open("icon.jpg")
  9. # get the correct size
  10. x, y = icon.size
  11. im.paste(icon, (0,0,x,y))
  12.  
  13. draw.flush()
  14. 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()
Reputation Points: 10
Solved Threads: 1
Newbie Poster
flaerpen is offline Offline
23 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Cookie retrieval works in python but not IE
Next Thread in Python Forum Timeline: Data View





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC