2

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 16x16 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?

4

2 回答 2

2

这是一个叠加层。你不能把其他东西“放在上面”:来自文档

叠加对象始终可见,并且始终显示在常规显示内容之上。

YUV 覆盖利用媒体播放器通常用于显示视频的硬件。内容通常不会像这样写入“帧缓冲区”(导致屏幕截图/屏幕捕获软件等的空白区域无休止的悲伤)。

因此,您必须将要添加的内容直接“绘制”到 y,u,v 数据中。(或将 y,u,v 数据转换为 RGB 数据并以更常规的方式显示)。

于 2012-04-22T00:42:40.627 回答
0

以为我会发布我是如何在 pygame 中从 YCbCr 进行转换的,因为我无法使用谷歌找到任何解决方案。

#!/usr/bin/env python

import pygame
import Image
import sys

W = 352
H = 288
WH = (W, H)

pygame.init()
screen = pygame.display.set_mode(WH)

fd = open('foreman.yuv', 'rb')
y = fd.read(W * H)
u = fd.read(W * H / 4)
v = fd.read(W * H / 4)
fd.close()

# Use PIL to get a black-n-white version of the YCbCr
# and convert it to RGB
im = Image.fromstring('L', WH, y)
rgb = im.convert('RGB')

s = pygame.image.frombuffer(rgb.tostring(), rgb.size, rgb.mode)

screen.blit(s, (0,0))
pygame.display.flip()
于 2012-04-26T13:36:05.157 回答