-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopengraph_gen.py
47 lines (39 loc) · 1.63 KB
/
opengraph_gen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from PIL import Image
from io import BytesIO
import requests
def create_open_graph_image(image_urls):
# Function to fetch and resize an image from a URL
def fetch_and_resize_image(url, size):
response = requests.get(url)
if response.status_code == 200:
image = Image.open(BytesIO(response.content))
return image.resize(size)
else:
print(f"Failed to retrieve the image from {url}")
return None
images = [fetch_and_resize_image(url, (243, 412)) for url in image_urls]
# Calculate the base image dimensions for a 16:9 aspect ratio
image_count = len(images)
spacing = 50 # Space between images
total_image_width = (243, 412)[0] * image_count
total_spacing_width = spacing * (image_count - 1)
content_width = total_image_width + total_spacing_width
aspect_ratio = 1200 / 630
base_height = int(content_width / aspect_ratio)
base_height += 100 # Add padding to the height
base_width = int(base_height * aspect_ratio)
# Create a black rectangle as the base image
base_image = Image.new('RGB', (base_width, base_height), 'black')
# Calculate positions to evenly space and center the images
start_x = (base_width - content_width) // 2
positions = []
current_x = start_x
for _ in range(image_count):
positions.append((current_x, (base_height - (243, 412)[1]) // 2))
current_x += (243, 412)[0] + spacing
# Paste the images onto the base image
for img, pos in zip(images, positions):
if img is not None:
base_image.paste(img, pos)
# Save the final image
return base_image