-2

i tried to extract a time frame of a video, and show it as image(JPEG). But unfortunately the displayed image using PIL.Image.fromarray() is bluer than it supposed to be. the tricky part is, when i save it first (using cv2.imwrite) and open that new file using PIL.Image.open(), the image has the right color.

enter image description here

I thought it has something to do with the RGB array composition, but then i checked it, and both array (direct from the frame and from the Image.open()) are exactly the same.

Is there anything i can do to show the right image without saving it as an external file beforehand?

Thank you.

def TimeFrame(file, tf):
    capture = cv2.VideoCapture(file)
    frameCount = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))
    capture.set(cv2.CAP_PROP_POS_FRAMES, tf)
    _, frame = capture.read()


    directTF = Image.fromarray(frame)

    cv2.imwrite("12345678999.jpg", frame)
    image = Image.open("12345678999.jpg")

note: file is the name of the video and tf is the certain timeframe that you want to extract the image from.

Ben
  • 21
  • 5

2 Answers2

3

OpenCV uses BGR as its default colour order for images

Use cv2.cvtColor(img, cv2.COLOR_BGR2RGB) before displaying

0

OpenCV uses BGR, PIL uses RGB.

You might want to try

directTF = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

to have OpenCV flip those channels before you ask PIL to read the array.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • Hii it worked! thank you so much. Out of curiosity, why pyhton says that both arrays are the same, when in fact, we need to flip the array beforehand? – Ben Feb 11 '22 at 15:41