hydrus/include/ClientImageHandling.py

132 lines
3.8 KiB
Python
Raw Normal View History

2015-11-18 22:44:07 +00:00
import numpy.core.multiarray # important this comes before cv!
import cv2
import HydrusImageHandling
2016-04-27 19:20:37 +00:00
import HydrusGlobals
2015-11-18 22:44:07 +00:00
if cv2.__version__.startswith( '2' ):
IMREAD_UNCHANGED = cv2.CV_LOAD_IMAGE_UNCHANGED
else:
IMREAD_UNCHANGED = cv2.IMREAD_UNCHANGED
def EfficientlyResizeNumpyImage( numpy_image, ( target_x, target_y ) ):
( im_y, im_x, depth ) = numpy_image.shape
if target_x >= im_x and target_y >= im_y: return numpy_image
# this seems to slow things down a lot, at least for cv!
#if im_x > 2 * target_x and im_y > 2 * target_y: result = cv2.resize( numpy_image, ( 2 * target_x, 2 * target_y ), interpolation = cv2.INTER_NEAREST )
2016-04-14 01:54:29 +00:00
return cv2.resize( numpy_image, ( target_x, target_y ), interpolation = cv2.INTER_AREA )
2015-11-18 22:44:07 +00:00
def EfficientlyThumbnailNumpyImage( numpy_image, ( target_x, target_y ) ):
( im_y, im_x, depth ) = numpy_image.shape
if target_x >= im_x and target_y >= im_y: return numpy_image
( target_x, target_y ) = HydrusImageHandling.GetThumbnailResolution( ( im_x, im_y ), ( target_x, target_y ) )
return cv2.resize( numpy_image, ( target_x, target_y ), interpolation = cv2.INTER_AREA )
def GenerateNumpyImage( path ):
2016-05-11 18:16:39 +00:00
# this used to be a regular cv.imread call, but it was crashing the whole process on random thumbs, hooray
# it was just the read that was the problem, so this seems to work fine, even if pil is only about half as fast
2016-04-27 19:20:37 +00:00
2016-05-11 18:16:39 +00:00
pil_image = HydrusImageHandling.GeneratePILImage( path )
2015-11-18 22:44:07 +00:00
2016-05-11 18:16:39 +00:00
numpy_image = GenerateNumPyImageFromPILImage( pil_image )
2015-11-18 22:44:07 +00:00
return numpy_image
def GenerateNumPyImageFromPILImage( pil_image ):
2016-06-29 19:55:46 +00:00
pil_image = HydrusImageHandling.Dequantize( pil_image )
2015-11-18 22:44:07 +00:00
( w, h ) = pil_image.size
s = pil_image.tobytes()
return numpy.fromstring( s, dtype = 'uint8' ).reshape( ( h, w, len( s ) // ( w * h ) ) )
def GeneratePerceptualHash( path ):
2016-05-11 18:16:39 +00:00
numpy_image = GenerateNumpyImage( path )
2015-11-18 22:44:07 +00:00
( y, x, depth ) = numpy_image.shape
if depth == 4:
# create weight and transform numpy_image to greyscale
numpy_alpha = numpy_image[ :, :, 3 ]
2016-08-24 18:36:56 +00:00
numpy_alpha_float = numpy_alpha / 255.0
2015-11-18 22:44:07 +00:00
2016-08-24 18:36:56 +00:00
numpy_image_bgr = numpy_image[ :, :, :3 ]
2015-11-18 22:44:07 +00:00
2016-08-24 18:36:56 +00:00
numpy_image_gray_bare = cv2.cvtColor( numpy_image_bgr, cv2.COLOR_BGR2GRAY )
2015-11-18 22:44:07 +00:00
2016-08-24 18:36:56 +00:00
# create a white greyscale canvas
2015-11-18 22:44:07 +00:00
2016-08-24 18:36:56 +00:00
white = numpy.ones( ( y, x ) ) * 255.0
2015-11-18 22:44:07 +00:00
2016-08-24 18:36:56 +00:00
# paste the grayscale image onto the white canvas using: pixel * alpha + white * ( 1 - alpha )
2015-11-18 22:44:07 +00:00
2016-08-24 18:36:56 +00:00
numpy_image_gray = numpy.uint8( ( numpy_image_gray_bare * numpy_alpha_float ) + ( white * ( numpy.ones( ( y, x ) ) - numpy_alpha_float ) ) )
2015-11-18 22:44:07 +00:00
else:
numpy_image_gray = cv2.cvtColor( numpy_image, cv2.COLOR_BGR2GRAY )
numpy_image_tiny = cv2.resize( numpy_image_gray, ( 32, 32 ), interpolation = cv2.INTER_AREA )
# convert to float and calc dct
numpy_image_tiny_float = numpy.float32( numpy_image_tiny )
dct = cv2.dct( numpy_image_tiny_float )
# take top left 8x8 of dct
dct_88 = dct[:8,:8]
# get mean of dct, excluding [0,0]
mask = numpy.ones( ( 8, 8 ) )
mask[0,0] = 0
average = numpy.average( dct_88, weights = mask )
# make a monochromatic, 64-bit hash of whether the entry is above or below the mean
bytes = []
for i in range( 8 ):
byte = 0
for j in range( 8 ):
byte <<= 1 # shift byte one left
value = dct_88[i,j]
if value > average: byte |= 1
bytes.append( byte )
answer = str( bytearray( bytes ) )
# we good
return answer