Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ImageComparer(object):
- def __init__(self, image_one, image_two):
- if os.path.exists('/mnt/ramdisk'):
- self.tmp_folder = '/mnt/ramdisk'
- else:
- self.tmp_folder = tempfile.gettempdir()
- self.work = {}
- self.work.update({'original_one': {'path': image_one}})
- self.work.update({'original_two': {'path': image_two}})
- self.delete_us = []
- def reload_image_as(self, load_type, **kwargs):
- """
- reuses self.work by reseting it back to "starting" state
- if 'move' in kwargs, before popping each key they're stored into self.work['move']
- :param load_type: 'rgb', 'gray'
- :param kwargs: str move
- """
- self.type = load_type
- all_keys = []
- for string,_ in self.work.items():
- all_keys.append(string)
- for string in all_keys:
- if string != 'original_one' and string != 'original_two':
- if string != 'rgb' and string != 'gray':
- if 'move' in kwargs:
- if kwargs['move'] not in self.work:
- self.work.update({kwargs['move']: {}})
- self.work[kwargs['move']].update({string: self.work[string]})
- self.work.pop(string)
- if 'only_move' not in kwargs:
- if self.type == 'rgb':
- self.work['original_one']['image'] = Image.open(self.work['original_one']['path'])
- self.work['original_two']['image'] = Image.open(self.work['original_two']['path'])
- elif self.type == 'gray':
- self.work['original_one']['image'] = Image.open(self.work['original_one']['path']).convert('LA')
- self.work['original_two']['image'] = Image.open(self.work['original_two']['path']).convert('LA')
- self.delete_us.append(self.work['original_one']['image'])
- self.delete_us.append(self.work['original_two']['image'])
- def make_slices(self, **kwargs):
- """
- takes self.work['resized_one/two']['image'] and slices them up in 12 slices
- if highjacked, data of interest is in self.work['slices'][count]['rgb_image']
- :param kwargs['no_slice'] == makes 12 slices, but all are the same/entire image
- """
- if 'resized_one' not in self.work or 'resized_two' not in self.work:
- self.make_images_same_size(**kwargs)
- if 'slices' not in self.work:
- self.work.update({'slices': {}})
- else:
- return
- loads = [self.work['resized_one']['image'], self.work['resized_two']['image']]
- for c in range(len(loads)):
- if c not in self.work['slices']:
- self.work['slices'].update({c: {}})
- image = loads[c]
- im_width = image.size[0]
- im_height = image.size[1]
- left = 0
- top = 0
- right = (im_width / 3) - 1
- bottom = (im_height / 4) - 1
- for cc in range(12):
- if cc not in self.work['slices'][c]:
- self.work['slices'][c].update({cc: {}})
- if cc > 0:
- if right + (im_width / 3) - 1 > im_width:
- left = 0
- top += (im_height / 4) - 1
- right = (im_width / 3) - 1
- bottom += (im_height / 4) - 1
- else:
- left += (im_width / 3) - 1
- right += (im_width / 3) - 1
- if 'no_slice' in kwargs:
- cropped_image = image
- else:
- cropped_image = image.crop((left,top,right,bottom))
- self.delete_us.append(cropped_image)
- self.work['slices'][c][cc].update({'image': cropped_image})
- def make_images_same_size(self, **kwargs):
- """
- takes the smallest of the images and make both of them that size
- if highjacked you're interested in self.work['resized_one/two']['image'] & ['path']
- """
- im1 = self.work['original_one']['image']
- im2 = self.work['original_two']['image']
- if im1.size[0] * im1.size[1] > im2.size[0] * im2.size[1]:
- size = im2.size[0], im2.size[1]
- else:
- size = im1.size[0], im1.size[1]
- self.work.update({'resized_one': {'image': im1.resize(size=size, resample=Image.LANCZOS)}})
- self.work.update({'resized_two': {'image': im2.resize(size=size, resample=Image.LANCZOS)}})
- self.work['resized_one'].update({'path': self.tmp_folder + '/resized_one.webp'})
- self.work['resized_two'].update({'path': self.tmp_folder + '/resized_two.webp'})
- self.work['resized_one']['image'].save(self.work['resized_one']['path'], 'webp', quality=50, optimize=False)
- self.work['resized_two']['image'].save(self.work['resized_two']['path'], 'webp', quality=50, optimize=False)
- self.delete_us.append(self.work['resized_one']['image'])
- self.delete_us.append(self.work['resized_two']['image'])
- def fast_fill_values(self, **kwargs):
- """
- injects self.work['slices'][count][count]['colors']
- injects self.work['slices'][count][count]['len_colors']
- injects self.work['slices'][count][count]['histogram']
- injects self.work['slices'][count][count]['entropy']
- """
- self.make_slices(**kwargs)
- cdef list all_histogram = []
- cdef list all_entropy = []
- cdef list all_len_colors = []
- cdef list hist_diff, slicelist, listofvalues, shrink
- cdef dict cd
- cdef float final_slice_percent, each_percent, add_values
- cdef float one_size, two_size
- cdef int c, cc, ccc, cccc
- cdef int maxcolors, tmp_hist
- cdef str stringkey
- for c in range(2):
- for cc in range(len(self.work['slices'][c])):
- slice = self.work['slices'][c][cc]
- if 'colors' not in slice:
- maxcolors = 16800
- colors = slice['image'].getcolors(maxcolors=16800)
- while colors == None:
- maxcolors = maxcolors * 2
- colors = slice['image'].getcolors(maxcolors=maxcolors)
- if maxcolors > 10000000000:
- break
- slice.update({'colors': colors})
- slice.update({'len_colors': len(colors)})
- if 'histogram' not in slice:
- histogram = slice['image'].histogram()
- slice.update({'histogram': histogram})
- if 'entropy' not in slice:
- entropy = slice['image'].entropy()
- slice.update({'entropy': entropy})
- for c in range(1):
- for cc in range(len(self.work['slices'][c])):
- slicelist = [self.work['slices'][0][cc], self.work['slices'][1][cc]]
- shrink_count = len(slice['histogram']) / 32
- for slice in slicelist:
- shrink = []
- while slice['histogram'] != []:
- tmp_hist = 0
- for ccc in range(int(shrink_count)):
- if slice['histogram'] == []:
- break
- tmp_hist += slice['histogram'][-1]
- slice['histogram'].pop(-1)
- shrink.append(tmp_hist)
- slice['histogram'] = shrink
- hist_diff = []
- for ccc in range(len(slicelist[0]['histogram'])):
- try:
- if slicelist[0]['histogram'][ccc] < slicelist[1]['histogram'][ccc]:
- hist_diff.append(slicelist[0]['histogram'][ccc] / slicelist[1]['histogram'][ccc])
- elif slicelist[0]['histogram'][ccc] > slicelist[1]['histogram'][ccc]:
- hist_diff.append(slicelist[1]['histogram'][ccc] / slicelist[0]['histogram'][ccc])
- else:
- hist_diff.append(1)
- except IndexError:
- pass
- final_slice_percent = 0.0
- for each_percent in hist_diff:
- final_slice_percent += each_percent
- all_histogram.append(final_slice_percent / len(hist_diff))
- for cccc in range(2):
- slicelist[cccc].update({'history_percent_off': round(final_slice_percent/len(hist_diff), 2)})
- cd = {
- 'entropy_percent_off': ('entropy', all_entropy),
- 'len_colors_percent_off': ('len_colors', all_len_colors),
- }
- for c in range(1):
- for cc in range(len(self.work['slices'][c])):
- slicelist = [self.work['slices'][0][cc], self.work['slices'][1][cc]]
- for stringkey, guidetuple in cd.items():
- key = guidetuple[0]
- if slicelist[0][key] < slicelist[1][key]:
- guidetuple[1].append(slicelist[0][key] / slicelist[1][key])
- for ccc in range(2):
- slicelist[ccc].update({stringkey: round(slicelist[0][key] / slicelist[1][key], 2)})
- elif slicelist[0][key] > slicelist[1][key]:
- guidetuple[1].append(slicelist[1][key] / slicelist[0][key])
- for ccc in range(2):
- slicelist[ccc].update({stringkey: round(slicelist[1][key] / slicelist[0][key], 2)})
- else:
- for ccc in range(2):
- slicelist[ccc].update({stringkey: 1.0})
- one_size = os.path.getsize(self.work['resized_one']['path'])
- two_size = os.path.getsize(self.work['resized_two']['path'])
- if one_size < two_size:
- self.work.update({'size_percent_off': round(one_size / two_size, 2)})
- else:
- self.work.update({'size_percent_off': round(two_size / one_size, 2)})
- cd = {
- 'entropy_percent_off': all_entropy,
- 'history_percent_off': all_histogram,
- 'len_colors_percent_off': all_len_colors,
- }
- for stringkey, listofvalues in cd.items():
- add_values = 0
- for value in listofvalues:
- add_values += value
- if add_values > 0:
- self.work.update({stringkey: round(add_values / len(listofvalues), 2)})
- else:
- self.work.update({stringkey: 1})
RAW Paste Data