Creating Image using Python

Adding text and background color for multiple images!

Photo by Glenn Carstens-Peters on Unsplash (left) | Photo by Chris Ried on Unsplash (right)

Trying to find ways to create multiple images with the same context and process, I ended up using Python to create my own batch of images as per my requirement. Here I have created multiple images with dimensions of Instagram Story (1080 x 1920) with the total number of images and text for each image drawn from user input.

Prerequisites: I have used ‘CenturyGothic’ true type font, however, any .ttf file can be used for the project.

  1. Import Essential libraries
pip install Pillow
import PIL
from PIL import Image, ImageDraw, ImageFont

2. Create Empty List

lst = []

3. Number of images and text in each image loop

N determines the number of images and Each element in this list will contain the text you want in each image

n = int(input("Enter number of elements : "))# iterating till the range
for i in range(0, n):
ele = input()
lst.append(ele) # adding the element

4. Check the text you entered

print(lst)

5. Loop to select font, dimension, and color of the background, the position of text, and color of the font. Save images at the same location as that of the code.

for x in range(len(lst)):
img = Image.new('RGB', (1080, 1920), color = (73, 109, 137))
fnt = ImageFont.truetype('CenturyGothic.ttf', 130)
d = ImageDraw.Draw(img)
d.text((300,900), lst[x], font = fnt, fill=(255, 255, 0))
y = lst[x] + " name.jpg"
img.save(y)

This is the very basic code I used and can be modified and many ways including size, dimension, color, location, and more.

This gives an advantage where you can have a fixed aesthetic for your Instagram or Twitter and don't have to worry about doing the same process over and over again.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vinay Gandhi

Vinay Gandhi

I am an Analyst, Engineer, Problem Solver & Tech Enthusiast.