簡體   English   中英

路徑名太長,在 Python 中嘗試使用 OpenCV 或 PIL.Image 保存圖像時出錯

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

我正在嘗試將圖像保存為:

img_path = folder_path + "\\image1.png" 

folder_path 存在,因為我之前檢查過它:

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

我已經嘗試過 OpenCV

cv2.imwrite(img_path, img)

但它不會像我那樣創建圖像

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

它會引發錯誤,並且在查看文件夾時,圖像不存在。 我也試過 PIL.Image

img.save(img_path) 

這給出了 FileNotFoundError。

我認為問題在於 folder_path 太長(它有 279 個字符),因為當我嘗試將圖像保存到另一個文件夾時,它會正確保存。 我不認為存在許可問題。 有沒有辦法為操作系統創建某種“快捷方式”,路徑名稱可能更短? 將圖像保存在該路徑中很重要。

我發現,使用os.chdir(folder_path)更改工作目錄,然后將圖像保存為cv2.imwrite("image1.png")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM