13

I am using PIL for my project and I have ImageDraw object. I want to get the image that is drawn on ImageDraw object. How do I get the image ?

Nikhil Ashodariya
  • 131
  • 1
  • 1
  • 4

2 Answers2

15

Is THIS what you are looking for?

from PIL import Image, ImageDraw
im = Image.new('RGBA', (400, 400), (0, 255, 0, 0))
draw = ImageDraw.Draw(im) 
draw.text((20, 20), "DRAW TEXT", fill="red")
draw.polygon([(5,5), (25,5), (25,20), (5,25)], fill="green", outline=None)
im.show()
im.save("ImageDraw.png")

Here is the ImageDraw.png image file (resized 300%):

enter image description here

Here the draw object is used for drawing polygons in the image. While drawing to the draw object you changed the im object.

P.S. check out also :

Drawing a line on an image with pil

Python imaging library save function

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
Claudio
  • 7,474
  • 3
  • 18
  • 48
  • 2
    What if I'm passed just the ImageDraw.Draw object? Is there any way to get the image back from that? – Pro Q Jun 20 '18 at 20:39
  • Sorry, don't know a way to get the image back out of the ImageDraw.Draw object. Maybe there is a way, but I suppose that there isn't because the image content itself isn't part of the ImageDraw.Draw object. If it is possible to delete the image itself without an error, the ImageDraw.Draw object linking to it will point to a non existing image, so at least at this point there should be an error while trying to draw on that image. – Claudio Jun 21 '18 at 22:11
  • try `draw.im` (e.g.: `draw.im.size` will return the image size) – tamersalama Jul 17 '18 at 22:22
  • You do not need to get the image back from the draw, the original image you used as parameter in the first call : draw = ImageDraw.Draw(im) has been directly altered and so so you can just use "im.width" and that will be same image that has the darw in it. – MMEL Nov 06 '20 at 00:12
  • imd.im (as in PIL.ImageDraw(PIL.Image.Image)) returns something like it (PIL.Image.Image) and has many methods with the same name but is not actually the same as PIL.Image.Image. This is really useful for getting size, channels and many other things. And you can use it for pasting and other things, however they take similar but different parameters which are not documented nor is there any promise of future support or consistency. So unless it's a project where the production is the important part or it's just a one off, better don't use that except to get image size and mode. – Ismael Harun Apr 22 '21 at 13:08
7

It's simply:

original_image = edited_image._image

The im object gets passed into ImageDraw, then gets saved into the class variable _image. That's about it.

Lafftar
  • 145
  • 3
  • 11