fvid/fvid/fvid_cython.pyx

40 lines
1.1 KiB
Cython
Raw Normal View History

# distutils: language=c++
2020-10-13 14:15:56 +02:00
# cython: boundscheck=False
# cython: cdivision=True
# cython: wraparound=False
# cython: c_string_type=unicode, c_string_encoding=ascii
from libcpp.string cimport string
2020-10-13 14:15:56 +02:00
2021-02-25 19:57:46 +01:00
cpdef str cy_gbfi(image):
2020-10-13 14:15:56 +02:00
cdef int width, height, x, y
cdef string bits = b''
2020-10-25 19:04:04 +01:00
cdef (int, int, int) pixel
2020-10-13 14:15:56 +02:00
width, height = image.size
px = image.load()
for y in range(height):
for x in range(width):
pixel = px[x, y]
# if the white difference is smaller (comparison part 1), that means the pixel is closer
# to white, otherwise, the pixel must be black
bits.append(b'1' if abs(pixel[0] - 255) < abs(pixel[0] - 0) and abs(pixel[1] - 255) < abs(pixel[1] - 0) and abs(pixel[2] - 255) < abs(pixel[2] - 0) else b'0')
2020-10-13 14:15:56 +02:00
return bits
2021-02-25 19:57:46 +01:00
cpdef str cy_gbfi_h265(image):
cdef int width, height, x, y
cdef string bits = b''
2021-02-25 19:57:46 +01:00
width, height = image.size
px = image.load()
for y in range(height):
for x in range(width):
bits.append(b'1' if px[x, y] == 255 else b'0')
2021-02-25 19:57:46 +01:00
return bits