Paste a image into another

Thread Solved

Join Date: Jun 2007
Posts: 23
Reputation: flaerpen is an unknown quantity at this point 
Solved Threads: 1
flaerpen flaerpen is offline Offline
Newbie Poster

Paste a image into another

 
0
  #1
Jul 17th, 2007
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:

  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:

  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Paste a image into another

 
0
  #2
Jul 17th, 2007
Make sure you give it the correct size of the icon ...
  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")
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 23
Reputation: flaerpen is an unknown quantity at this point 
Solved Threads: 1
flaerpen flaerpen is offline Offline
Newbie Poster

Re: Paste a image into another

 
0
  #3
Jul 17th, 2007
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:

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

and I got a white image, nothing in there when I opened the image.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,009
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 928
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Paste a image into another

 
0
  #4
Jul 17th, 2007
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 ...
  1. print icon.mode
this should show "RGB"
Last edited by vegaseat; Jul 17th, 2007 at 6:04 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 23
Reputation: flaerpen is an unknown quantity at this point 
Solved Threads: 1
flaerpen flaerpen is offline Offline
Newbie Poster

Re: Paste a image into another

 
0
  #5
Jul 17th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 23
Reputation: flaerpen is an unknown quantity at this point 
Solved Threads: 1
flaerpen flaerpen is offline Offline
Newbie Poster

Re: Paste a image into another

 
0
  #6
Jul 18th, 2007
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:

  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()
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC