从I; 16到JPEG的PIL转换产生白色图像 [英] PIL converting from I;16 to JPEG produce white image
问题描述
我在使用PIL将图像I; 16转换为JPEG时遇到问题. 可以在此处(作为泡菜)中找到我的原始图像). 原始图像来自DICOM文件. 这是要尝试的代码:
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()
不幸的是,该图像是完全白色的,而应该是胸部X射线扫描图像.
Unfortunately the image is completely white, while it should be a chest x-ray scan image.
我关注了这个其他stackoverflow问题,以及以下
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()
我能够可视化图像,但不幸的是,my.jpeg
是黑色图像.请帮忙!
I am able to visualise the image, but unfortunately my.jpeg
is a black image. Please help!
推荐答案
您的值是16位的,需要减小到8位才能显示.您可以使用以下方法将它们从当前的2,712(即ims.min()
)范围扩展到4,328(即ims.max()
)范围:
Your values are 16-bit and need to be reduced to 8-bit for display. You can scale them from their current range of 2,712 (i.e. ims.min()
) to 4,328 (i.e. ims.max()
) with the following:
from PIL import Image
import numpy as np
import pickle
# Load image
ims = pickle.load(open("pixel_array.pickle", "rb"))
# Normalise to range 0..255
norm = (ims.astype(np.float)-ims.min())*255.0 / (ims.max()-ims.min())
# Save as 8-bit PNG
Image.fromarray(norm.astype(np.uint8)).save('result.png')
这篇关于从I; 16到JPEG的PIL转换产生白色图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!