Working on a YUV-viewer in python using pygame.
The code below displays one frame of YUV 4:2:0
#!/usr/bin/env python
import pygame
W = 352
H = 288
WH = (W, H)
pygame.init()
screen = pygame.display.set_mode(WH)
overlay = pygame.Overlay(pygame.YV12_OVERLAY, WH)
fd = open('foreman.yuv', 'rb')
y = fd.read(W * H)
u = fd.read(W * H / 4)
v = fd.read(W * H / 4)
overlay = pygame.Overlay(pygame.YV12_OVERLAY, WH)
overlay.display((y, u, v))
This code displays a 16×16 semi-transparent rectangle in position (0,0)
pygame.init()
screen = pygame.display.set_mode(WH)
s = pygame.Surface((16,16))
s.set_alpha(128)
s.fill((255,255,255))
screen.blit(s, (0,0))
pygame.display.flip()
But, how do I combine them? I.e. how do I display a semi-transparent rectangle in position (0,0) on top of the YUV-data so that the YUV-data can be seen through the rectangle?
It’s an OVERLAY. You can’t put other stuff “on top” of it: from the docs:
YUV overlays tap into hardware normally used by media players to display video. The content is typically never written to a “framebuffer” as such (causing endless grief with blank areas for screenshot/screen-capture software etc).
So you’d have to draw what you want to add over it directly “into” the y,u,v data. (Or convert the y,u,v data to RGB data and display it by more conventional means).