Skip to content

Converting P mode images to HSV fails with "ValueError: conversion not supported" in version 6.0.0 and later #3997

@BaconRanch

Description

@BaconRanch

What did you do?

I want to take an RGB PNG as input, quantize it, convert to HSV and call getcolors() on the result to see a listing of (pixel count, (H,S,V)) values. As a bigger-picture idea of what I'm trying to accomplish, the goal is to look for a certain hue and determine if the number of pixels that are "close" to that hue are above a certain threshold. Eg: "Do more than 10% of the pixels in the image have a hue near 250?" The information from getcolors helps quite a bit with answering that question and the quantization helps with the "near 250" part of the question. The code below is what I've been using, and it works fine up through Pillow 5.4.1.

What did you expect to happen?

Pillow 5.4.1 produces this output when calling getcolors():

[(3601, (244, 227, 146)),
 (3605, (155, 255, 128)),
 (21059, (117, 37, 171)),
 (44719, (63, 195, 136)),
 (6406, (26, 137, 250)),
 (4166, (11, 203, 237)),
 (1912, (194, 184, 255)),
 (5664, (159, 220, 255)),
 (411983, (0, 0, 255)),
 (1137, (0, 0, 0))]

What actually happened?

Pillow 6.0.0 and later throws an exception as part of the convert() call:

ValueError                                Traceback (most recent call last)
script.py in <module>()
      1 from PIL import Image
      2 x = Image.open('PNG-Gradient_hex.png').quantize(colors=10,method=1)
----> 3 print x.convert(mode='HSV').getcolors()

PIL/Image.pyc in convert(self, mode, matrix, dither, palette, colors)
   1030                 # normalize source image and try again
   1031                 im = self.im.convert(getmodebase(self.mode))
-> 1032                 im = im.convert(mode, dither)
   1033             except KeyError:
   1034                 raise ValueError("illegal conversion")

ValueError: conversion not supported

What are your OS, Python and Pillow versions?

  • OS: macOS Mojave (10.14.5)
  • Python: 2.7.16
  • Pillow: 6.0.0 and later
from PIL import Image
x = Image.open('PNG-Gradient_hex.png').quantize(colors=10,method=1)
print x.convert(mode='HSV').getcolors()

Sample input image:
PNG-Gradient_hex

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions