0

I'm trying to save an image but I'm getting an error message saying there is no such file or directory, even though I have the picture saved.

import PIL
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import ctypes, time
import urllib

while 1:
    font_size = 50

    font = ImageFont.load_default() 
    img = Image.open('bg.bmp')
    draw = ImageDraw.Draw(img)

    draw.text((300, 500), 'Period 1: ')
    draw.text((300, 550), 'Period 2: ')
    draw.text((300, 600), 'Period 3: ')
    draw.text((300, 650), 'Period 4: ')
    draw.text((300, 700), 'Period 5: ')
    draw.text((300, 750), 'Period 6: ')
    draw = ImageDraw.Draw(img)
    draw = ImageDraw.Draw(img)
    img.save("mod_bg.bmp")





pathToBmp = "C:\Documents and Settings\User\My Documents\My Pictures\ScreenShot.bmp"
SPI_SETDESKWALLPAPER = 20  
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, pathToBmp , 0)

quit()

this is the error message

Traceback (most recent call last):
File "C:\Documents and Settings\User\Desktop\Project.py", line 12, in <module>
img = Image.open('bg.bmp')
File "C:\Python27\lib\site-packages\PIL\Image.py", line 1952, in open
fp = __builtin__.open(fp, "rb")
IOError: [Errno 2] No such file or directory: 'bg.bmp'

1 Answer 1

2

You didn't specify a full path, so Python is looking in the current working directory, probably same directory your script is in, for the file. Put bg.bmp there, or else specify the correct path.

Also, since you're using Windows, do a dir in a command line window and make sure whatever program you used to make that image didn't add an extra extension and the file's not actually named bg.bmp.bmp or something.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't get it: OP wants to save a file, which means, file might not exist previously. Why should OP then "Put bg.bmp" anywhere? Shouldn't the file be automatically created upon save, if it doesn't exist?
The error occurs when he's trying to read the file bg.bmp. bg.bmp isn't where he expects it to be. He can't get to the part where he writes the file because he gets an error trying to read the file he wants to modify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.