(WindowsError 183)cv2.imread/write如何使这些函数在运行脚本的不同位置执行

2024-05-18 00:16:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从目录路径C:\Automation\OCR\images运行脚本。我们正在阅读的PNG也在这条路上。输出路径为:C:\Automation\OCR\Drop。所发生的情况是,我在shell中得到一个错误,即WindowsError:[error 183]无法在文件已经存在时创建该文件:“C:\Automation\OCR\Drop”我希望能够隔离脚本,从特定文件夹中读取PNG文件,然后在其他文件夹中输出预处理的PNG。你知道吗

下面的图片。你知道吗

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)

Tags: importimgpngvaluemodenpcv2kernel
1条回答
网友
1楼 · 发布于 2024-05-18 00:16:46

这条线

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

应该改为

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

如果你得到了意想不到的行为,一定要搜索OpenCV文档,这样你就知道调用函数的正确方法,这样你就可以得到预期的返回值。例如,docs for ^{}显示:

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表示“return value”,因此它不太具有描述性。大多数未命名的返回值都不重要,或者用于调试目的;在使用文件系统执行某些操作的情况下,这种返回值通常是一个布尔值,让您知道操作是否成功完成。你知道吗

将来,您还可以在解释器中运行help(cv2.imwrite)(或使用任何其他OpenCV函数),并且您应该能够获得上面显示的语法行,这样您至少可以看到应该如何调用该函数。你知道吗

相关问题 更多 >