python - 使用 numpy 和 mencoder 直接绘制到电影中

标签 python numpy matplotlib video-processing mencoder

因此,这应该是对 this thread 的评论, 但它显然是关闭的,所以就这样吧。正如这里所建议的那样,我一直在使用 matplotlib、numpy 和 mencoder 玩得很成功。从那以后我就收养了Voki Codder buffer to stdin solution ,这大大加快了整个过程。问题是,我找不到有关命令的 -format="bgra"部分的任何文档。这意味着字节从右到左是蓝绿红 alpha,右。它们必须是 uint32 还是其他。问题是我正在绘制 float 的颜色图,所以我试图将它们转换为灰度,但我得到了很多奇怪的图案,这让我坚信我做错了什么。我编写此函数是为了在一个范围内将 float 转换为 uint32。但是结果不是我所期望的,我是不是做了什么蠢事?

def grayscale(x, min, max):
  return np.uint32((x-min)/(max-min)*0xffffff)

最佳答案

我认为您对 uint32 代表什么感到困惑。它是 4 段 uint8 整数。

如果您有 float 据,并希望以灰度表示它,您不想将其重新缩放到完整的 32 位范围,而是想将其重新缩放到 8 位范围,并为红色、绿色和蓝色波段(然后大概放入一个恒定的 alpha 波段)。

您也可以只使用不同的字节顺序。 Y8 只是一个灰度级 8 位波段,Y16 是一个灰度级 16 位波段。 (查看 mencoder -rawvideo format=help 的输出以获得完整(虽然有些困惑)列表。)

只是为了说明如何使用 numpy 将 32 位整数视为四个 8 位整数组:

import numpy as np
height, width = 20,20

# Make an array with 4 bands of uint8 integers
image = np.zeros((height, width, 4), dtype=np.uint8)

# Filling a single band (red) 
b,g,r,a = image.T
r.fill(255) 

# Fill the image with yellow and leave alpha alone
image[...,:3] = (255, 255, 0) 

# Then when we want to view it as a single, 32-bit band:
image32bit = image.reshape(-1).view(np.uint32).reshape(height, width)
# (Note that this is a view. In other words,  we could change "b" above 
#  and it would change "image32bit")

然而,在您的情况下,您可能想要做更多类似这样的事情:

import numpy as np
from videosink import VideoSink

height, width = 20,20
numframes = 1000
data = np.random.random((height, width, numframes))

# Rescale your data into 0-255, 8-bit integers 
# (This could be done in-place if you need to conserve memory)
d    ata_rescaled = 255.0 / (data.max() - data.min()) * (data - data.min())
data_rescaled = data_rescaled.astype(np.uint8)

# The key here is the "Y8" format. It's 8-bit grayscale.
video = VideoSink((height,width), "test", rate=20, byteorder="Y8")

# Iterate over last axis
for frame in data.T:
    video.run(frame.T)
video.close()

关于python - 使用 numpy 和 mencoder 直接绘制到电影中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7042190/

相关文章:

python 2.7 : output utf-8 in Windows console

python - Python 中的时间复杂度 - 大 O 表示法

python-3.x - Seaborn 散点图标记参数不起作用

python - 如何将 1d numpy 数组 append 到 2d numpy 数组 python

python - 在 matplotlib 中动画化 3d 散点图

python - 绘制阶梯图并提取连续值

python - python中一致的读写文件

python - 在 Pandas DataFrame 中重新定义索引值

python - 设置 Chameleon 中内联替换的默认值

python - 将数组添加到 numpy 数组