I did some image-processing on multi-frame TIFF images from a 12-bit camera and would like to save the output. However, the PIL documentation does not list a 12-bit mode for fromarray()
. How does PIL handle bit depth and how can I ensure that the saved TIFF images will have the same dynamic range as the original ones?
Example code:
import os
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# Read image file names
pathname = '/home/user/images/'
filenameList = [filename for filename in os.listdir(pathname)
if filename.endswith(('.tif', '.TIF', '.tiff', '.TIFF'))]
# Open image files, average over all frames, save averaged image files
for filename in filenameList:
img = Image.open(pathname + filename)
X, Y = img.size
NFrames = img.n_frames
imgArray = np.zeros((Y, X))
for i in range(NFrames):
img.seek(i)
imgArray += np.array(img)
i += 1
imgArrayAverage = imgArray/NFrames
imgAverage = Image.fromarray(imgArrayAverage) # <=== THIS!!!
imgAverage.save(pathname + filename.rsplit('.')[0] + '.tif')
img.close()