![]() 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. import numpy as np from PIL import Image array np.random.randint(255, size(400, 400),dtypenp.uint8) image omarray(array) image. ![]() image.save('output.png')įinally, remember to save the picture in PNG format. We use the omarray () function to convert the array back to the PIL image object and finally display the image object using the show () method. 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. To make the image background transparent, we need to change “ RGB” to “ RGBA” first. We can see that this is a file of data type, and the mode of this file is “RGB”. If you use L mode, then greyscale.png will be an RGB image (with no alpha). The documentation states When translating a color image to black and white (mode L ), the library uses the ITU-R 601-2 luma transform: L R 299/1000 + G 587/1000 + B 114/1000 where the RGB maps to Red, Green and Blue. from PIL import Image import numpy as np im Image.open('hopper.jpg') a np.asarray(im) Then this can be used to convert it to a Pillow image: im omarray(a) Parameters: obj Object with array interface mode Optional mode to use when reading obj. If you use LA mode, then greyscale.png will be an RGBA image with the alpha channel of image.png preserved. The mode we care about at the moment is ‘L’. 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 -*- img Image.open('image.png').convert('LA') img.save('greyscale.png') LA mode has luminosity (brightness) and alpha. So in below, we use the Pillow package to perform conversion processing. 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)). to add an alpha layer to an image, you can > use a direct convert: > imOut im.convert('RGBA') sets alpha to 255 > if you have an RGB image, you can also modify it in place > im.putalpha(value) set alpha to given value > or > im.putalpha(matte) where matte is a mode 'L' image > Thanks Fredrik.ImageWithColorPalette = nvert("P", palette=Image.If we want to convert a picture background into transparent, we can use the Pillow package in Python to do it. # Convert using adaptive palette of color depth 8 ![]() # convert the color image to black and white imageīlackAndWhiteImage = nvert("1") # Convert the color image to grey scale image For example, ff0000 specifies pure red, and ff0000cc specifies red with 80 opacity ( cc is 204 in decimal form, and 204 / 255. Using these transforms we can convert a PIL image or a numpy.ndarray. The ImageColor module supports the following string formats: Hexadecimal color specifiers, given as rgb, rgba, rrggbb or rrggbbaa, where r is red, g is green, b is blue and a is alpha (also called ‘opacity’). These transforms are provided in the ansforms package.
0 Comments
Leave a Reply. |