一个简单的将yuv图片转化为jpeg格式图片的Python脚本程序
来源:互联网 发布:淘宝mrd模板 编辑:程序博客网 时间:2024/05/18 00:00
from PIL import Image
def yuv420_to_rgb888(width, height, yuv):
# function requires both width and height to be multiples of 4
if (width % 4) or (height % 4):
raise Exception("width and height must be multiples of 4")
rgb_bytes = bytearray(width*height*3)
red_index = 0
green_index = 1
blue_index = 2
y_index = 0
for row in range(0,height):
u_index = width * height + (row//2)*(width//2)
v_index = u_index + (width*height)//4
for column in range(0,width):
Y = yuv[y_index]
U = yuv[u_index]
V = yuv[v_index]
C = (Y - 16) * 298
D = U - 128
E = V - 128
R = (C + 409*E + 128) // 256
G = (C - 100*D - 208*E + 128) // 256
B = (C + 516 * D + 128) // 256
R = 255 if (R > 255) else (0 if (R < 0) else R)
G = 255 if (G > 255) else (0 if (G < 0) else G)
B = 255 if (B > 255) else (0 if (B < 0) else B)
rgb_bytes[red_index] = R
rgb_bytes[green_index] = G
rgb_bytes[blue_index] = B
u_index += (column % 2)
v_index += (column % 2)
y_index += 1
red_index += 3
green_index += 3
blue_index += 3
return rgb_bytes
def testConversion(source, dest):
print("opening file")
f = open(source, "rb")
yuv = f.read()
f.close()
print("read file")
rgb_bytes = yuv420_to_rgb888(4208,3120, yuv)
# cProfile.runctx('yuv420_to_rgb888(1920,1088, yuv)', {'yuv420_to_rgb888':yuv420_to_rgb888}, {'yuv':yuv})
print("finished conversion. Creating image object")
img = Image.frombytes("RGB", (4208,3120), bytes(rgb_bytes))
print("Image object created. Starting to save")
img.save(dest, "JPEG")
img.close()
print("Save completed")
testConversion("C:/adb1031/yuveffectout/MV_F_Cap1.yuv", "C:/adb1031/yuveffectout/MV_F_Cap1.jpg")
testConversion("C:/adb1031/yuveffectout/MV_F_Cap2.yuv", "C:/adb1031/yuveffectout/MV_F_Cap2.jpg")
def yuv420_to_rgb888(width, height, yuv):
# function requires both width and height to be multiples of 4
if (width % 4) or (height % 4):
raise Exception("width and height must be multiples of 4")
rgb_bytes = bytearray(width*height*3)
red_index = 0
green_index = 1
blue_index = 2
y_index = 0
for row in range(0,height):
u_index = width * height + (row//2)*(width//2)
v_index = u_index + (width*height)//4
for column in range(0,width):
Y = yuv[y_index]
U = yuv[u_index]
V = yuv[v_index]
C = (Y - 16) * 298
D = U - 128
E = V - 128
R = (C + 409*E + 128) // 256
G = (C - 100*D - 208*E + 128) // 256
B = (C + 516 * D + 128) // 256
R = 255 if (R > 255) else (0 if (R < 0) else R)
G = 255 if (G > 255) else (0 if (G < 0) else G)
B = 255 if (B > 255) else (0 if (B < 0) else B)
rgb_bytes[red_index] = R
rgb_bytes[green_index] = G
rgb_bytes[blue_index] = B
u_index += (column % 2)
v_index += (column % 2)
y_index += 1
red_index += 3
green_index += 3
blue_index += 3
return rgb_bytes
def testConversion(source, dest):
print("opening file")
f = open(source, "rb")
yuv = f.read()
f.close()
print("read file")
rgb_bytes = yuv420_to_rgb888(4208,3120, yuv)
# cProfile.runctx('yuv420_to_rgb888(1920,1088, yuv)', {'yuv420_to_rgb888':yuv420_to_rgb888}, {'yuv':yuv})
print("finished conversion. Creating image object")
img = Image.frombytes("RGB", (4208,3120), bytes(rgb_bytes))
print("Image object created. Starting to save")
img.save(dest, "JPEG")
img.close()
print("Save completed")
testConversion("C:/adb1031/yuveffectout/MV_F_Cap1.yuv", "C:/adb1031/yuveffectout/MV_F_Cap1.jpg")
testConversion("C:/adb1031/yuveffectout/MV_F_Cap2.yuv", "C:/adb1031/yuveffectout/MV_F_Cap2.jpg")
阅读全文
0 0
- 一个简单的将yuv图片转化为jpeg格式图片的Python脚本程序
- 如何将jpg等格式的图片转化为yuv
- 将一组tiff格式的图片转化为一个pdf文件
- 用JpegLib压缩YUV,yuv转化为jpeg格式图像程序源代码
- webp格式图片转化为常见的png格式图片
- 关于Python图片着色后存储为“JPEG”格式存在明显色差的测试
- 用 python 将图片转化为字符串
- caffe-将图片转化为siamese网络需要的数据库格式
- caffe-将图片转化为siamese网络需要的数据库格式
- MFC中将bmp格式图片改为Jpeg格式图片的简单方法
- linux下bmp转化为jpeg程序源代码, jpeg图片压缩
- 如何将图片中的一个任意四边形区域的图像转化为矩形【附源码】
- 一个非常简单的opencv实现bmp,jpeg格式转化的代码
- Python 如何优雅的将数字转化为时间格式
- YUV格式、RGB格式、JPEG格式、MJPEG格式之间的转换(C程序)之一
- android将图片转化为指定的宽和高
- android将图片转化为指定的宽和高
- 将图片转化为字节数组更好的方法
- 文件上传(类型限制)及删除实例
- Qt开发社区开区之言
- Android/Ios 运行Cordova打包Vue的WebApp
- 30:含k个3的数
- springboot发送邮件
- 一个简单的将yuv图片转化为jpeg格式图片的Python脚本程序
- PCB电源布线宽度的问题
- 【学习摘记】马士兵Servlet&JSP_课时15_application——servlet的运行context
- java选择排序
- android中常见的内存溢出和解决办法
- Linux使用vim编辑器,屏幕被锁
- 四.二分法查找
- GCC的编译流程分为了四个步骤:
- win7 64位 php环境开启curl服务Call to undefined function curl_init