njsmith / colorspacious

A powerful, accurate, and easy-to-use Python library for doing colorspace conversions
MIT License
169 stars 16 forks source link

Issue with conversion from sRGB255 to JCh and back #2

Closed mycarta closed 8 years ago

mycarta commented 8 years ago

I get some odd result when converting a png image of some data with Jet colormap: in

from sRGB255 to JCh and back: srgb255-jch-srgb255

The result looks instead normal when converting the input to 0-1 range and converting from sRGB1 to JCh and back: srgb1-jch-srgb1

I've loaded a complete example at this GitHub location: https://github.com/mycarta/PerceptualColormaps/blob/master/test_color_conversions_rgb-2-JCh-2-rgb_and_rgb-2-LCh-2-rgb.ipynb

njsmith commented 8 years ago

I think the problem is that matplotlib expects float arrays to be [0, 1] and integer arrays to be [0, 255], and converting to and from JCh gives you a float array on [0, 255]. Try doing np.round(rgb_J).astype(int) and see if that helps? or just sticking with sRGB1 should also work :-)

njsmith commented 8 years ago

(Note that you can reproduce the problem by just doing img = img.astype(float); plt.imshow(img) without using colorspacious at all.)

mycarta commented 8 years ago

Thanks for the tips. Unfortunately using rgb_JJ = np.around(rgb_J).astype(int) did not change the outcome. And similarly, when I tried img = img.astype(float); plt.imshow(img) (which as you suggests, reproduces the problem), followed by img = np.round(img).astype(int); plt.imshow(img) I also do not get back my original image.

I think I will stick with sRGB1. };-)

njsmith commented 8 years ago

Maybe it's .astype(np.uint8) that matplotlib is looking for? In any case, sounds like you've found a solution and there's nothing for colorspacious to do :-)