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);
`
3
  • Just checking, have you pasted your complete code? Commented May 22, 2015 at 14:50
  • Why use the while? Can't you just do if? Commented May 22, 2015 at 15:00
  • @IronManMark20 : Initially, there is no image, so i have to wait until the image come. Commented May 22, 2015 at 21:31

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

there is no while loop in this code and i need the presence of the while loop in my coding
But why ? @AliDhouib why are you making it complex ? it will serve it's purpose, try running it. please explain me the context ?
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.
can you help me? any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.