Home > OS >  Saving grayscale image to a directory in python
Saving grayscale image to a directory in python

Time:03-09

I have a piece of code that takes in image data as grayscale values, and then converts into an image using matplotlib below

import matplotlib.pyplot as plt
import numpy

image_data = image_result.GetNDArray()
numpy.savetxt('data.cvs', image_data)

# Draws an image on the current figure
image = plt.imshow(image_data, cmap='gray')

I want to be able to export this data to LabView as a .png file. So I need to save these image to a folder where LabView and display them. Is there a function with pillow or os that can do this?

CodePudding user response:

plt.imsave('output.png', image)

Does this work?

CodePudding user response:

If image_data is a Numpy array of shape height x width with dtype=np.uint8 or dtype=np.uint16, you can make a PIL Image and save it as a PNG like this:

from PIL import Image

# Make PIL Image from Numpy array
pImage = Image.fromarray(image_data)
pImage.save('forLabView.png')

Check what your array is with:

print(image_data.shape, image_data.dtype)
  • Related