Reputation: 61
I have a .txt file that has pixel coordinates and pixel values. I tried to visualize it in png and jpeg formats which are compressed 8-bit, but I wanted it to be in 16-bit image format like a tiff. I have tried saving my image in tiff format but it is not the image I expected it has a lot of noise and I cannot even see the image when I open the file when compared to png or jpeg.
Here is my code
import numpy as np
from PIL import Image
import pandas as pd
from matplotlib import pyplot as plt
file = open('filename')
data = pd.read_table(file, header=None, skiprows=8, decimal=",")
data = data.iloc[:, :]
rows, cols = data.shape
na = np.array(data)
plt.imshow(na)
plt.imsave('mes.png',an)
na.save('myimg.tif')
What am I doing wrong while implementing this, Any suggestions or changes? Any help is appreciated, Thank you for your time.
Upvotes: 1
Views: 1273
Reputation: 207465
The issue is that you have not normalised your data to the correct range of 0..65535 for an unsigned 16-bit TIFF or PNG file.
I am using the OpenCV function here, but you can use Numpy or straight Python to get the same result. So, after converting to a Numpy array
... your code ...
na = np.array(data)
import cv2
# Normalise (scale) to range 0..65535
sc = cv2.normalize(na, None, alpha=0, beta=65535, norm_type = cv2.NORM_MINMAX, dtype = cv2.CV_16U)
# Save, using any ONE of the following methods
Image.fromarray(sc).save('result.tif')
Image.fromarray(sc).save('result.png')
cv2.imwrite('result.tif', sc)
cv2.imwrite('result.png', sc)
Upvotes: 1