How to Make Black and White Image Converter Using Python

Introduction :

Hello friends! In today’s post, I am going to tell you how you can convert any color image into Black and White Image with the help of Python Programming Language, that too in just 4 lines of Python code, so if you If you also want to know about it, then read this post till the end and read it carefully so that you understand everything correctly.

Installing Library:

Friends, to do this project, first of all we will need a library named Pillow, so first of all you have to install this library in your System (Computer), you can use pip command to install it.

pip install pillow

Code for Black and White Image Converter Using Python:

from PIL import Image

img=Image.open("image1.png")

BlackAndWhite=img.convert("L")

BlackAndWhite.save("result_image.png")

Explanation :

1. First of all, we have imported a function named Image from the Pillow library so that we can open any image in our program.

2. And after that we have created a variable with the name img and in it with the help of Image.open (function) we have opened an image here which we want to convert into Black and White image and the name of this image is image1. .png

image1

3. After that we have created a variable named BlackandWhite and inside it we have written img.convert because we are converting an image into Black and White image. And inside this we have given an argument named “L” so that it can convert into black and white image.

4. After that we have to save this image, for this we have written BlackandWhite.save because our Black and White image is stored in the variable BlackandWhite, so we have written BlackandWhite.save and inside it we have given the name of that image. The name with which we want to save this Black and White image is, so we have named this image result_image.png and it is very important to add .png extension here.

5. And after that you have to run this program and after that you will get an image with the name with which you have saved the image.

Output :

result_image.png

Summary :

So friends, in this way, with the help of Python Programming Language, you can convert any Color Image into Black and White Image, that too very easily! So if you like this project then please tell us by commenting and if you want to know about more similar projects then follow us and subscribe to our YouTube Channel CodeWithShani.

Leave a Reply

Your email address will not be published. Required fields are marked *