mirror of
https://github.com/gunchev/home_bin.git
synced 2025-11-20 12:15:46 +00:00
Cleanup and optimize yuv420p_to_png.py
This commit is contained in:
parent
c45966f597
commit
e62479d8a0
1 changed files with 55 additions and 56 deletions
|
|
@ -1,61 +1,60 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
# Author : sherin.s@gmail.com (Sherin Sasidhan)
|
# Author : sherin.s@gmail.com (Sherin Sasidhan)
|
||||||
# Date : 05-Oct-2010
|
# Date : 05-Oct-2010
|
||||||
|
# URL : https://shrex999.wordpress.com/2013/07/31/yuv-to-rgb-python-imaging-library/
|
||||||
import Image
|
#
|
||||||
|
# Updated: 2019-06-15 - Doncho Gunchev <dgunchev@gmail.com>
|
||||||
|
|
||||||
|
from PIL import Image
|
||||||
import sys
|
import sys
|
||||||
from struct import *
|
from struct import *
|
||||||
import array
|
from array import array
|
||||||
|
from os.path import splitext
|
||||||
|
|
||||||
if len(sys.argv) != 4:
|
if len(sys.argv) != 4:
|
||||||
print "***** Usage syntax Error!!!! *****\n"
|
sys.exit("""***** Usage syntax Error!!!! *****
|
||||||
print "Usage:"
|
|
||||||
print "python <script> <.yuv file yuv420p> <width>> <height> "
|
Usage:
|
||||||
sys.exit(1) # exit
|
%r yuv420p_FILE.yuv WIDTH HEIGHT
|
||||||
else:
|
""" % sys.argv[0])
|
||||||
pass
|
|
||||||
|
def load_yuv420p(yuv_file_path, width, height):
|
||||||
image_name = sys.argv[1]
|
"""Read YUV420p file and return PIL Image"""
|
||||||
width = int(sys.argv[2])
|
yuv_file = open(yuv_file_path, "rb")
|
||||||
height = int(sys.argv[3])
|
|
||||||
|
y_data = array('B')
|
||||||
y = array.array('B')
|
y_data.fromfile(yuv_file, height * width)
|
||||||
u = array.array('B')
|
u_data = array('B')
|
||||||
v = array.array('B')
|
u_data.fromfile(yuv_file, (height // 2) * (width // 2))
|
||||||
|
v_data = array('B')
|
||||||
f_y = open(image_name, "rb")
|
v_data.fromfile(yuv_file, (height // 2) * (width // 2))
|
||||||
f_uv = open(image_name, "rb")
|
|
||||||
f_uv.seek(width*height, 1)
|
result = Image.new("RGB", (width, height))
|
||||||
|
pixels = result.load()
|
||||||
image_out = Image.new("RGB", (width, height))
|
for i in range(height):
|
||||||
pix = image_out.load()
|
for j in range(width):
|
||||||
|
y_value = (y_data[(i * width) + j] - 16) * 1.164
|
||||||
print "width=", width, "height=", height
|
offset = ((i // 2) * (width // 2)) + (j // 2)
|
||||||
|
u_value = u_data[offset] - 128
|
||||||
for i in range(0, height/2):
|
v_value = v_data[offset] - 128
|
||||||
for j in range(0, width/2):
|
# B = 1.164(Y - 16) + 2.018(U - 128)
|
||||||
u.append(ord(f_uv.read(1)));
|
# G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
|
||||||
|
# R = 1.164(Y - 16) + 1.596(V - 128)
|
||||||
for i in range(0, height/2):
|
b_value = y_value + 2.018 * u_value
|
||||||
for j in range(0, width/2):
|
g_value = y_value - 0.813 * v_value - 0.391 * u_value
|
||||||
v.append(ord(f_uv.read(1)));
|
r_value = y_value + 1.596 * v_value
|
||||||
for i in range(0,height):
|
pixels[j, i] = (int(r_value), int(g_value), int(b_value))
|
||||||
for j in range(0, width):
|
|
||||||
y.append(ord(f_y.read(1)));
|
return result
|
||||||
#print "i=", i, "j=", j , (i*width), ((i*width) +j)
|
|
||||||
#pix[j, i] = y[(i*width) +j], y[(i*width) +j], y[(i*width) +j]
|
def main():
|
||||||
Y_val = y[(i*width)+j]
|
yuv_file_path = sys.argv[1]
|
||||||
U_val = u[((i/2)*(width/2))+(j/2)]
|
width, height = int(sys.argv[2]), int(sys.argv[3])
|
||||||
V_val = v[((i/2)*(width/2))+(j/2)]
|
picture = load_yuv420p(yuv_file_path, width, height)
|
||||||
B = 1.164 * (Y_val-16) + 2.018 * (U_val - 128)
|
picture.save(splitext(yuv_file_path)[0] + '.png')
|
||||||
G = 1.164 * (Y_val-16) - 0.813 * (V_val - 128) - 0.391 * (U_val - 128)
|
picture.show()
|
||||||
R = 1.164 * (Y_val-16) + 1.596*(V_val - 128)
|
|
||||||
pix[j, i] = int(R), int(G), int(B)
|
|
||||||
|
if __name__ == "__main__":
|
||||||
######################################################
|
main()
|
||||||
# B = 1.164(Y - 16) + 2.018(U - 128)
|
|
||||||
# G = 1.164(Y - 16) - 0.813(V - 128) - 0.391(U - 128)
|
|
||||||
# R = 1.164(Y - 16) + 1.596(V - 128)
|
|
||||||
######################################################
|
|
||||||
|
|
||||||
#image_out.save("out.bmp")
|
|
||||||
image_out.show()
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue