How to Return an Image from a Python Code Step or HTTP Response Step?

This topic was automatically generated from Slack. You can find the original thread here.

How to return an image from a Python step?
How can I return an image from a Python code step? I tried base64 serialization but it does not work :disappointed:
In the end I even want to return the image from a HTTP response step.
Any hints…?

can you help?

Of course, I’m here to help! You mentioned that you want to return an image from a Python code step and eventually return it from an HTTP response step. To achieve this, you can use the following approach: 1. Read the image file and convert it to a base64 encoded string. 2. Return the base64 encoded string from the Python code step. 3. In the HTTP response step, set the Content-Type header to the appropriate image MIME type (e.g., image/jpeg, image/png) and return the base64 decoded image data. Here’s an example of a Python code step that reads an image file and returns its base64 encoded string: import base64 with open("path/to/your/image.jpg", "rb") as image_file: encoded_image = base64.b64encode(image_file.read()).decode("utf-8") return {"encoded_image": encoded_image} Next, in the HTTP response step, you can set the Content-Type header and return the base64 decoded image data: const base64Image = steps.python_step.outputs.encoded_image; const imageData = Buffer.from(base64Image, "base64"); return { status_code: 200, headers: { "Content-Type": "image/jpeg", }, body: imageData, }; Replace steps.python_step with the actual name of your Python code step, and make sure to use the correct MIME type for your image in the Content-Type header.

, would you mind adding the snippet above into your Python code step?

Thanks! That already was very helpful!

But the problem isn’t solved yet.
This is code in the image generation step. As you can see I return a base64 string.
I’m using ASCII decoding in line 38 to keep it most basic.

image.png

The base64 string indeed is an image. I checked it with a base64->PNG tool.

In the next step I return the string like this. I deserialize it with atob(). As I assume a byte array is supposed to be send back to the client in the body of the HTTP response.
But that’s not working :disappointed:
What am I missing?

image.png

Using the CURL command the bytes get stored, but the pie.png is not recognized as a PNG and doesn’t display.

curl -X POST
https://eoks1a1hzm37ri8.m.pipedream.net
-H ā€˜Content-Type: application/json’
-d ā€˜{ā€œaā€:60,ā€œbā€:40}’
–output pie.png

, to get the image output from your response, you’ll need to decode the base64 data on your client side (curl won’t decode base64 automatically). The browser can decode it automatically, but for terminal, you’ll need to decode it yourself. You can search the internet on how to do that

The image will be received by some no-code tool where it’s hard to do such a processing step.
Can’t I send back just raw bytes?

The http-response step is using Javascript. I can use atob(). But that does cut it yet. What’s missing?

How about uploading the image to Google Drive/Dropbox, then just pass the authenticated link to download the image? I think it would be more scalable method

Then very temporary images will accumulate on GDrive :disappointed: I want to avoid garbage collection.

This looked promising at first. But something is still missing…

Sending the image as base64 encoding is correct, it’s the problem when the Client (where you received it, the no-code tool) need to decode it. So I don’t think it can be solve on Pipedream side if you insist with this method

:worried: Hm… really? That’s a bummer.
Ok, I’ll check the no-code tool. Maybe I can add a processing step there after the REST call to the pipedream flow.
Thanks for your help!!!

I got it to work!:partying_face: The magic is possible using a Buffer. (There was a hint to that in this article: Triggers)

image.png

This way no decoding is needed as the example from Insomnia shows. The CURL call nicely stores the bytes in a file which can be displayed.