Problem with code

Hello,

I have a problem. I get the followig error:

“C:\Users\Georgi Petkov\pythonProject7\venv\Scripts\python.exe” “C:/Users/Ilian Petkov/pythonProject7/main.py”
Traceback (most recent call last):
File “C:\Users\Georgi Petkov\pythonProject7\main.py”, line 19, in
arr = Image.open(‘Img/arrow/{}.png’.format(row[‘arrow’])).convert(“RGBA”)
File “C:\Users\Georgi Petkov\pythonProject7\venv\lib\site-packages\PIL\Image.py”, line 3092, in open
fp = builtins.open(filename, “rb”)
FileNotFoundError: [Errno 2] No such file or directory: ‘Img/arrow/Arrow Red.png’

Thank you in advance!

Well, it means what it says: there’s no Img/arrow/Arrow Red.png file.
Do you expect that there will be?

Notice that it is a relative path. Are you running your programme in a
directory where that path is valid?

Cheers,
Cameron Simpson cs@cskk.id.au

Hello Cameron,

I know there are no files but I’m looking for where else the problem might be as there are no .png in the help files

Well that’s the immediate problem. You’re trying to open a .png file, and there isn’t one.

We can’t help any more without knowing a lot more about your code and what it’s trying to do.

Maybe paste in your whole main.py file between triple backticks, eg:

```
contents of main.py
go here
```

Cheers,
Cameron Simpson

I have attached the file to the email you provided above as it is giving me an error here.

Best Regards!

If you know there are no files, what did you expect to happen?

“I can’t open this box.”

“But you don’t have a box.”

“I know I don’t have any boxes. But why can’t I open it?”

The problem lies in your code. You are asking Python to open a png file that doesn’t exist.

Your code says:

Image.open('Img/arrow/{}.png'.format(row['arrow']))

The row['arrow'] part comes from your data, and it resolves to ‘Arrow Red’, so Python tries to open ‘Img/arrow/Arrow Red.png’ which doesn’t exist.

There’s nothing we can do to solve this for you. Only you can solve it:

  • Why are you trying to open and convert a file that doesn’t exist?
  • Where does ‘Arrow Red’ come from?
  • Are you sure the path ‘Img/arrow/’ is correct?

We can’t answer those questions, only you can.

I’m transcribing Georgi’s main.py below for reference, since he sent
it to me directly.

Georgi: we prefer output and code pasted inline in messages, not as
attachments, on the whole. Just paste code between triple backticks, eg:

 ```
 your code
 or output
 goes here
 ```

Commentry in a later post. But the core issue is indeed that you’re
trying to open a file which doesn’t exist. You’ve got a
label_information.csv file which appears to mention some “arrow”
names, which you’re using to infer an associated image file pathname.

Cheers,
Cameron Simpson cs@cskk.id.au

# Import libaries
from PIL import Image
import pandas as pd
from PIL import Image, ImageOps, ImageDraw
from PIL import ImageFont
from barcode import EAN13
from barcode.writer import ImageWriter

# Import the file
df = pd.read_csv('label_information.csv')

# Start the loop
for index, row in df.iterrows():

     # Create White Canvas with border
     bg = Image.new('RGBA', (500, 200), color = (255, 255, 255))
     bg = ImageOps.expand(bg,border=1,fill='black')
     # Open Arrow
     arr = Image.open('Img/arrow/{}.png'.format(row['arrow'])).convert("RGBA")
     arr = arr.resize((int(arr.size[0] * 1.35), int(arr.size[1] * 1.35)))
     # Paste with Coordinates
     bg.paste(arr, (-40, 0), arr)

     # Icon 1
     zone = Image.open('Img/log/{}.png'.format(row['log'])).convert("RGBA")
     perc_size = 0.45
     l1, l2 = int(zone.size[0] * perc_size), int(zone.size[1] * perc_size)
     zone = zone.resize((l1, l2))
     # Paste with coordinates
     x1, y1 = 415, 0
     bg.paste(zone, (x1, y1), zone)

     # Icon 2
     zone = Image.open('Img/cat/{}.png'.format(row['cat'])).convert("RGBA")
     zone = zone.resize((l1, l2))
     # Paste with coordinates
     x1, y1 = 415, y1 + l2
     bg.paste(zone, (x1, y1), zone)

     # Icon 3
     zone = Image.open('Img/special/{}.png'.format(row['special'])).convert("RGBA")
     zone = zone.resize((l1, l2))
     # Paste with coordinates
     x1, y1 = 415, y1 + l2
     bg.paste(zone, (x1, y1), zone)

Hello,

Thanks everyone for the replies!

Is there anything else required of me in order for you to help me?

Best Regards!

It’s pretty simple - use the correct path to the file.