Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Python 3 Pillow Script to Generate Image of Any Color & Size in Command Line

Posted on February 11, 2023

 

 

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

 

 

Python
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.

 

 

 

Recent Posts

  • Node.js OfficeGen Example to Add Text & Images in Powerpoint Presentation in Javascript
  • Node.js OfficeGen Example to Generate Excel Files By Adding Data inside Cells & Sheets in Javascript
  • Node.js OfficeGen Example to Create Word Docx Files & Add Text Images inside it Using Javascript
  • React.js Twitter API Tutorial to Embed Profile & Timeline, Hashtags of User in Browser Using Javascript
  • Android Java Tutorial to Change Styles & Visibility of System Bars (Top, Action & Status) Full Example
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme