1

I'm using sci-kits module skimage to convert an image from RGB colorspace to LAB and back again. I found the following functions from this question: Convert an image RGB->Lab with python, but this does not address how an image could be reduced to static.

Code:

file = 'C://Users/Alec/Pictures/25 zone test.png'
pix = numpy.array(PIL.Image.open(file))
print(pix[0,0])
pix = color.rgb2lab(pix)
print(pix[0,0])
pix = color.lab2rgb(pix)
print(pix[0,0])
pix *= 255
print(pix[0,0])
pix = pix.astype(int)
print(pix[0,0])
pic = PIL.Image.fromarray(pix, 'RGB')
pic.show()

Output:

[255 255 255]
[  1.00000000e+02  -2.45493786e-03   4.65342115e-03]
[ 1.  1.  1.]
[ 255.  255.  255.]
[255 254 254]

The output of the print statements seems more or less appropriate, however, the resulting image certainly is not.

Is there a step that I'm missing for this to work?

Original Image:

Original Image

Result:

enter image description here

2

1 Answer 1

2

This round-trip conversion gives me back the original image (or something very close):

from skimage import io, color
import matplotlib.pyplot as plt

image = io.imread('/tmp/colors.png')
lab = color.rgb2lab(image)
rgb = color.lab2rgb(lab)

plt.imshow(rgb)
plt.show()
3
  • There must be an issue with the way PIL displays the image. Using pyplot worded!
    – asheets
    Nov 29, 2017 at 15:17
  • But in your code this assertion would fail: assert (image == rgb).all().
    – Gill Bates
    Jan 6, 2020 at 18:21
  • Yes, the ranges are different. The following would succeed: np.allclose(util.img_as_float(image), util.img_as_float(rgb)) Jan 13, 2020 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.