I am having a problem converting an image I;16 to JPEG with PIL. My original image can be found here (as pickle). The original image comes from a DICOM file. Here is the code to try:
import pickle
import matplotlib.pyplot as plt
from PIL import Image
ims = pickle.load(open("pixel_array.pickle", "rb"))
img = Image.fromarray(ims)
print(img.mode)
rgb_im = img.convert("RGB")
print(rgb_im.mode)
fig, ax = plt.subplots(figsize=(20, 10))
ax.imshow(rgb_im, cmap=plt.cm.bone)
fig.show()
Unfortunately the image is completely white, while it should be a chest x-ray scan image.
I followed this other stackoverflow question, and with the following
ims = pickle.load(open("pixel_array.pickle", "rb"))
img = Image.fromarray(ims)
print(img.mode)
img.mode = 'I'
rgb_im = img.point(lambda i:i*(1./256)).convert('L')
rgb_im.save('my.jpeg')
fig, ax = plt.subplots(figsize=(20, 10))
ax.imshow(rgb_im, cmap=plt.cm.bone)
fig.show()
I am able to visualise the image, but unfortunately my.jpeg
is a black image. Please help!