Python pillow support many image modes, you can get all image modes in here.
One image contains one mode, this mode can be got and converted to others. In this tutorial, we will introduce you how to get image mode and convert it to others.
How to get image mode by python pillow?
It is very easy to get image mode in python pillow, here is an example:
from PIL import Image im = Image.open("file.png") print(im.mode) #RGB
We can use im.mode to get image mode.
The file.png is:
Run this code, we can find the mode of file.png is RGB.
How to convert image mode?
Different image mode determines the data of each image pixel. We can use im.convert() to convert current mode to others. Here is an example:
modes = ["1",'L','P','RGB','RGBA','CMYK','YCbCr','HSV',"I",'F'] for m in modes: file_name = "mode_"+str(m) imx = im.convert(m) imx.show(file_name)
modes contains some image modes, we will convert RGB mode to others.
Run this code, we will get these results.