I'm writing a small script that shows some things in Pygame and then writes those same things to a video using OpenCV with Python. The weird thing is that I noticed that the colors in the video showed slightly darker.
So I wrote a simpler program that writes a basic video with a dark background and a blue square moving horizontally. I took a snapshot from the video and compared its BGR values with the ones specified in the program. They were, in fact, darker.
It's the first time I play with writing videos so I don't know much about colorspaces, so maybe this is something obvious but I haven't found the solution to match both colors.
import cv2
import numpy as np
from cv2 import VideoWriter, VideoWriter_fourcc
# Video parameters
width = 1920
height = 1080
fps = 30
seconds = 5
video = VideoWriter('./square.avi', VideoWriter_fourcc(*'mp4v'), float(fps), (width, height))
# Square parameters
cellHeight = 100
cellWidth = 500
xPos = 100
yPos = 100
cellColor = (255, 194, 102) # BGR, ends up as (255, 188, 92) in the written frames
backroundColor = (25, 25, 25) # BGR, ends up as (22, 25, 23)
# Frame loop
for _ in range(seconds * fps):
frame = np.empty((height, width, 3), dtype=np.uint8)
frame[:,:] = backroundColor
pt1 = (xPos, yPos) # NW corner
pt2 = (xPos + cellWidth, yPos + cellWidth) # SE corner
cv2.rectangle(frame, pt1=pt1, pt2=pt2, color=cellColor, thickness=-1)
video.write(frame)
xPos += 10
video.release()
Here you can see the difference between pygame and the video. Despite it is not the same frame, you can see the different colors, especially the green and red.