Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python将原始Y视频缓冲区转换为图像?_Python_Opencv - Fatal编程技术网

如何使用python将原始Y视频缓冲区转换为图像?

如何使用python将原始Y视频缓冲区转换为图像?,python,opencv,Python,Opencv,我有一个Y视频平面的原始文件(Y.raw)从原始视频帧格式YUV(YUV422\u半平面)中提取出来。如何将此Y.raw转换为图像png或jpg?您基本上得到了一个没有打包或交错的单通道灰度图像。因此,您有几个选择: 命令行上的ImageMagick 通过NetPBM PGM格式和Photoshop或GIMP 蟒蛇 终端中命令行的ImageMagick 如果您像大多数Linux发行版一样安装了ImageMagick(或者您可以在Mac和Windows上免费安装),您可以在终端中将其转换为类

我有一个Y视频平面的原始文件
(Y.raw)
从原始视频帧格式YUV
(YUV422\u半平面)
中提取出来。如何将此
Y.raw
转换为图像png或jpg?

您基本上得到了一个没有打包或交错的单通道灰度图像。因此,您有几个选择:

  • 命令行上的ImageMagick
  • 通过NetPBM PGM格式和Photoshop或GIMP
  • 蟒蛇

终端中命令行的ImageMagick

如果您像大多数Linux发行版一样安装了ImageMagick(或者您可以在Mac和Windows上免费安装),您可以在终端中将其转换为类似的版本

假设它是640x480像素和8位,您想要一个PNG:

convert -depth 8 -size 640x480 gray:y.raw result.png
假设它是1024x768像素和16位,您想要对比度拉伸的JPG:

convert -depth 16 -size 1024x768 gray:y.raw -auto-level result.jpg

通过NetPBM PGM格式和Photoshop或GIMP

假设您没有ImageMagick,您可以将该文件制作成
PGM
(便携式灰色地图),您可以在
GIMP
Paint
feh
Adobe Photoshop中查看/编辑/保存:

{ printf "P5\n640 480\n255\n"; cat y.raw; } > result.pgm
如果不幸的是,您在Windows上,并且希望这样做,我想它会是这样的:

echo "P5"        > header.txt
echo "640 480"  >> header.txt
echo "255"      >> header.txt
copy /b header.txt+y.raw result.ppm
#!/usr/local/bin/python3

from PIL import Image

file=open("y.raw",'rb')
rawdata=file.read()
file.close()

imgsize = (640,480)

# Use the PIL raw decoder
img = Image.frombytes('L',imgsize,rawdata)
img.save('result.png')
convert -size 640x480 gradient: -depth 8 gray:y.raw

Python

如果你真的真的想写一堆Python,它可能看起来像这样:

echo "P5"        > header.txt
echo "640 480"  >> header.txt
echo "255"      >> header.txt
copy /b header.txt+y.raw result.ppm
#!/usr/local/bin/python3

from PIL import Image

file=open("y.raw",'rb')
rawdata=file.read()
file.close()

imgsize = (640,480)

# Use the PIL raw decoder
img = Image.frombytes('L',imgsize,rawdata)
img.save('result.png')
convert -size 640x480 gradient: -depth 8 gray:y.raw

如果其他任何人正在阅读此内容,并希望他们有一帧Y数据可供使用,您可以使用ImageMagick轻松创建一个包含黑白渐变的模拟帧,如下所示:

echo "P5"        > header.txt
echo "640 480"  >> header.txt
echo "255"      >> header.txt
copy /b header.txt+y.raw result.ppm
#!/usr/local/bin/python3

from PIL import Image

file=open("y.raw",'rb')
rawdata=file.read()
file.close()

imgsize = (640,480)

# Use the PIL raw decoder
img = Image.frombytes('L',imgsize,rawdata)
img.save('result.png')
convert -size 640x480 gradient: -depth 8 gray:y.raw

对于
ls
,它将如下所示:

-rw-r--r--   1 mark  staff  307200 23 Mar 10:05 y.raw

你基本上得到了一个单通道灰度图像,没有包装或交错。因此,您有几个选择:

  • 命令行上的ImageMagick
  • 通过NetPBM PGM格式和Photoshop或GIMP
  • 蟒蛇

终端中命令行的ImageMagick

如果您像大多数Linux发行版一样安装了ImageMagick(或者您可以在Mac和Windows上免费安装),您可以在终端中将其转换为类似的版本

假设它是640x480像素和8位,您想要一个PNG:

convert -depth 8 -size 640x480 gray:y.raw result.png
假设它是1024x768像素和16位,您想要对比度拉伸的JPG:

convert -depth 16 -size 1024x768 gray:y.raw -auto-level result.jpg

通过NetPBM PGM格式和Photoshop或GIMP

假设您没有ImageMagick,您可以将该文件制作成
PGM
(便携式灰色地图),您可以在
GIMP
Paint
feh
Adobe Photoshop中查看/编辑/保存:

{ printf "P5\n640 480\n255\n"; cat y.raw; } > result.pgm
如果不幸的是,您在Windows上,并且希望这样做,我想它会是这样的:

echo "P5"        > header.txt
echo "640 480"  >> header.txt
echo "255"      >> header.txt
copy /b header.txt+y.raw result.ppm
#!/usr/local/bin/python3

from PIL import Image

file=open("y.raw",'rb')
rawdata=file.read()
file.close()

imgsize = (640,480)

# Use the PIL raw decoder
img = Image.frombytes('L',imgsize,rawdata)
img.save('result.png')
convert -size 640x480 gradient: -depth 8 gray:y.raw

Python

如果你真的真的想写一堆Python,它可能看起来像这样:

echo "P5"        > header.txt
echo "640 480"  >> header.txt
echo "255"      >> header.txt
copy /b header.txt+y.raw result.ppm
#!/usr/local/bin/python3

from PIL import Image

file=open("y.raw",'rb')
rawdata=file.read()
file.close()

imgsize = (640,480)

# Use the PIL raw decoder
img = Image.frombytes('L',imgsize,rawdata)
img.save('result.png')
convert -size 640x480 gradient: -depth 8 gray:y.raw

如果其他任何人正在阅读此内容,并希望他们有一帧Y数据可供使用,您可以使用ImageMagick轻松创建一个包含黑白渐变的模拟帧,如下所示:

echo "P5"        > header.txt
echo "640 480"  >> header.txt
echo "255"      >> header.txt
copy /b header.txt+y.raw result.ppm
#!/usr/local/bin/python3

from PIL import Image

file=open("y.raw",'rb')
rawdata=file.read()
file.close()

imgsize = (640,480)

# Use the PIL raw decoder
img = Image.frombytes('L',imgsize,rawdata)
img.save('result.png')
convert -size 640x480 gradient: -depth 8 gray:y.raw

对于
ls
,它将如下所示:

-rw-r--r--   1 mark  staff  307200 23 Mar 10:05 y.raw

用Python?或者这只是一次性的事情?如果是后者,我建议研究ImageMagick。也有一个Python绑定可用于此?或者这只是一次性的事情?如果是后者,我建议研究ImageMagick。也有一个Python绑定可用于此。