我正在從目錄路徑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。
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)
你可以改寫你的問題嗎?我不明白你在問什麼 – Milk
我不知道你想要什麼,但這條線怎麼樣'img = cv2.imwrite('enhancedThresholdTeleCapture.png',0)'如果你沒有提供,應該保存一個圖像輸入圖像的功能?該功能也不會返回一個圖像,所以我想你從來沒有讀過使用這些函數的OpenCV文檔... – Piglet
@Milk我已經編輯了這個帖子沉重。 – lizardwizard