What is the Problem with Running a Script on Visual Studio Code and How Can it be Fixed?

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)
}

Hi , on top of my mind maybe it’s because your action might exceed Pipedream resource or timeout limit.

Could you try accessing your Workflow setting and adjust the Execution Controls section then try again?

For more information about Pipedream’s limit, see Limits

thats my settings

I mean you can try to increase the Timeout and Memory and try again

Just for visibility we got this sorted :slightly_smiling_face:

what was it?

was just a few revisions to the code and pulling the right PD steps through

import base64
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 = pd.steps["python_1"]["$return_value"][0]
    text = pd.steps["python_1"]["$return_value"][1]
    logo_file = pd.steps["python_1"]["$return_value"][2]
    result = generate_ad(image_file, text, logo_file)
    print(result)

This was end result - may not be perfect but it runs and gives output required :slightly_smiling_face:

thanks - was just curious :slightly_smiling_face:

Is it really that easy to find the dominant color in Python?

dominant_color = max(pixels, key=lambda pixel: pixel[0] + pixel[1] + pixel[2])

I guess that just finds the highest frequency color in a given image?

everything is easier in python - except dependency management

That much I’m learning. virtualenv, conda, miniconda, docker, and I’m probably missing other ways haha

I’m new to python (6months in) and partly why I love pipedream so much is that I can use it to get the result i want - although now starting to learn nodejs because of pipedream lol

Very cool, it’s not so so different. I personally like data filtering, transformations, async handling etc in Node.js.

I highly recommend reading this book to learn the differences between other languages like Python/Ruby/PHP to JS:

https://eloquentjavascript.net/