使用PIL在Python中编写映像文件的问题

时间:2010-10-26 16:04:58

标签: python python-imaging-library

我正在使用PIL模块编写一个小的Python脚本来更改MultiGen Creator中3D模型上使用的一些纹理的大小。我也在使用openflight API,这就是mg *功能。

这是脚本

import PIL
from PIL import Image

db = mgGetCurrentDb()
ret,index,name = mgGetFirstTexture (db)
while (ret):
 myAttr = mgReadImageAttributes (name)
 existingattrs = mgGetAttList (myAttr,fltImgHeight,fltImgWidth)
 print existingattrs[2]
 print existingattrs[4]
 if existingattrs[2] != 0 and existingattrs[4] != 0:
  Height = existingattrs[2]/4
  Width = existingattrs[4]/4

  print name
  print Width
  print Height
  imageFile = (name)
  im1 = Image.open(imageFile)
  im2 = im1.resize((Width,Height),PIL.Image.BILINEAR)
  ImgOut = "C:\DB\PLW\out.jpg"

  im2.save(ImgOut)

  ret,index,name = mgGetNextTexture (db)

无论如何所有似乎都有效,但当我尝试编写文件时,我收到以下错误

E: Traceback (most recent call last):
E:   File "<string>", line 24, in <module>
E:   File "C:\Python25\lib\site-packages\PIL\Image.py", line 1439, in save
E:     save_handler(self, fp, filename)
E:   File "C:\Python25\Lib\site-packages\PIL\JpegImagePlugin.py", line 471, in _save
E:     ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
E:   File "C:\Python25\Lib\site-packages\PIL\ImageFile.py", line 499, in _save
E:     s = e.encode_to_file(fh, bufsize)
E: IOError: [Errno 0] Error

1 个答案:

答案 0 :(得分:2)

您需要将文件名中的\字符翻倍或使用原始字符串:

  ImgOut = "C:\\DB\\PLW\\out.jpg" 
  ImgOut = r"C:\DB\PLW\out.jpg" 

错误消息基本上是说它无法打开文件。