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

asheets
  • 770
  • 1
  • 8
  • 28
  • Possible duplicate of [Convert an image RGB->Lab with python](https://stackoverflow.com/questions/13405956/convert-an-image-rgb-lab-with-python) – user1767754 Nov 29 '17 at 02:30
  • You are doing some weird stuff like multiplying by 255 and then overwriting it in the next line – user1767754 Nov 29 '17 at 07:27

1 Answers1

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()
Stefan van der Walt
  • 7,165
  • 1
  • 32
  • 41