Speedup by 40-45%

This commit is contained in:
Theelgirl 2020-10-17 15:33:12 +00:00 committed by GitHub
parent db8e45547c
commit 048cf66e99
1 changed files with 12 additions and 10 deletions

View File

@ -1,12 +1,13 @@
# distutils: language=c++
# distutils: language=c
# cython: boundscheck=False
# cython: cdivision=True
# cython: wraparound=False
cpdef str cy_get_bits_from_image(image):
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
cdef (int, int, int) pixel, white_diff, black_diff
cdef (bint, bint, bint) truth_tuple
width, height = image.size
@ -17,13 +18,13 @@ cpdef str cy_get_bits_from_image(image):
for x in range(width):
pixel = px[x, y]
pixel_bin_rep = "0"
pixel_bin_rep = <str>"0"
# for exact matches
# for exact matches, indexing each pixel individually is faster in cython for some reason
if pixel[0] == 255 and pixel[1] == 255 and pixel[2] == 255:
pixel_bin_rep = "1"
pixel_bin_rep = <str>"1"
elif pixel[0] == 0 and pixel[1] == 0 and pixel[2] == 0:
pixel_bin_rep = "0"
pixel_bin_rep = <str>"0"
else:
white_diff = (abs(pixel[0] - 255), abs(pixel[1] - 255), abs(pixel[2] - 255))
# min_diff = white_diff
@ -31,10 +32,11 @@ cpdef str cy_get_bits_from_image(image):
# 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"
truth_tuple = (white_diff[0] < black_diff[0], white_diff[1] < black_diff[1], white_diff[2] < black_diff[2])
if all(truth_tuple):
pixel_bin_rep = <str>"1"
else:
pixel_bin_rep = "0"
pixel_bin_rep = <str>"0"
# adding bits
bits += pixel_bin_rep