I am taking an array from a camera, thresholding the values and want to save the new image. I am using PIL.Image.fromarry()
to turn an array into an image but I am only seeing greyed out images. The image is there because when I import into another program I can see what I want. When I show the image with plt.show(data)
that is how I would like to see the image. How can I change the color values?
from flirpy.camera.lepton import Lepton
import matplotlib.pyplot as plt
import numpy as np
import cv2
from PIL import Image as im
camera = Lepton()
for i in range(5):
image = camera.grab()
cimage = np.around(image/100 - 273.15, 2)
cimage = np.where(cimage < 20, 20, cimage)
cimage = np.where(cimage > 30, 30, cimage)
data = im.fromarray(cimage.astype(np.uint8),'L')
data.save(f'test_{i:04}.png')
plt.imshow(data)
plt.show()
camera.close()
Matplotlib:
PIL:
Matplotlib applies a colormap to your image and, under certain circumstances also scales the values to the full colour range. PIL doesn't do that, so if you give it a greyscale image (single channel) with all the values in the range 20..30, it will come out black, because it really expects values in the range 0..255 and it doesn't colorise the grey for you either.
Here's how you can convert a Matplotlib
cmap
into a palette and push it into aPIL Image
:That gives this as the input greyscale image:
and this as the result with the
viridis
andinferno
colormaps:Note also that you don't need to make 2 passes over your image to clip it to the range 20..30 like this:
You should be able to use
np.clip()
:If we apply the code above to your image: