mirror of
https://github.com/arsenetar/dupeguru.git
synced 2024-11-18 04:59:03 +00:00
d1f460b091
--HG-- rename : py/__init__.py => base/py/__init__.py rename : py/app.py => base/py/app.py rename : py/app_cocoa.py => base/py/app_cocoa.py rename : py/app_me_cocoa.py => base/py/app_me_cocoa.py rename : py/app_pe_cocoa.py => base/py/app_pe_cocoa.py rename : py/app_se_cocoa.py => base/py/app_se_cocoa.py rename : py/data.py => base/py/data.py rename : py/data_me.py => base/py/data_me.py rename : py/data_pe.py => base/py/data_pe.py rename : py/directories.py => base/py/directories.py rename : py/engine.py => base/py/engine.py rename : py/export.py => base/py/export.py rename : py/gen.py => base/py/gen.py rename : py/ignore.py => base/py/ignore.py rename : py/modules/block/block.pyx => base/py/modules/block/block.pyx rename : py/modules/block/setup.py => base/py/modules/block/setup.py rename : py/modules/cache/cache.pyx => base/py/modules/cache/cache.pyx rename : py/modules/cache/setup.py => base/py/modules/cache/setup.py rename : py/picture/__init__.py => base/py/picture/__init__.py rename : py/picture/block.py => base/py/picture/block.py rename : py/picture/cache.py => base/py/picture/cache.py rename : py/picture/matchbase.py => base/py/picture/matchbase.py rename : py/results.py => base/py/results.py rename : py/scanner.py => base/py/scanner.py rename : py/tests/__init__.py => base/py/tests/__init__.py rename : py/tests/app_cocoa_test.py => base/py/tests/app_cocoa_test.py rename : py/tests/app_test.py => base/py/tests/app_test.py rename : py/tests/block_test.py => base/py/tests/block_test.py rename : py/tests/cache_test.py => base/py/tests/cache_test.py rename : py/tests/directories_test.py => base/py/tests/directories_test.py rename : py/tests/engine_test.py => base/py/tests/engine_test.py rename : py/tests/export_test.py => base/py/tests/export_test.py rename : py/tests/ignore_test.py => base/py/tests/ignore_test.py rename : py/tests/results_test.py => base/py/tests/results_test.py rename : py/tests/scanner_test.py => base/py/tests/scanner_test.py extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%4074
93 lines
3.2 KiB
Cython
93 lines
3.2 KiB
Cython
# Created By: Virgil Dupras
|
|
# Created On: 2009-04-23
|
|
# $Id$
|
|
# Copyright 2009 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
cdef extern from "stdlib.h":
|
|
int abs(int n) # required so that abs() is applied on ints, not python objects
|
|
|
|
class NoBlocksError(Exception):
|
|
"""avgdiff/maxdiff has been called with empty lists"""
|
|
|
|
class DifferentBlockCountError(Exception):
|
|
"""avgdiff/maxdiff has been called with 2 block lists of different size."""
|
|
|
|
|
|
cdef object getblock(object image):
|
|
"""Returns a 3 sized tuple containing the mean color of 'image'.
|
|
|
|
image: a PIL image or crop.
|
|
"""
|
|
cdef int pixel_count, red, green, blue, r, g, b
|
|
if image.size[0]:
|
|
pixel_count = image.size[0] * image.size[1]
|
|
red = green = blue = 0
|
|
for r, g, b in image.getdata():
|
|
red += r
|
|
green += g
|
|
blue += b
|
|
return (red // pixel_count, green // pixel_count, blue // pixel_count)
|
|
else:
|
|
return (0, 0, 0)
|
|
|
|
def getblocks2(image, int block_count_per_side):
|
|
"""Returns a list of blocks (3 sized tuples).
|
|
|
|
image: A PIL image to base the blocks on.
|
|
block_count_per_side: This integer determine the number of blocks the function will return.
|
|
If it is 10, for example, 100 blocks will be returns (10 width, 10 height). The blocks will not
|
|
necessarely cover square areas. The area covered by each block will be proportional to the image
|
|
itself.
|
|
"""
|
|
if not image.size[0]:
|
|
return []
|
|
cdef int width, height, block_width, block_height, ih, iw, top, bottom, left, right
|
|
width, height = image.size
|
|
block_width = max(width // block_count_per_side, 1)
|
|
block_height = max(height // block_count_per_side, 1)
|
|
result = []
|
|
for ih in range(block_count_per_side):
|
|
top = min(ih * block_height, height - block_height)
|
|
bottom = top + block_height
|
|
for iw in range(block_count_per_side):
|
|
left = min(iw * block_width, width - block_width)
|
|
right = left + block_width
|
|
box = (left, top, right, bottom)
|
|
crop = image.crop(box)
|
|
result.append(getblock(crop))
|
|
return result
|
|
|
|
cdef int diff(first, second):
|
|
"""Returns the difference between the first block and the second.
|
|
|
|
It returns an absolute sum of the 3 differences (RGB).
|
|
"""
|
|
cdef int r1, g1, b1, r2, g2, b2
|
|
r1, g1, b1 = first
|
|
r2, g2, b2 = second
|
|
return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2)
|
|
|
|
def avgdiff(first, second, int limit, int min_iterations):
|
|
"""Returns the average diff between first blocks and seconds.
|
|
|
|
If the result surpasses limit, limit + 1 is returned, except if less than min_iterations
|
|
iterations have been made in the blocks.
|
|
"""
|
|
cdef int count, sum, i, iteration_count
|
|
count = len(first)
|
|
if count != len(second):
|
|
raise DifferentBlockCountError()
|
|
if not count:
|
|
raise NoBlocksError()
|
|
sum = 0
|
|
for i in range(count):
|
|
iteration_count = i + 1
|
|
item1 = first[i]
|
|
item2 = second[i]
|
|
sum += diff(item1, item2)
|
|
if sum > limit * iteration_count and iteration_count >= min_iterations:
|
|
return limit + 1
|
|
result = sum // count
|
|
if (not result) and sum:
|
|
result = 1
|
|
return result |