简体   繁体   English

路径名太长,在 Python 中尝试使用 OpenCV 或 PIL.Image 保存图像时出错

[英]Path name too long, error when trying to save image with OpenCV or PIL.Image in Python

I'm trying to save an image as:我正在尝试将图像保存为:

img_path = folder_path + "\\image1.png" 

folder_path exists, as I previously check it: folder_path 存在,因为我之前检查过它:

if(not os.path.exists(folder_path)):
    raise OSError("Directory does not exist") #doesn't raise error

I have tried with OpenCV我已经尝试过 OpenCV

cv2.imwrite(img_path, img)

But it doesn't create the image, as when I do但它不会像我那样创建图像

if(not os.path.exists(img_path)):
    raise OSError("Image not saved correctly.") #raises error

It raises the error, and when looking to the folder, the image is not there.它会引发错误,并且在查看文件夹时,图像不存在。 I have also tried with PIL.Image我也试过 PIL.Image

img.save(img_path) 

Which gives a FileNotFoundError.这给出了 FileNotFoundError。

I think the problem is that folder_path is too long (it has 279 characters), as when I try to save the image to another folder it saves it correctly.我认为问题在于 folder_path 太长(它有 279 个字符),因为当我尝试将图像保存到另一个文件夹时,它会正确保存。 I don't think there is a permission problem.我不认为存在许可问题。 Is there any way to create some kind of "shortcut" for the OS that the name of the path may be shorter?有没有办法为操作系统创建某种“快捷方式”,路径名称可能更短? It's important for the images to be saved at that path.将图像保存在该路径中很重要。

我发现,使用os.chdir(folder_path)更改工作目录,然后将图像保存为cv2.imwrite("image1.png")

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

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