fvid/fvid/fvid_cython.pyx

31 lines
867 B
Cython
Raw Normal View History

2020-10-17 17:33:12 +02:00
# distutils: language=c
2020-10-13 14:15:56 +02:00
# cython: boundscheck=False
# cython: cdivision=True
# cython: wraparound=False
2020-10-25 19:04:04 +01:00
cpdef str cy_get_bits_from_image(image):
2020-10-13 14:15:56 +02:00
cdef int width, height, x, y
2020-10-25 19:04:04 +01:00
cdef str pixel_bin_rep, bits
cdef (int, int, int) pixel
2020-10-13 14:15:56 +02:00
width, height = image.size
px = image.load()
bits = ""
for y in range(height):
for x in range(width):
pixel = px[x, y]
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"0"
2020-10-13 14:15:56 +02:00
# if the white difference is smaller (comparison part 1), that means the pixel is closer
# to white, otherwise, the pixel must be black
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):
2020-10-17 17:33:12 +02:00
pixel_bin_rep = <str>"1"
2020-10-13 14:15:56 +02:00
# adding bits
bits += pixel_bin_rep
return bits