PIL/Pillow
Install
pip install pillow
python from PIL import Image
Sample
Loading Image
img = Image.open("input.tif") img_array = np.array(img)
Troubleshooting - Decompression Bomb Error
https://pillow.readthedocs.io/en/stable/reference/Image.html
https://stackoverflow.com/questions/56174099/how-to-load-images-larger-than-max-image-pixels-with-pil
## PIL.Image.DecompressionBombError: Image size (400000000 pixels) exceeds limit of 178956970 pixels, could be decompression bomb DOS attack. Image.MAX_IMAGE_PIXELS = None
Troubleshooting - Unidentified Image Error
## Traceback (most recent call last): ## File "<stdin>", line 1, in <module> ## File "PIL/Image.py", line 3280, in open ## raise UnidentifiedImageError(msg) ## PIL.UnidentifiedImageError: cannot identify image file 'input.tif'
- Check if the input image is not corrupted at first
- Consider using Tifffile if the format is unsupported by PIL/Pillow (e.g., 32-bit TIFF with alpha channel)
Image Mode
Extracting Alpha Channel from 2-Channel LA/PA or 4-Channel RGBA Image
img = Image.open("input.tif")
img.mode ## LA, PA, or RGBA
img_alpha = img.getchannel("A") img_alpha_array = np.array(img_alpha)
## Binarization (as needed) img_alpha_array = np.where(img_alpha_array == 0, 1, 0).astype(np.uint8) #img_alpha_array = np.where(img_alpha_array == 0, 255, 0).astype(np.uint8)
img_out = Image.fromarray(img_alpha_array, mode="L") img_out.save("output.tif")
References
Acknowledgments
Daiphys is a professional services company in research and development of leading-edge technologies in science and engineering.
Get started accelerating your business through our deep expertise in R&D with AI, quantum computing, and space development; please get in touch with Daiphys today!
Daiphys Technologies LLC - https://www.daiphys.com/