使用 Python 读取大型 16 位灰度 PNG 的大问题

Big issue reading a large 16bit grayscale PNG using Python

我在尝试将大型科学图像 (7 Mb) 16 位 PNG 图像转换为 JPG 以便压缩它并检查 Python 中的任何最终伪影时遇到了一个大问题。 原始图像可以在以下位置找到: https://postimg.cc/p5PQG8ry 在这里阅读其他答案我尝试过 Pillow 和 OpenCV 但没有成功,我唯一获得的是白色 sheet。我做错了什么? 注释行是 的尝试,但似乎对我无效,生成数据类型错误。

import numpy as np
from PIL import Image 
import cv2
image = cv2.imread('terzafoto.png', -cv2.IMREAD_ANYDEPTH)
cv2.imwrite('terza.jpg', image)
im = Image.open('terzafoto.png').convert('RGB')
im.save('terzafoto.jpg', format='JPEG', quality=100)
#im = Image.fromarray(np.array(Image.open('terzafoto.jpg')).astype("uint16")).convert('RGB')

多亏了 Dan Masek,我才能够在我的代码中找到错误。 我没有正确地将数据从 16 位转换为 8 位。 这里更新的代码包含 OpenCV 和 Pillow 的解决方案。

import numpy as np
from PIL import Image
import cv2
im = Image.fromarray((np.array(Image.open('terzafoto.png'))//256).astype("uint8")).convert('RGB')
im.save('PIL100.jpg', format='JPEG', quality=100)
img = cv2.imread('terzafoto.png', cv2.IMREAD_ANYDEPTH)
cv2.imwrite('100.jpeg', np.uint8(img // 256),[int(cv2.IMWRITE_JPEG_QUALITY), 100])

图像质量因子可以根据您的需要进行设置。 100表示​​无损压缩。