Python image convert l
The above is how to use python package pillow to convert the image background into transparent. Of course, if the background is all white, there is no obvious difference, hahaha. The background of this one is already transparent. JPG format does not seem to support transparent backgrounds. image.save('output.png')įinally, remember to save the picture in PNG format. Of course, you can set what color you want to convert here. Then we first take out the pixel value of this picture and determine whether the pixel is white (255, 255, 255): # TransparencyĪs you can see, I replaced all dots with pixel values (255, 255, 255) with transparent. This is why we need to convert to this format. RGBA stands for Red, Green, Blue and Alpha channel.Īlpha channel stands for transparency. And on the bottom row, R0, G0, B255, so the Luminance has become: L 0 + 0 + 255 114/1000 L 29. To make the image background transparent, we need to change “ RGB” to “ RGBA” first. Well, the formula for converting RGB to L is: L R 299/1000 + G 587/1000 + B 114/1000.
We can see that this is a file of data type, and the mode of this file is “RGB”. If it is the first time to use Pillow, use the following command to install it: pip3 install pillowĪfter the installation is over, let’s look at an example: # -*- coding: utf-8 -*. So in below, we use the Pillow package to perform conversion processing. 1 2 3 4 5 6 from PIL import Image img Image.open('C://Users/Shado/Desktop/kitten.jpg') newimg img.convert ('L') newimg.
Passing ‘L’ into the pillow convert function converts the image from it’s regular RGB colors to simple black and white (gray-scale). If you want to know more how to use Pillow module, you can refer to here: Ĭonvert the background of the picture to transparentĪs we can see, most of the background color is white color ( rgb(255, 255, 255)). The below use is the most simple and common one that we could find. If we want to convert a picture background into transparent, we can use the Pillow package in Python to do it.