From 676ae0cc7e88dbff74665415bfcf3d37ed2f24e7 Mon Sep 17 00:00:00 2001 From: Theelgirl <43764914+Theelgirl@users.noreply.github.com> Date: Tue, 13 Oct 2020 12:15:56 +0000 Subject: [PATCH] Create fvid_cython.pyx --- fvid/fvid_cython.pyx | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 fvid/fvid_cython.pyx diff --git a/fvid/fvid_cython.pyx b/fvid/fvid_cython.pyx new file mode 100644 index 0000000..18ab9d1 --- /dev/null +++ b/fvid/fvid_cython.pyx @@ -0,0 +1,44 @@ +# distutils: language=c++ +# cython: boundscheck=False +# cython: cdivision=True +# cython: wraparound=False +from operator import sub +from tqdm import tqdm + +cpdef cy_get_bits_from_image(image): + cdef int width, height, x, y + cdef str pixel_bin_rep + cdef tuple white_diff, black_diff, pixel + + width, height = image.size + + px = image.load() + bits = "" + + for y in range(height): + for x in range(width): + pixel = px[x, y] + + pixel_bin_rep = "0" + + # for exact matches + if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255: + pixel_bin_rep = "1" + elif pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0: + pixel_bin_rep = "0" + else: + white_diff = (abs(pixel[0] - 255), abs(pixel[1] - 255), abs(pixel[2] - 255)) + # min_diff = white_diff + black_diff = (abs(pixel[0] - 0), abs(pixel[1] - 0), abs(pixel[2] - 0)) + + # if the white difference is smaller, that means the pixel is closer + # to white, otherwise, the pixel must be black + if all((white_diff[0] < black_diff[0], white_diff[1] < black_diff[1], white_diff[2] < black_diff[2])): + pixel_bin_rep = "1" + else: + pixel_bin_rep = "0" + + # adding bits + bits += pixel_bin_rep + + return bits