Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Post Creator code #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added NextQABG.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added RobotoSlab.ttf
Binary file not shown.
57 changes: 57 additions & 0 deletions image_creator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from PIL import Image, ImageDraw, ImageFont
import json
import textwrap
import os

def create_quote_image(quote, background_path, font_path, output_path, width=1080, height=1080):
# Open the background image
background_img = Image.open(background_path)

# Resize and crop the background image to fit 1080x1080
background_img.thumbnail((width, height))
background_img = background_img.crop((0, 0, width, height))

# Create a draw object
draw = ImageDraw.Draw(background_img)

# Specify the font and size
font_size = 48
font = ImageFont.truetype(font_path, font_size)

# Wrap text
wrapped_text = textwrap.fill(quote, width=30)

# Get text size
text_bbox = draw.multiline_textbbox((0, 0), wrapped_text, font=font)
text_width = text_bbox[2] - text_bbox[0]
text_height = text_bbox[3] - text_bbox[1]

# Calculate text position (centered)
text_x = (width - text_width) / 2
text_y = (height - text_height) / 2

# Add text to image
draw.multiline_text((text_x, text_y), wrapped_text, font=font, fill=(255, 255, 255), align="center")

# Save the result
background_img.save(output_path)

# Load quotes from JSON file
with open('quotes.json', 'r') as file:
quotes = json.load(file)

# Set paths for background image and font file
background_path = "NextQABG.png"
font_path = "RobotoSlab.ttf"

# Ensure the files exist
if not os.path.exists(background_path):
raise FileNotFoundError(f"Background image not found: {background_path}")
if not os.path.exists(font_path):
raise FileNotFoundError(f"Font file not found: {font_path}")

# Process each quote
for i, quote in enumerate(quotes):
create_quote_image(quote, background_path, font_path, f'quote_image_{i}.png')

print("All images have been created successfully.")
5 changes: 5 additions & 0 deletions quotes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
"The best way to get started is to quit talking and begin doing.",
"The pessimist sees difficulty in every opportunity. The optimist sees opportunity in every difficulty.",
"Don't let yesterday take up too much of today."
]
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ beautifulsoup4==4.10.0
python-dotenv==1.0.0
Requests==2.31.0
pyppeteer==2.0.0
nepali-datetime==1.0.8.2
nepali-datetime==1.0.8.2
pillow==8.2.0
numpy==1.19.5