将 OpenCV 代码片段从 C++ 转换为 Python

Converting OpenCV code snippet from C++ to Python

我正在尝试将此代码转换为 python。
谁能帮帮我?

    cv::Mat image;
while (image.empty()) 
{
    image = cv::imread("capture.jpg",1);
}
cv::imwrite("result.jpg",image);
`

在 Python 中,C++ 的 Mat 变为 numpy 数组,因此图像处理变得像访问多维数组一样简单。但是,C++ 和 Python.

中的方法名称相同
import cv2 #importing opencv module

img = cv2.imread("capture.jpg", 1) #Reading the whole image

cv2.imwrite("result.jpg", img) # Creating a new image and copying the contents of img to it

编辑:如果你想在图像文件生成后立即写入内容,那么你可以使用os.path.isfile() which return a bool 值取决于给定目录中文件的存在。

import cv2 
import os.path

while not os.path.isfile("capture.jpg"):
    #ignore if no such file is present.
    pass

img = cv2.imread("capture.jpg", 0)

cv2.imwrite("result.jpg", img)

每个方法的详细实现和基本图片操作也可以参考docs