This article mainly introduces Python by PiL to the PNG image to fill the background color of the method, example analysis Python use PIL module operation PNG image technique, very practical value, need friends can refer to the
This example describes how Python fills the background color of a PNG picture with PiL. Share to everyone for your reference. The specific analysis is as follows:
Some PNG pictures have no background color, and if you want to fill the background with monochrome (such as white), you can use the following code, which fills the jb51.net.png picture in the current directory with a white background.
Use the background color of the specified color, and then fill the picture with an alpha channel to the monochrome background.
For example, use the following white background:
?
1 2 3 4 5 6 7 8 9 10 |
im = Image.open (' jb51.net.png ') X,y = im.size Try: # Use White to fill the background from:www.jb51.net # (Alpha band as Paste mask). p = image.new (' RGBA ', Im.size, (255,255,255)) P.paste (IM, (0, 0, x, y), IM) p.save (' Jb51.net.png ') except:pass |
I hope this article will help you with your Python programming.