2013-04-16 96 views
0

我試圖保存圖像,但我收到一條錯誤消息,說沒有這樣的文件或目錄,即使我保存了圖片。Python腳本圖像保存目錄/文件名錯誤

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

這是錯誤消息

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' 

回答

2

沒有指定一個完整的路徑,所以Python是尋找在當前工作目錄中,可能同一個目錄下你的腳本是,該文件。把bg.bmp放在那裏,否則指定正確的路徑。

而且,由於你使用的是Windows,做一個命令行窗口dir,並確保任何程序,你用來做這種形象不添加額外的擴展名和文件實際上沒有命名bg.bmp.bmp什麼的。