Welcome folks today in this blog post we will be using the pillow
library in python to generate the image
of any color
and size.
All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code and also install the below library using the pip
command as shown below
pip install pillow
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from PIL import Image # Create a new image with a white background image = Image.new('RGB', (1280, 720), 'red') # Save the image to a file image.save('image.jpg') # Send the content-type header print("Content-Type: image/jpeg") # Send the image file with open('image.jpg', 'rb') as f: print(f.read()) |
As you can see we are importing the pillow
library and then we are generating the image with a solid
background color of red
we are providing the rgb
color. And also we are providing the custom width
and height
. And then we are saving the generated
image in the root directory by setting the headers
and then we are opening the image using the open()
method. And inside it we are passing the path
of the image and we are opening the image in read binary
mode.