Python Pillow Get and Convert Image Mode: A Beginner Guide – Python Pillow Tutorial

By | August 30, 2020

Python pillow support many image modes, you can get all image modes in here.

Understand Python Pillow Image Mode: A Completed Guide – Python Pillow Tutorial

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:

png rgb mode image

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.

Mode Image
1 python pillow image mode 1 example
L python pillow image mode L example
P python pillow image mode P example
RGB png rgb mode image
RGBA python pillow image mode RGBA example
CMYK python pillow image mode CMYK example
YCbCr python pillow image mode YCbCr example
HSV python pillow image mode HSV example
I python pillow image mode I example
F python pillow image mode F example

Leave a Reply