使用PIL在Python中写入图像文件的问题

0 投票
1 回答
1808 浏览
提问于 2025-04-16 06:06

我正在写一个小的Python脚本,使用PIL模块来改变一些在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 个回答

2

你需要在文件名中的\字符前面加一个\,或者使用原始字符串:

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

这个错误信息基本上是在说它无法打开这个文件。

撰写回答