handling of transparancy
smason opened this issue · 2 comments
I've just been playing with your library and it kept telling me that images were very different when they looked the same. It seems that it was due to the color present in transparent areas.
For example; if I create two images as:
from PIL import Image, ImageDraw
# greyscale image, completely white and also completely transparent
im1 = Image.new('LA', (100, 100), (0xff, 0))
im2 = im1.copy()
# replace most of it with "transparent black"
ImageDraw.Draw(im2).rectangle((10, 10, 90, 90), (0, 0))
and then run dhash on them:
from dhash import dhash_row_col
print(dhash_row_col(im1), dhash_row_col(im2))
I get very different results, even though they'll be rendered the same as far as most viewers are concerned. They're obviously "different" images and you can use an image editor to pull the channels apart and show this. But given that this library is about perceptual differences I think it should probably do something about this.
I'm working around this by filling transparent areas with white, as suggested in:
def fill_transparent(image, fill_color='white'):
if image.mode in ('RGBA', 'LA'):
cleaned = Image.new(image.mode[:-1], image.size, fill_color)
cleaned.paste(image, image.split()[-1])
return cleaned
return image
print(*[dhash_row_col(fill_transparent(im)) for im in [im1, im2]])
Not sure if this is deliberate, or if/how it should be exposed to users of the library — hence opening an issue rather than pull request.
This is an interesting issue, thanks. Yeah, I think putting that conversion logic inside get_grays
would make sense. I probably won't get to this anytime soon, but if you'd like to submit a PR for it, that'd be good. It be good if it also worked for wand/ImageMagick.
OK, I'll see what I can do. I've forked a copy that I've done the PIL stuff in.
I've not used Wand before, so might take a while to figure the API out
Will submit a pull request when I've got something sensible together…