简体   繁体   中英

I am trying to save this image in a folder but it is showing this error. What am I doing wrong here?

This is my code

from PIL import Image
from PIL import ImageDraw

# Open an Image
FRAME_SIZE = (720,1280)
img = Image.new('RGB', FRAME_SIZE, color = 'black')

# Call draw Method to add 2D graphics in an image
I1 = ImageDraw.Draw(img)

# Add Text to an image
I1.text((100, 100), "nice Car", fill=(255, 32, 0))

# Display edited image
img.show()

# Save the edited image
img.save("./new_folder/car2.png")

And this is error.

Traceback (most recent call last):
  File "D:\Python Projects\devops-directive-hello-world\trial.py", line 19, in <module>
    img.save("./new_folder/car2.png")
  File "C:\Users\Pushpendra\Desktop\Drone\devops-directive-hello-world\lib\site-packages\PIL\Image.py", line 2317, in save
    fp = builtins.open(filename, "w+b")
FileNotFoundError: [Errno 2] No such file or directory: './new_folder/car2.png'

Process finished with exit code 1

Just change the destination folder.

img.save("car2.png")

this will do the trick. At least you must choose an existing directory

The most possible reason for this error to occur is your Python directory is in C: so your relative path is referencing your Python directory only.

Work around in this case can be to use absolute path, like so: 'D/Python Projects/newfolder/'

Hopefully it solves your problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM