How to create instagram card with only python pillow library (P1)


Good morning guys.
Recently, I am very interested with Quohat-Inscard which has some great effects on instagram. So I decided to show you how to create a similar one using the python programming language.

Xem bài viết bằng tiếng việt.

Such a process would come handy whenever someone is working with photo contests and needs to extract them quickly from a number of images.

The project has two essential elements:
  1. Python to process an image
  2. Sever to take a picture form instagram
Today I will write about how to process images by python. let's code!

Look at the Instacard, it has 5 layers: Background, shadow, card, avatar and infomation. I am using only PIL library to make it. First, creating project and installing library with your terminal window.

>> pip install pillow

1. Import library

from PIL import Image, ImageDraw
from PIL import ImageFilter, ImageFont

Import Image PIL to get image information such as size and processing it. ImageDraw, ImageFont library for texts, ImageFilter for effects.

2. Load image from folder

Copy your images which you download from instagram, they are placed in the main directory in your project. Rename it to "image.jpg" then open it and get size.

image = Image.open('image.jpg')
h = image.size[0]
w = image.size[1]

You can use another square image after added this code instead.

your_image = Image.open('image.jpg')
image = your_image.resize((1080, 1080), Image.ANTIALIAS)
h = image.size[0]
w = image.size[1]

Note: Make sure your image has height ≤ width

3. Create backgound

#background module
background = image.crop((0, int(int(w-h)/2), int(h), int(int(w)-int(w-h)/2)))

Background will have size 1080x1080. Let's see it.



The size of the background will be the standard size. It will appear some errors when you use the portrait image. You can fix it by coding the conditions for the input image size.

5. Create card

#card modulecard = image.crop((270, 50, 860, 1050))

After that, I will make second layer - shadow.

4. Create crop shadow

#shadow modulesh1 = background
sh2 = Image.open('./materials/shadow.jpg')
sh3 = Image.open('./materials/solid_shadow.jpg')
mask_sh = Image.new("L", sh2.size, 0)
draw_sh = ImageDraw.Draw(mask_sh)
mask_sh = sh3.resize(sh2.size).convert('L')
back_sh = sh1.copy()
back_sh.paste(sh2, (270, 100), mask_sh)
sh = back_sh.filter(ImageFilter.GaussianBlur(radius=10))

Before, you have to create a folder named "materials" and put materials to materials folder in project. You can dowload them below.



6. Insert card

Insert card on the background with shadow available.

#insert cardbackground_shadow = sh
is1 = background_shadow.resize((1200, 1200), Image.ANTIALIAS)
is2 = card
is3 = Image.open('./materials/solid.jpg')
mask_is = Image.new("L", is2.size, 0)
draw_is = ImageDraw.Draw(mask_is)
mask_is = is3.resize(is2.size).convert('L')
back_is = is1.copy()
back_is.paste(is2, (298, 115), mask_is)

Let's see result :)


7. Insert avatar

Copy your avatar which you download on instagram to material folder in your project, image size will be 150x150 pixel. If it is not present, we'll create it. We should resize it before create mask, don't foget to download materials above.

#insert_avatarav1 = back_is
av2_1 = Image.open('./materials/avatar.jpg')
av2 = av2_1.resize((100, 100), Image.ANTIALIAS)
av3 = Image.open('./materials/solid_avatar.jpg')
mask_av = Image.new("L", av2.size, 0)
draw_av = ImageDraw.Draw(mask_av)
mask_av = av3.resize(av2.size).convert('L')
av = av1.copy()
av.paste(av2, (345, 170), mask_av)

8. Insert information

#texttext = av
draw_text = ImageDraw.Draw(text)
font_01 = ImageFont.truetype(r'.\fonts/TCCEB.ttf', 28)
font_02 = ImageFont.truetype(r'.\fonts/TCCEB.ttf', 40)
draw_text.text((530, 160),"phongcanon",(255,255,255),font=font_01)
draw_text.text((502, 220),"34",(255,255,255),font=font_02)
draw_text.text((615, 220),"325",(255,255,255),font=font_02)
draw_text.text((775, 220),"56",(255,255,255),font=font_02)

You can edit it, "phongcanon" is account name, "34" is number of posts, "325" is number of following and "56" is followers.

9. Insert theme icon

#insert theme
in1 = text
in2 = Image.open('./materials/white.jpg')
in3 = Image.open('./materials/info.jpg')
mask_info = Image.new("L", in2.size, 0)
draw_in = ImageDraw.Draw(mask_info)
mask_info = in3.resize(in2.size).convert('L')
info = in1.copy()
info.paste(in2, (298, 115), mask_info)
info.save('result.jpg', quality=95)

We made it.
This project will be developed with get JSON function later. Finally, let's see the results.


Please tell me if there are any errors in the article. Have fun and share it, share button below.