Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message when drawing a rectangle on a grayscale image #5523

Closed
ivan-marroquin opened this issue Jun 6, 2021 · 4 comments
Closed

Error message when drawing a rectangle on a grayscale image #5523

ivan-marroquin opened this issue Jun 6, 2021 · 4 comments

Comments

@ivan-marroquin
Copy link

ivan-marroquin commented Jun 6, 2021

What did you do?

I would like to draw a rectangle on a grayscale image. thanks for your help

What did you expect to happen?

Drawing a rectangle on a RGB image works as expected but it fails when using a grayscale image

What actually happened?

I get the following error message:

PIL (255, 255, 0) <class 'tuple'> L <class 'str'>

Traceback (most recent call last):

  File "<ipython-input-12-e10de1d28bc4>", line 1, in <module>
    draw.rectangle((0,20,0,30), fill=None, outline=(255,255,0))

  File "/home/ivan/python_3.6.5/lib/python3.6/site-packages/PIL/ImageDraw.py", line 192, in rectangle
    ink, fill = self._getink(outline, fill)

  File "/home/ivan/python_3.6.5/lib/python3.6/site-packages/PIL/ImageDraw.py", line 113, in _getink
    ink = self.draw.draw_ink(ink, self.mode)

TypeError: function takes exactly 1 argument (3 given)

What are your OS, Python and Pillow versions?

  • OS: Linux/Windows
  • Python: 3.6.5
  • Pillow: 5.2.0
from PIL import Image, ImageDraw, ImageOps

newimage= Image.new('RGB', (680, 800), color= (255,0,255))
draw= ImageDraw.Draw(newimage)
draw.rectangle((0,20,0,30), fill=None, outline=(255,255,0)) # works as expected

grayimage= ImageOps.grayscale(newimage)
draw= ImageDraw.Draw(grayimage) 
draw.rectangle((0,20,0,30), fill=None, outline=(255,255,0)) # produces error
@radarhere
Copy link
Member

Hi. If an image is in mode L, then it doesn't have red, green and blue colors. Instead, it has just a single grayscale color. So instead of trying to draw (255, 255, 0) on your L image, you could either

  • use a grayscale color - draw.rectangle((0,20,0,30), fill=None, outline=255)
  • if you want to draw yellow, convert the image to RGB first and then try again - grayimage = grayimage.convert("RGB")

#5504 will be a part of the next Pillow release, changing the error message from "TypeError: function takes exactly 1 argument (3 given)" to a clearer "color must be int or single-element tuple".

@ivan-marroquin
Copy link
Author

Thanks a lot for the clarification

@radarhere
Copy link
Member

Shall I close this issue then?

@ivan-marroquin
Copy link
Author

Thanks for all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants