Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Convert an Image to Differnt Modes Using Pillow in Python

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

import numpy as np
from PIL import Image, ImageOps
img = Image.open("../../home/media/poker/4h.png")
img
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=37x54 at 0x11DB473D0>

Comments

  1. You can use the method Image.convert to convert a PIL.Image to different modes.

  2. ImageOps.grayscale(img) is equivalent to img.convert("L"). As a matter of fact, ImageOps.grayscale(img) directly calls img.convert("L") according to the implementation.

Gray Scale - “L” Mode

ImageOps.grayscale(img)
<PIL.Image.Image image mode=L size=37x54 at 0x7F4515F85160>
img.convert("L")
<PIL.Image.Image image mode=L size=37x54 at 0x7F4515F85748>

Black-and-White - “1” Mode

By default, dithering is applied (white noisies are added).

img.convert("1")
<PIL.Image.Image image mode=1 size=37x54 at 0x7F27CE694908>

You can disable dithering with the option dither=False.

img_bw = img.convert("1", dither=False)
img_bw
<PIL.Image.Image image mode=1 size=37x54 at 0x7F4515F85E80>
img_bw.convert("1", dither=False)
<PIL.Image.Image image mode=1 size=37x54 at 0x7F458C8BF080>

When a black-and-white image is converted to a numpy array, a 2-dimensional array (instead of 3-dimensional) is returned.

np.array(img_bw)
array([[False, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], [ True, True, True, ..., True, True, True], ..., [False, True, True, ..., True, True, True], [False, False, False, ..., False, False, False], [False, False, False, ..., False, False, False]])

You can also use Pillow to convert an image to other (colorfull) modes. For more details, please refer to Modes of the Method Image.convert .

!wget https://i.imgur.com/mB96s.png
--2020-02-16 11:00:53--  https://i.imgur.com/mB96s.png
Resolving i.imgur.com (i.imgur.com)... 127.0.0.1
Connecting to i.imgur.com (i.imgur.com)|127.0.0.1|:443... failed: Connection refused.
Image.open("mB96s.png")
<PIL.PngImagePlugin.PngImageFile image mode=RGB size=518x604 at 0x7F4515F7A160>
Image.open("mB96s.png").convert("L")
<PIL.Image.Image image mode=L size=518x604 at 0x7F4517776EF0>
def binarize(arr2d, threshold=200):
    arr2d = arr2d.copy()
    nrow, ncol = arr2d.shape
    for i in range(nrow):
        for j in range(ncol):
            if arr2d[i, j] > threshold:
                arr2d[i, j] = 255
            else:
                arr2d[i, j] = 0
    return arr2d
img_gs = Image.open("mB96s.png").convert("L")
Image.fromarray(binarize(np.array(img_gs)))
<PIL.Image.Image image mode=L size=518x604 at 0x7F458C8BF400>
Image.open("mB96s.png").convert("1", dither=False)
<PIL.Image.Image image mode=1 size=518x604 at 0x7F4515F59978>
Image.open("mB96s.png").convert("L").convert("1", dither=False)
<PIL.Image.Image image mode=1 size=518x604 at 0x7F458C8BF6A0>

Convert a RGBA Image to RGB

img_rgba = img.convert("RGBA")
img_rgba.mode
'RGBA'
img_rgb = img_rgba.convert("RGB")
img_rgb.mode
'RGB'