44

I want to draw a line and show it. assume I have a PIL image.

draw = ImageDraw.Draw(pilImage)
draw.line((100,200, 150,300), fill=128)

How can I show the image? Before drawing the line I could do:

imshow(pilImage)

but imshow(draw) does not show the image.

How do I convert this back to a PIL image?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
eran
  • 14,496
  • 34
  • 98
  • 144

1 Answers1

84

This should work:

from PIL import Image, ImageDraw
im = Image.new('RGBA', (400, 400), (0, 255, 0, 255)) 
draw = ImageDraw.Draw(im) 
draw.line((100,200, 150,300), fill=128)
im.show()

Basically using ImageDraw draw over the image, then display that image after changes, to draw a thick line pass width

draw.line((100,200, 150, 300), fill=128, width=3)
mdahlman
  • 9,204
  • 4
  • 44
  • 72
Anurag Uniyal
  • 85,954
  • 40
  • 175
  • 219
  • 1
    great. Might I also ask how can I draw a line with a weight of more than one pixel? – eran Oct 24 '12 at 16:54
  • 1
    @Eran_Id I couldn't find how to set thickness but you can draw thick lines using polygons – Anurag Uniyal Oct 24 '12 at 17:01
  • 2
    It looks like you can draw a thick line using the "width" option. Similar to how the "fill" option is being used here. – golmschenk Oct 21 '13 at 14:48
  • I enter EXACTLY in the format you suggest and the documentation lists, draw.line((0,0,0,2400), fill = grid_val*255, width = 1), and the system throws "SystemError: new style getargs format but argument is not a tuple". It absolutely refuses to plot a line. – Elliot Jan 14 '14 at 17:45
  • 1
    draw.line([(left, top), (left, bottom), (right, bottom), (right, top), (left, top)], width=3,fill=128) can you say me how this draws line! I am confused – SRIDHARAN Dec 06 '17 at 16:10
  • 1
    @Sridharan try it and you will see a line on the image. – Anurag Uniyal Dec 30 '17 at 04:53
  • @Elliot I have the exact same issue. Did you find a fix for this? – Diggy. Apr 10 '20 at 23:51
  • fill=128 didn't work for me. Try fill='black'. – Jose Solorzano Mar 24 '21 at 19:28
  • 1
    ! ! the above does not work, because with "RGBA" channels, an alpha value must be set (for opacity: alpha value 255) . such as : fill=0x990000FF . or just use "RGB" format. – Daniol Dan Nov 29 '21 at 00:55