Saving 16-bit NumPy Arrays as Images with PIL
Here's a code example demonstrating how to save a 16-bit NumPy array as an image using PIL:
import numpy as np
from PIL import Image
# Create a 16-bit NumPy array
arr = np.array([[0, 65535], [32768, 16384]], dtype=np.uint16)
# Convert the NumPy array to an image
im = Image.fromarray(arr)
# Save the image to a file
im.save('16bit_image.png')
In this example, we begin by creating a 2x2 NumPy array with values ranging from 0 to 65535. We then transform this NumPy array into a PIL Image object using the 'fromarray' method. Finally, we save the image to a file using the 'save' method, specifying PNG as the file format.
Remember that when saving a 16-bit image, the chosen file format must support 16-bit depth. PNG is one such format capable of handling 16-bit depth.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/mWZ2 著作权归作者所有。请勿转载和采集!