-2

我正在從目錄路徑C:\ Automation \ OCR \ images運行我的腳本。我們正在閱讀的PNG也在這條道路上。輸出路徑是:C:\ Automation \ OCR \ Drop。會發生什麼情況是我的shell在WindowsError中出現錯誤:[錯誤183]該文件已存在時無法創建文件:'C:\ Automation \ OCR \ Drop'我希望能夠將腳本隔離,讀取來自特定文件夾的PNG文件,然後將預處理的PNG輸出到不同的文件夾中。(WindowsError 183)cv2.imread/write如何使這些功能在運行腳本的不同位置

Pics below。

http://imgur.com/a/AbWUA

import cv2 
import numpy as np 
import math 
import os 
from matplotlib import pyplot as plt 
from cycler import cycler 
from PIL import Image, ImageEnhance 

# Read PNG 
dirname = 'C:\Automation\OCR\Drop' 
os.mkdir(dirname) 
img = cv2.imread('teleCapture.png', 0) 

def bilateral_adaptive_threshold(img, ksize=20, C=0, mode='floor', true_value=255, false_value=0): 
mask = np.full(img.shape, false_value, dtype=np.uint8) 

kernel_l = np.array([[1] * (ksize) + [-ksize]], dtype=np.int16) 
kernel_r = np.array([[-ksize] + [1] * (ksize)], dtype=np.int16) 
kernel_u = np.array([[1]] * (ksize) + [[-ksize]], dtype=np.int16) 
kernel_d = np.array([[-ksize]] + [[1]] * (ksize), dtype=np.int16) 

if mode == 'floor': 
    delta = C * ksize 
elif mode == 'ceil': 
    delta = -C * ksize 
else: raise ValueError("Unexpected mode value. Expected value is 'floor' or 'ceil'.") 

left_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_l, anchor=(ksize,0), delta=delta, borderType=cv2.BORDER_CONSTANT) 
right_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_r, anchor=(0,0), delta=delta, borderType=cv2.BORDER_CONSTANT) 
up_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_u, anchor=(0,ksize), delta=delta, borderType=cv2.BORDER_CONSTANT) 
down_thresh = cv2.filter2D(img, cv2.CV_16S, kernel_d, anchor=(0,0), delta=delta, borderType=cv2.BORDER_CONSTANT) 

if mode == 'floor': 
    mask[((0 > left_thresh) & (0 > right_thresh)) | ((0 > up_thresh) & (0 > down_thresh))] = true_value 
elif mode == 'ceil': 
    mask[((0 < left_thresh) & (0 < right_thresh)) | ((0 < up_thresh) & (0 < down_thresh))] = true_value 
return mask 

# Write modified PNG to the path 
os.chdir(dirname) 
cv2.imwrite('enhancedThresholdTeleCapture.png', img) 
+1

你可以改寫你的問題嗎?我不明白你在問什麼 – Milk

+0

我不知道你想要什麼,但這條線怎麼樣'img = cv2.imwrite('enhancedThresholdTeleCapture.png',0)'如果你沒有提供,應該保存一個圖像輸入圖像的功能?該功能也不會返回一個圖像,所以我想你從來沒有讀過使用這些函數的OpenCV文檔... – Piglet

+0

@Milk我已經編輯了這個帖子沉重。 – lizardwizard

回答

0

此行

img = cv2.imwrite('enhancedThresholdTeleCapture.png',0) 

應改爲

cv2.imwrite('enhancedThresholdTeleCapture.png', img) 

如果您收到意外的行爲,一定要尋找OpenCV的文檔,所以你知道的正確的方式來調用函數,以便您可以獲得預期的返回值。例如,docs for cv2.imwrite()顯示:

Python:cv2.imwrite(filename, img[, params]) → retval

Parameters:

  • filename – Name of the file.
  • image – Image to be saved.
  • params – ...

隨着功能的描述和它做什麼。語法

cv2.imwrite(filename, img[, params]) → retval 

告訴我們調用該函數需要知道的一切。該函數有兩個需要自變量,filenameimg。顯示的最後一個參數params可選(並且僅用於某些文件類型的特殊選項)。列表中的括號表示它是可選的。然後箭頭顯示返回值。如果該函數沒有返回值,則它只是None。在這種情況下,有一個返回值;名稱爲「返回值」的retval,所以它不是很具描述性。大多數未命名的返回值不重要,或者用於調試目的;在使用文件系統執行某些操作的情況下,這種返回值非常常見,因爲它是一個布爾值,讓您知道操作是否成功完成。

將來,您還可以在解釋器中運行help(cv2.imwrite)(或與任何其他OpenCV函數一起使用),並且應該能夠獲得上面顯示的語法行,以便至少可以看到如何調用該函數。

+0

我使用img = cv2.imread('C:\ Automation \ OCR \ images \ teleCapture.png',0)來讀取PNG文件...在預處理這個圖像後,我將把這個修改後的文件寫入一個特定的目錄。 (python腳本,讀取文件和寫入文件是3個不同的位置)這裏是我寫的。 cv2.imwrite('C:\ Automation \ OCR \ hey \ enhancedThresholdTeleCapture.png',img)當我將文件寫入此位置時,輸出PNG文件不可讀。我將附上照片,顯示當輸出是腳本的腳本VS不同的位置時會發生什麼情況。 @AlexanderReynolds – lizardwizard

相關問題