Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create an empty n*m PNG file in Python?

I would like to combine 4 PNG images to one PNG file. I know who to combine them with Image.paste method, but I couldn't create an save output file! Actually, I want to have a n*m empty PNG file, and use to combine my images. I need to specify the file size, if not I couldn't use paste method.

like image 871
Amir Avatar asked Oct 06 '12 13:10

Amir


People also ask

Does Python support PNG?

INSTALLATION. PyPNG is pure Python and has no dependencies. It requires Python 3.5 or any compatible higher version. to access the png module in your Python program.


2 Answers

from PIL import Image image = Image.new('RGB', (n, m)) 
like image 142
John La Rooy Avatar answered Oct 13 '22 23:10

John La Rooy


You can use the method PIL.Image.new() to create the image. But the default color is in black. To make a totally white-background empty image, you can initialize it with the code:

from PIL import Image img = Image.new("RGB", (800, 1280), (255, 255, 255)) img.save("image.png", "PNG") 

It creates an image with the size 800x1280 with white background.

like image 25
ccy Avatar answered Oct 13 '22 21:10

ccy