繁体   English   中英

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

[英]Problem Writing Image file in Python with PIL

我正在使用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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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