簡體   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