1

I'm trying to convert this code to python.
can anyone help me?

    cv::Mat image;
while (image.empty()) 
{
    image = cv::imread("capture.jpg",1);
}
cv::imwrite("result.jpg",image);
`
ZdaR
  • 22,343
  • 7
  • 66
  • 87
Ali Dhouib
  • 13
  • 3

1 Answers1

1

In Python the Mat of C++ becomes a numpy array and hence the image manipulation becomes as simple as accessing a multi dimensional array. However the methods name are same in both C++ and 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

EDIT: If you want to write the contents as soon as the image file is being generated then you can use os.path.isfile() which return a bool value depending upon the presence of a file in the given directory.

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)

You can also refer to docs for detailed implementation of each method and basic image operations.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ZdaR
  • 22,343
  • 7
  • 66
  • 87
  • there is no while loop in this code and i need the presence of the while loop in my coding – Ali Dhouib May 22 '15 at 21:32
  • But why ? @AliDhouib why are you making it complex ? it will serve it's purpose, try running it. please explain me the context ? – ZdaR May 22 '15 at 21:46
  • I have to wait until the program receive an image, it comes from another process throw a server! so i have to repeat reading the image from the path until it becomes true. And after reading that image, i have another process in this same program. – Ali Dhouib May 22 '15 at 21:54
  • can you help me? any idea? – Ali Dhouib May 22 '15 at 22:00
  • I will point you to here:https://stackoverflow.com/questions/23628325/cv2-imread-checking-if-image-is-being-read – IronManMark20 May 22 '15 at 22:02