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

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

Closed
BaconRanch opened this issue Jul 30, 2019 · 6 comments · Fixed by #4004
Projects

Comments

@BaconRanch
Copy link

BaconRanch commented Jul 30, 2019

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

@hugovk
Copy link
Member

hugovk commented Jul 31, 2019

git bisect points to f61828a from PR #3726:

bisect.sh:

#!/usr/bin/env bash
python3 setup.py clean
rm -f "PIL/*.so"
pip3 uninstall pillow -y
python3 setup.py install
python3 3997.py

3997.py:

from PIL import Image
x = Image.open("3997.png").quantize(colors=10, method=1)
print(x.convert(mode="HSV").getcolors())
$ python --version
Python 3.7.4
$ git bisect start
$ git bisect good 5.4.1
$ git bisect bad 6.0.0
$ git bisect run ./bisect.sh
...
f61828acdcc846cbf29ff991425eb40d966d5b03 is the first bad commit
commit f61828acdcc846cbf29ff991425eb40d966d5b03
Date:   Tue Mar 19 11:13:58 2019 +1100

    Promote P images to PA in putalpha

:040000 040000 fbea0af0f1959311d72dd269fd70607cf255e84a 9aa294c996483580d0752a74c7d9d27a7ecfe487 M	Tests
:040000 040000 d2b9276a992da843d799dc94a578981e5eed635f 2759727adb9e49468f91217bfc19bb266cf8d6ae M	src
bisect run success

@radarhere
Copy link
Member

If you're interested in an immediate workaround, I would suggest converting to RGB first. Quickly looking at the situation, that is what Pillow would have been doing internally in 5.4.1.

from PIL import Image
x = Image.open('PNG-Gradient_hex.png').quantize(colors=10,method=1)
print x.convert('RGB').convert('HSV').getcolors()

@BaconRanch
Copy link
Author

Thank you! Yes, that fixes my problem.
While I'm personally happy with this solution, I understand this is still an issue since the calling code has to be explicit about converting to RGB first, so I'll keep the ticket open and leave it to you all to decide what to do with it.

@radarhere radarhere added this to In progress in Pillow Aug 2, 2019
@radarhere
Copy link
Member

I've created PR #4004 to resolve this.

@radarhere
Copy link
Member

Also, while this has no bearing on the problem that you've reported, since you are using Python 2.7, you should be aware that Pillow is a project that has committed to dropping support for Python 2.7 in 2020 - https://python3statement.org/.

@radarhere radarhere moved this from In progress to Review/QA in Pillow Aug 2, 2019
@BaconRanch
Copy link
Author

I'm in the slow process of converting codebases at work over from 2.7 to 3 now. I think about the end of 2019 bearing down as a deadline all the time. Thank you for supporting 2.7 through the end of 2019 and not leaving earlier! I need every day I can get!

Pillow automation moved this from Review/QA to Closed Sep 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Pillow
  
Closed
Development

Successfully merging a pull request may close this issue.

3 participants