1

我正在用 scipy 学习图像处理。我在保存图像时遇到了一些相当基本的操作困难。这是我的代码:

import scipy
from scipy import misc

img=misc.imread("C:\\..\\name.jpg")

misc.imsave("image.jpg",img)

我收到错误消息:

Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    misc.imsave("image.jpg",img)
  File "C:\Python27\lib\site-packages\scipy\misc\pilutil.py", line 158, in imsave
    im.save(name)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1461, in save
    fp = builtins.open(fp, "wb")
IOError: [Errno 13] Permission denied: 'image.jpg'
4

3 回答 3

2

保存时尝试使用完整路径:

misc.imsave(r'C:\path\image.jpg', img)

您的错误是权限错误,因此您可能无权在当前目录中写入。您还可以使用 更改当前目录os.chdir( newpath )

于 2013-06-19T12:42:27.470 回答
0

上面的代码在给定目录中创建了一个文件,但它是空的(0 字节)并在 IDLE 中产生错误:

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    misc.imsave(r"D:\Darek\back3.jpg", img)
  File "C:\Python27\lib\site-packages\scipy\misc\pilutil.py", line 158, in imsave
    im.save(name)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 1467, in save
    save_handler(self, fp, filename)
  File "C:\Python27\lib\site-packages\PIL\JpegImagePlugin.py", line 557, in _save
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
  File "C:\Python27\lib\site-packages\PIL\ImageFile.py", line 466, in _save
    e = Image._getencoder(im.mode, e, a, im.encoderconfig)
  File "C:\Python27\lib\site-packages\PIL\Image.py", line 395, in _getencoder
    return encoder(mode, *args + extra)
TypeError: function takes at most 11 arguments (13 given)
于 2013-06-19T12:52:40.080 回答
0

嗯,你的代码在dreampie shell中对我来说很好

import scipy
from scipy import misc
img = misc.imread("C:/folder/name.jpg")
misc.imsave("C:/folder2/image.jpg",img)

我不太了解 PIL,但似乎涉及到编码器问题。您是否尝试过使用不同图像文件的代码?

于 2013-06-19T13:27:37.627 回答