This topic was automatically generated from Slack. You can find the original thread here.
Hey guys!
I am running this script and it takes forever to finish the test, literarily it wont end.
When i am running this script on my VS code, it runs perfectly.
Can you help me understand what is the problem and how to fix it?
import io
import requests
from PIL import Image, ImageDraw
def generate_ad(image_url: str, text: str, logo_url: str) -> bytes:
# Load the image and logo
image = Image.open(requests.get(image_url, stream=True).raw)
logo = Image.open(requests.get(logo_url, stream=True).raw)
_# Use a transparent logo_
logo = logo.convert("RGBA")
_# Resize the logo to a reasonable size_
logo_width, logo_height = logo.size
max_logo_height = image.height // 5
**if** logo_height > max_logo_height:
new_height = max_logo_height
logo = logo.resize((**int**(logo_width ** new_height / logo_height), new_height))
_# Get the dominant color of the logo_
pixels = logo.getdata()
dominant_color = **max**(pixels, key=**lambda** pixel: pixel[0] + pixel[1] + pixel[2])
_# Create a blank image for the ad_
ad_width = image.width
ad_height = image.height
ad = Image.new('RGBA', (ad_width, ad_height), (255, 255, 255, 255))
_# Convert the image to "RGBA" and paste it onto the ad image_
image = image.convert("RGBA")
ad.alpha_composite(image)
_# Paste the logo onto the ad image_
logo_x = 0
logo_y = ad_height - logo.height
ad.alpha_composite(logo, (logo_x, logo_y))
_# Draw the text onto the ad image_
draw = ImageDraw.Draw(ad)
_# Get the size of the text_
text_width, text_height = draw.textsize(text)
_# Determine the position of the text in the middle of the ad_
text_x = (ad_width - text_width) // 2
text_y = (ad_height - text_height) // 2
_# Use the dominant color of the logo as the text color_
text_color = dominant_color
_# Draw the text without a background_
draw.text((text_x, text_y), text, fill=text_color)
# Save the final ad to a buffer
buffer = io.BytesIO()
ad.save(buffer, “PNG”)
buffer.seek(0)
_# Encode the image in base64_
image_data = base64.b64encode(**buffer**.read()).decode("utf-8")
_# Return the image as a link_
**return** f"data:image/png;base64,{image_data}"
# Specify the input files
*def handler(pd: “pipedream”):
image_file = “https://storage.googleapis.com/tally-response-assets/r9ak5v/4ab51e6c-0449-4c38-9d7c-ad8ef2a8431e/B-Train_in_paradise_with_grass_and_lake_background_1cf3e998-95d8-40ae-b4f6-6b5ce4f046e1.png”
text = “New Graphics”
logo_file = “https://storage.googleapis.com/tally-response-assets/r9ak5v/b95b95be-86ef-4069-ba27-43065c8a5352/logo.jpg”
result = {
“data”: generate_ad(image_file, text, logo_file)
}
