Skip to content

Wrong color conversion from HSV string #7225

@matenestor

Description

@matenestor

What did you do?

I tried to create an image with HSV mode and set its color to blue with following kwargs

  • color=(170, 255, 255) # 170 == 255 * (2/3)
  • color="hsv(240, 100%, 100%)"

What did you expect to happen?

To get a blue image with both approaches.

What actually happened?

The color="hsv(240, 100%, 100%)" kwarg created a white image.

What are your OS, Python and Pillow versions?

  • OS: Arch Linux
  • Python: Python 3.10.11
  • Pillow: 9.4.0

Image.new("HSV", (200, 200), (170, 255, 255)).show()

tmp7n00mtvm

Image.new("HSV", (200, 200), color="hsv(240, 100%, 100%)").show()

tmpewrf26xo

I tried converting to RGB first, but that has the same result.

# blue
Image.new("HSV", (200, 200), color=(170, 255, 255)).convert("RGB").save("image.png")

# white
Image.new("HSV", (200, 200), color="hsv(240, 100%, 100%)").convert("RGB").save("image.png")

Something weird is also happening with getpixel() method.

When a tuple is used to set a color it works. The image is still in HSV mode, so 170 is 240 hue, and 255 is 100% saturation and value.
The pixel bytes have expected values.

Image.new("HSV", (200, 200), (170, 255, 255)).getpixel((0,0))
Out[173]: (170, 255, 255)

However, when a string is used, then the pixel bytes are totally off. It looks like it is returning RGB mode, since this is blue color in RGB code.

Image.new("HSV", (200, 200), color="hsv(240, 100%, 100%)").getpixel((0,0))
Out[174]: (0, 0, 255)

So I tried using RGB mode for the image and that works as expected.

Image.new("RGB", (200, 200), (0, 0, 255)).getpixel((0,0))
Out[177]: (0, 0, 255)

Image.new("RGB", (200, 200), color="hsv(240, 100%, 100%)").getpixel((0,0))
Out[178]: (0, 0, 255)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions