4 minutes
Automate an AI driven Instagram account
Large Language Models like ChatGPT aren’t always good at being funny, but with certain prompts I can get it to make me laugh.
I want to share this fun with the world (without using Twitter).
There are plenty of popular Instagram accounts that only post quotes, which is just text overlayed on an image.
Let’s create an account to share this nonsense with the world.
Create a nice background
Many of these inspiring Instagram posts have a calming background.
Let’s get some help form Midjourney.
I used the Aspect Ratio of 4:5 (--ar 4:5
) since that works best for Instagram portrait size.
After some variations and upscaling I have a nice background for my profound but absurd AI generated quotes.
Did this a few more times and saved three background images:
- background_beach.png
- background_valley.png
- background_woods.png
Some code to overlay text on the image
Created a new Python project (using Python 3.11) with a virual environment
mkdir profound_ai_wisdom && cd profound_ai_wisdom
python -m venv .venv
source .venv/bin/activate
Our tool for working with images is Pillow
pip install pillow
I’m storing the list of quotes generated by ChatGPT in quotes.json
{
"quotes": [
"The echo of silence dances in the shadows of the unspoken word.",
"Only when we eat the fruit of the invisible tree can we truly understand the language of the wind.",
"A fish cannot climb a tree, but it can swim in the ocean of dreams.",
"The whisper of an enigma is the shadow cast by the sun of ignorance.",
"A spoonful of uncertainty is worth more than a kettle of boiling assumptions."
]
}
Next we have the python code to loop that through these quotes, select one of the backgrounds randomly, attribute the quote to a profound AI guru, overlay the text, and save.
# overlay_text.py
from PIL import Image, ImageDraw, ImageFont
import textwrap
import json
import random
import os
def wrap_text(draw, text, font, max_width):
lines = textwrap.wrap(text, width=1)
line_width = 0
while line_width <= max_width:
bbox = draw.textbbox((0, 0), lines[0], font)
line_width = bbox[2] - bbox[0]
wrap_width = max_width // (line_width // len(lines[0]) + 1)
new_lines = textwrap.wrap(text, width=wrap_width)
if len(new_lines) == len(lines):
break
lines = new_lines
return lines
def add_text_to_image(image, text, font_path, font_size):
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_path, font_size)
img_width, img_height = image.size
wrapped_text = wrap_text(draw, text, font, img_width)
text_height = len(wrapped_text) * font_size
position_y = (img_height - text_height) // 2
color = (255, 255, 255) # White
border_color = (0, 0, 0) # Black
border_width = 2 # Border width in pixels
for line in wrapped_text:
bbox = draw.textbbox((0, 0), line, font)
text_width = bbox[2] - bbox[0]
position_x = (img_width - text_width) // 2
# Draw the black border
for x_offset in range(-border_width, border_width + 1):
for y_offset in range(-border_width, border_width + 1):
draw.text((position_x + x_offset, position_y + y_offset), line, border_color, font=font)
# Draw the white text
draw.text((position_x, position_y), line, color, font=font)
position_y += font_size
return image
width = 1080 # Ideal width for Instagram Portrait
height = 1350 # Ideal height for Instagram Portrait
# Location of Arial Unicode font on a macbook
font_path = '/Library/Fonts/Arial Unicode.ttf'
font_size = 70 # Big enough, but not too big
with open('quotes.json', 'r') as f:
quotes = json.load(f)
backgrounds = [
"background_beach",
"background_valley",
"background_woods",
]
for i, quote in enumerate(quotes["quotes"]):
bkg = random.choice(backgrounds)
image = Image.open(f'{bkg}.png')
attr_quote = f"{quote} - Profound AI Wisdom"
output_image = add_text_to_image(image, attr_quote, font_path, font_size)
output_dir = f"posts/{bkg}"
os.makedirs(output_dir, exist_ok=True)
output_image.save(f'{output_dir}/quote_{i:0{3}d}.png')
I won’t go into the logic for wrapping text, and drawing white text with a black border.
This makes an assumption that the quotes aren’t too long, or have too many long words (which will add too many new lines, and flood off the background image).
Generate the images
Now we run the program to generate a new image for each quote in quotes.json
python overlay_text.py
Now our posts
directory has filled up with images
Follow profound_ai_wisdom on Instagram for profound quotes that have no meaning!
Code for this can be found on Github.