I didn’t quite understand the purpose, but if you want to simply save the image to use it again with Opencv, you can use the method save()
of the image in this way:
pil_image = pyautogui.screenshot()
pil_image.save("<Nome_do_arquivo>")
image = cv2.imread("<Nome_do_arquivo>")
If what you want is to actually convert the image directly without saving to use with Opencv, the process will be a little more complicated and you will need to use the library numpy
for that reason.
First, you must turn your image into an object of numpy.array
and after that you should convert it from RGB to BGR. See below:
pil_image = pyautogui.screenshot()
image = numpy.array(pil_image)
# Converte de RGB para BGR
image = image[:, :, ::-1].copy()
cv2.imshow("Imagem", image)
A shortcut to . save would simply be:
pyautogui.screenshot('<Nome_do_arquivo>')
;)– Guilherme Nascimento
It was exactly the second example I needed. But I would also like to use cv2.imshow’s function to turn the image into Grayscale (cv2.imshow(img, 0). Is there any way to do this with numpy.array ?
– x8ss
@x8ss image is already the image in Opencv default, in BGR color space. Just convert
image
for the grayscale color space with the functioncv2.cvtColor
opencv.– danieltakeshi