×

Search anything:

Python script to make images black and white

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

In this article at OpenGenus, we learn how to convert Images to black and white using python script using Pillow and OpenCV.

TABLE OF CONTENT
a. Introduction
b. Steps In coverting the image in pillow
c. Converting using OpenCV
d. Conclusion

INTRODUCTION

Python is a widely used computer language that is used in many industries, including the processing of images. Converting a picture to grayscale, often known as black and white conversion, is a typical image processing activity. Fortunately, the Pillow package in Python offers a simple method for accomplishing this.
A Python imaging library called Pillow provides a number of image processing options, including opening, storing, resizing, cropping, and converting pictures. It supports a number of image formats, including JPEG, PNG, BMP, TIFF, GIF, and WebP. Pillow is a fork of the defunct Python Imaging Library (PIL). The Python package installer, pip, may be used to set up Pillow.

OpenCV is a popular computer vision library that provides a wide range of image and video processing capabilities. Real-time picture and video manipulation enables programmers to carry out tasks like object identification, facial recognition, image segmentation, and more. OpenCV is a dynamic tool for developers since it supports a number of computer languages, including Python, C++, Java, and MATLAB. JPEG, PNG, BMP, TIFF, GIF, and a host of other image and video formats are among the numerous formats that OpenCV supports. Pip, the Python package installer, may be used by developers to install OpenCV.

Steps

In order to use Pillow to convert a picture to black and white,

  1. Use the 'Image.open()'* function to open the image file, then assign it to a variable as shown below:
from PIL  import Image 
img = image.open('path/to/image.jpg') 
'''Open the image file'''

The 'Image' class is imported from the 'PIL' library in this code snippet, and the image file is opened using the 'open()' function of the 'Image' class. The 'open()' function receives the path to the image file as an argument. The image object returned by the 'open()' function is kept in the 'img' variable.

  1. Use the 'convert()' function of the 'picture' class to convert the picture to grayscale. The image will become grayscale when the parameter ''L'', which stands for Luminance, is used. As seen below, assign the transformed picture to the original variable.
'''python Grayscale the image'''
    img = img.convert('L')

The 'convert()' function on the 'img' variable is called in this snippet of code, and the ''L'' parameter is supplied to the method to convert the picture to grayscale. The grayscale version of the image is then given the 'img' variable again.

  1. Use the'save()' function of the 'picture' class to save the transformed picture. The first option is the image's filename and directory, as seen below:
img.save('path/to/black-and-whiteimage.jpg') 
''' Save the converted image'''

The path to the filename is supplied as an argument to the'save()' function in this code snippet when it is called on the 'image' variable. The grayscale image is saved as a new image file using the'save()' function.

Below is an overview of the entire Pillow script used to convert a picture to black and white:

  from PIL import Image
  Img = Image.open('path/to/image.jpg')
  Img = img.convert("L")
  Img.save("path/to/black-and-white_image.jpg")

There are a few methods to utilize Python and OpenCV to convert a picture to black and white, but one of the easiest is to write a short Python script.

Installing OpenCV first requires using pip, the Python package manager. Performing the following command on a terminal or command prompt will do this:

  pip install opencv-python

After installing OpenCV, you may make a Python script and import the required libraries. Here is an illustration of a script that will change a picture to black and white:

 import cv2

# Load the image
img = cv2.imread('input_image.jpg')

# Convert the image to grayscale
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Save the grayscale image
cv2.imwrite('output_image.jpg', gray_img)

The input picture is first loaded into the script using the cv2.imread() method, then it is saved in the img variable. The cv2.cvtColor() method, which accepts the input picture and a color conversion code as arguments, is then used to convert the image to grayscale. In this instance, we're converting from grayscale to BGR, OpenCV's default color space.

The cv2.imwrite() method, which accepts the output filename and the image data as arguments, is then used to save the grayscale picture.

That's all there is to it! OpenCV allows you to quickly convert any image to black and white with only a few lines of Python code.

Conclusion

Certainly! Powerful image processing libraries for Python include Pillow and OpenCV. Although they offer comparable features, they execute some image processing jobs differently.

One frequent image processing activity that can be readily completed with both libraries is the conversion of a picture to grayscale. An image may be converted to grayscale using the 'convert()' function from Pillow or 'cvtColor()' from OpenCV.

'convert()' in Pillow has a variety of conversion choices, including RGB, RGBA, CMYK, LAB, HSV, and YCbCr, each of which represents a distinct color space and number of channels. The 'cvtColor()' function of OpenCV, on the other hand, provides two grayscale conversion codes: 'cv2.COLOR_BGR2GRAY' for BGR pictures and 'cv2.COLOR_RGB2GRAY' for RGB images.

An picture may be utilized for a variety of image processing tasks after it has been converted to grayscale, including edge detection, segmentation, and object recognition.

In general, Pillow and OpenCV are both flexible libraries for Python image processing, and which one to choose will frequently rely on the task's unique needs.

Python script to make images black and white
Share this