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
In the end I even want to return the image from a HTTP response step.
Any hints�
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.
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.
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
What am I missing?
, 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
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
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
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!!!