Table of Contents

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'

Image Mode

https://pillow.readthedocs.io/en/stable/handbook/concepts.html

img.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

Pillow
https://pypi.org/project/pillow/
https://pillow.readthedocs.io/

https://qiita.com/pashango2/items/145d858eff3c505c100a