0

Here's my code:

import colorgram
import turtle

colors = colorgram.extract("color.jpg", 30)
color_list = []
for object in colors:
    color = object.rgb
    red = color.r
    green = color.g
    blue = color.b
    color_list.append((red, green, blue))

tim = turtle.Turtle()


screen = turtle.Screen()
screen.setworldcoordinates(-1, -1, screen.window_width()-1, screen.window_height()-1)
screen.exitonclick()

Error:

(venv) ➜  PythonProgramming git:(main) ✗ /Users/wenzexu/Documents/GitHub/PythonProgramming/venv/bin/python /Users/wenzexu/Documents/GitHub/PythonProgramming/day-18-turt
le/main.py
Traceback (most recent call last):
  File "/Users/wenzexu/Documents/GitHub/PythonProgramming/day-18-turtle/main.py", line 4, in <module>
    colors = colorgram.extract("color.jpg", 30)
  File "/Users/wenzexu/Documents/GitHub/PythonProgramming/venv/lib/python3.9/site-packages/colorgram/colorgram.py", line 38, in extract
    image = f if isinstance(f, Image.Image) else Image.open(f)
  File "/Users/wenzexu/Documents/GitHub/PythonProgramming/venv/lib/python3.9/site-packages/PIL/Image.py", line 3218, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'color.jpg'

I want to extract colors from this 'color.jpg', but in the VSCode terminal I got an error: no such file or directory. I have checked my file name, nothing wrong. So what should I supposed to do?

rioV8
  • 24,506
  • 3
  • 32
  • 49
Mcflurry
  • 5
  • 1
  • 2
  • Are both your script and `color.jpg` in the same directory? [See this answer](https://stackoverflow.com/a/22282935/2453382) for more understanding. –  Aug 03 '23 at 06:32
  • 1
    what's the working directory when you run the script in the VS Code terminal? – starball Aug 03 '23 at 08:25
  • don't use CodeRunner but a [launch config with `cwd` argument](https://stackoverflow.com/a/57698163/9938317) – rioV8 Aug 03 '23 at 09:54

1 Answers1

1

So this line here

colors = colorgram.extract("color.jpg", 30)

assumes that color.jpg is inside the current working directory. When running from the terminal it will be the same as the current dir the terminal is in. When using an IDE it can normally be set to something and in most cases the default is the directory the script is in.

So for your code to work the color.jpg probably has to be in the same directory as the script you are trying to run.

I would advice using an absolute path to the color.jpg if you do things like this, so it just works no matter where your working dir is.

Nopileos
  • 1,976
  • 7
  • 17
  • 1
    I would advise not to use an absolute path if you ever want to move the file. – AKX Aug 03 '23 at 06:45
  • Thank you! It's the problem of path. I tried this on pycharm before pasting it to vscode and forgot to change the path... I am stupid... – Mcflurry Aug 03 '23 at 07:11
  • @AKX How would you do it? I always use `f"sys.path[0]/{path}"` but in the end if you move the file it's in another place. So what do you mean? – Andreas Sabelfeld Aug 03 '23 at 09:12
  • @AndreasSabelfeld The correct solution is to either make sure of your working directory, or to refer to something in the same directory as the module being executed, `program_directory = os.path.dirname(__file__)` and `os.path.join()` from there. – AKX Aug 03 '23 at 09:15
  • @AKX Why refer the file relative to the script location??? If the script is in some `bin` directory on the path the image file will never be there. Use a command line argument with a file specification **relative** to the current directory. – rioV8 Aug 03 '23 at 09:59
  • @rioV8 Sometimes it is. Imagine you're writing a game and you have a bunch of sprite graphics – then you might refer to them with `os.path.join(os.path.dirname(__file__), "images", "enemy1.png")`. – AKX Aug 03 '23 at 10:03