imhotep
imhotep

Reputation: 67

Python Opencv output.avi screengrab color is blue intensive

When I try to perform a video capture of the screen, the output file always has more blue in the output. It never copies the color exactly.

I have tried a few different Codec's in for the fourcc to be used with opencv and video writer. I have tried MJPG with .mjpg, and XVID .avi filetypes. Also tried adding a .convert('RBGA') to the end of the ImagrGrab.grab() line. ex ImageGrab.grab().convert('RGBA')

from PIL import ImageGrab
from PIL import ImageColor
import cv2
import numpy as np

def run():
    fourcc = cv2.VideoWriter_fourcc(*'MJPG')
    vid = cv2.VideoWriter('test5.mjpg', fourcc, 8, (width, height))
    while(True):
        img = ImageGrab.grab(bbox=(0, 0, width, height))#testSpec: bbox=(0, 0, 800, 1000)
        img_np = np.array(img)
        # frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2GRAY)
        vid.write(img_np)
        ## Visually inspect recording in progress, by showing frame. 
        # cv2.imshow("frame", img_np) #frame 
        key = cv2.waitKey(1)
        if key == 27:
            break
    vid.release()
    cv2.destroyAllWindows(); 

Code works well for fullscreen capture. It just makes everything blue. It mostly changes all reds to deep blue. its very annoying.

Upvotes: 3

Views: 4367

Answers (1)

J.D.
J.D.

Reputation: 4561

OpenCV expects an image to be BGR. Your screen grab is RGB. So for openCV to display/save the image correctly you need to convert it to BGR. Seems your nearly there, as you apparently figured out how to convert to gray. Converting to BGR is basically the same:

    img_np = np.array(img)
    frame = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
    cv2.imshow("frame", frame)
    cv2.imshow("img", img_np)

This results in the following:
Left is unprocessed, right is color converted. The logo should indeed be blue.

enter image description here

Upvotes: 9

Related Questions