Using PIL on the code node (Python)

Hello,

I’m trying to follow the solution listed here:

Which downloads an image and then uses the code:

from PIL import Image
import io
import base64

binary_data = _input.last().binary.data.data
decoded_data = base64.b64decode(binary_data)

image = Image.open(io.BytesIO(decoded_data))
bw_image = image.convert('L')

bw_image_b = io.BytesIO()
bw_image.save(bw_image_b, 'png')
bw_bytes = bw_image_b.getvalue()

encoded_bw_image = base64.b64encode(bw_bytes).decode('utf-8')

return {"bw_image": encoded_bw_image}

To do basic Python PIL manipulations on the code node, but i’m encountering the error

ERROR: UnidentifiedImageError( PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x1388d30>

Has something changed in the syntax requirements, or am I just running it incorrectly?

Many thanks again! :smiley:

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hey @UnluckyForSome

Have you made sure that the binary_data is using the correct variable for the output from the node before it?

Hi there @Jon, I have a follow up on this question.
I am having a series of excel files which are quite old and I am looking to extract the data using a python script in the “code node”.

I upload the file using a “form node” first, which is fantastic.

  • my second option was to use “extract from file node” but I was not able to go through all the sheets in the document and to slice the data, I seemed to lack this option somehow.

so after many fails I decided to go through a hard coded python script so that I can manipulate and directly extract the data from the sheets (all have the same form).

you can see in the image bellow that I am having issues to access the file. I have used the Methods in the documentation but I cannot have access to the binary or the path to access the file.

_input.item, _input.all(), etc.

can you please tell me how to direct the uploaded file to the code?

this is the error:
Node type

n8n-nodes-base.code

Node version

2 (Latest)

n8n version

1.81.4 (Self Hosted)

Stack trace

Error: ValueError(msg) ValueError: Invalid file path or buffer object type: <class 'pyodide.ffi.JsException'> at PythonSandbox.getPrettyError (C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-nodes-base\nodes\Code\PythonSandbox.ts:101:11) at PythonSandbox.runCodeInPython (C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-nodes-base\nodes\Code\PythonSandbox.ts:85:15) at PythonSandbox.runCodeAllItems (C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-nodes-base\nodes\Code\PythonSandbox.ts:46:27) at ExecuteContext.execute (C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-nodes-base\nodes\Code\Code.node.ts:155:14) at WorkflowExecute.runNode (C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-core\src\execution-engine\workflow-execute.ts:1123:8) at C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-core\src\execution-engine\workflow-execute.ts:1470:27 at C:\Users\RubenCasillasPacheco\AppData\Roaming\npm\node_modules\n8n\node_modules\n8n-core\src\execution-engine\workflow-execute.ts:2029:11

Is this possible?

Many thanks.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.