# Using PIL on the code node (Python)

**URL:** <https://community.n8n.io/t/using-pil-on-the-code-node-python/65727>\
**Category:** Questions\
**Created:** [December 16, 2024, 7:20pm UTC](https://community.n8n.io/t/using-pil-on-the-code-node-python/65727 "2024-12-16T19:20:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![UnluckyForSome](https://avatars.discourse-cdn.com/v4/letter/u/eada6e/32.png) [@UnluckyForSome](https://community.n8n.io/u/UnluckyForSome)\
**Post date:** [December 16, 2024, 7:20pm UTC](https://community.n8n.io/t/using-pil-on-the-code-node-python/65727/1 "2024-12-16T19:20:27Z")

</div>

Hello,

I’m trying to follow the solution listed here:

> [@Image manipulation with n8n and code-node (python)](http://community.n8n.io/t/image-manipulation-with-n8n-and-code-node-python/41842):
>
> I want to make some changes to an images with python (using PIL or something similar). Could somebody give me an example on how to do this? The biggest problem for me is to load the image. Could somebody create an example code how to get an image from the previous node, convert it to black and white and output it? Thanks in advance! code for python: from PIL import Image image\_path = 'path\_to\_your\_image.jpg' #### How to do this in n8n by loading the image from the the previous node? image =…

Which downloads an image and then uses the code:

```auto
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! 😃

---

<div class="post-metadata">

**Author:** ![n8n](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/n8n/32/26906_2.png) [@n8n](https://community.n8n.io/u/n8n)\
**Post date:** [December 16, 2024, 7:20pm UTC](https://community.n8n.io/t/using-pil-on-the-code-node-python/65727/2 "2024-12-16T19:20:29Z")

</div>

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:**

---

<div class="post-metadata">

**Author:** ![Jon](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/jon/32/40174_2.png) [@Jon](https://community.n8n.io/u/Jon)\
**Post date:** [December 21, 2024, 12:51am UTC](https://community.n8n.io/t/using-pil-on-the-code-node-python/65727/3 "2024-12-21T00:51:36Z")

</div>

Hey @UnluckyForSome

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

---

<div class="post-metadata">

**Author:** ![rubcaspac60](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/rubcaspac60/32/44999_2.png) [@rubcaspac60](https://community.n8n.io/u/rubcaspac60)\
**Post date:** [March 13, 2025, 8:17am UTC](https://community.n8n.io/t/using-pil-on-the-code-node-python/65727/4 "2025-03-13T08:17:18Z")

</div>

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

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/5/d/5dfe8a802f6cb2913df30a48366ed6384150371a.png)

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.

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/6/c/6c48dba753c515915bb3b097fad065a165f7021e.png)

 ![image](https://europe1.discourse-cdn.com/n8n/original/3X/9/a/9a3c5047912d0e582afd5fe0513d8d4425934097.png)

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/n8n/user_avatar/community.n8n.io/system/32/36532_2.png) [@system](https://community.n8n.io/u/system)\
**Post date:** [June 11, 2025, 8:18am UTC](https://community.n8n.io/t/using-pil-on-the-code-node-python/65727/5 "2025-06-11T08:18:00Z")

</div>

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