Prianka has uploaded a new change for review. https://gerrit.wikimedia.org/r/181988
Change subject: Porting match_image.py from compat to core/scripts ...................................................................... Porting match_image.py from compat to core/scripts Added match_image.py in core/scripts/ as part of Pywikibot:Compat to Core migration making it compatible with pywikibot module. Needed to make use of external module urllib2 for fetching URLs. Bug T66871 Change-Id: I7aa6cc3e1c97e32d50621418cb9903f35321157f --- A scripts/match_images.py 1 file changed, 209 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/88/181988/1 diff --git a/scripts/match_images.py b/scripts/match_images.py new file mode 100644 index 0000000..7659bcd --- /dev/null +++ b/scripts/match_images.py @@ -0,0 +1,209 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +Program to match two images based on histograms. + +Usage: +match_images.py ImageA ImageB + +This is just a first version so that other people can play around with it. +Expect the code to change a lot! + +""" +# +# (c) Multichill, 2009 +# (c) pywikibot team, 2009-2014 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id$' +# + +import StringIO +import urllib2 +import pywikibot +from PIL import Image + + +def matchImagePages(imagePageA, imagePageB): + """ + This functions expects two image page objects. + It will return True if the image are the same and False if the images are + not the same + + """ + + imageA = getImageFromImagePage(imagePageA) + imageB = getImageFromImagePage(imagePageB) + + (imA_width, imA_height) = imageA.size + (imB_width, imB_height) = imageB.size + + imageB = imageB.resize((imA_width, imA_height)) + + imageA_topleft = imageA.crop((0, 0, int(imA_width / 2), int(imA_height / + 2))) + imageB_topleft = imageB.crop((0, 0, int(imA_width / 2), int(imA_height / + 2))) + + imageA_topright = imageA.crop((int(imA_width / 2), 0, imA_width, + int(imA_height / 2))) + imageB_topright = imageB.crop((int(imA_width / 2), 0, imA_width, + int(imA_height / 2))) + + imageA_bottomleft = imageA.crop((0, int(imA_height / 2), int(imA_width / + 2), + imA_height)) + imageB_bottomleft = imageB.crop((0, int(imA_height / 2), int(imA_width / + 2), + imA_height)) + + imageA_bottomright = imageA.crop((int(imA_width / 2), int(imA_height / 2), + imA_width, imA_height)) + imageB_bottomright = imageB.crop((int(imA_width / 2), int(imA_height / 2), + imA_width, imA_height)) + + imageA_center = imageA.crop((int(imA_width * 0.25), int(imA_height * 0.25), + int(imA_width * 0.75), int(imA_height * 0.75))) + imageB_center = imageB.crop((int(imA_width * 0.25), int(imA_height * 0.25), + int(imA_width * 0.75), int(imA_height * 0.75))) + + wholeScore = matchImages(imageA, imageB) + topleftScore = matchImages(imageA_topleft, imageB_topleft) + toprightScore = matchImages(imageA_topright, imageB_topright) + bottomleftScore = matchImages(imageA_bottomleft, imageB_bottomleft) + bottomrightScore = matchImages(imageA_bottomright, imageB_bottomright) + centerScore = matchImages(imageA_center, imageB_center) + averageScore = (wholeScore + topleftScore + toprightScore + + bottomleftScore + bottomrightScore + centerScore) / 6 + + print u'Whole image ' + str(wholeScore) + print u'Top left of image ' + str(topleftScore) + print u'Top right of image ' + str(toprightScore) + print u'Bottom left of image ' + str(bottomleftScore) + print u'Bottom right of image ' + str(bottomrightScore) + print u'Center of image ' + str(centerScore) + print u' -------------' + print u'Average ' + str(averageScore) + + # Hard coded at 80%, change this later on. + if ((averageScore * 100) > 80): + print u'We have a match!' + return True + else: + print u'Not the same.' + return False + + +def getImageFromImagePage(imagePage): + """ Get the image object to work based on an imagePage object """ + imageURL = imagePage.fileUrl() + imageURLopener = urllib2.urlopen(imageURL) + imageBuffer = StringIO.StringIO(imageURLopener.read()) + image = Image.open(imageBuffer) + + return image + + +def matchImages(imageA, imageB): + """ Match two image objects. Return the ratio of pixels that match """ + histogramA = imageA.histogram() + histogramB = imageB.histogram() + + totalMatch = 0 + totalPixels = 0 + + if not (len(histogramA) == len(histogramB)): + return 0 + + for i in range(0, len(histogramA)): + totalMatch = totalMatch + min(histogramA[i], histogramB[i]) + totalPixels = totalPixels + max(histogramA[i], histogramB[i]) + + if totalPixels == 0: + return 0 + + return float(totalMatch) / float(totalPixels) * 100 + + +def main(): + + # Array of images to work on + images = [] + imageTitleA = u'' + imageTitleB = u'' + familyA = u'' + familyB = u'' + langA = u'' + langB = u'' + imagePageA = None + imagePageB = None + + for arg in pywikibot.handle_args(): + if arg.startswith('-familyA:'): + if len(arg) == len('-familyA:'): + familyA = pywikibot.input(u'What family do you want to use?') + else: + familyA = arg[len('-familyA:'):] + elif arg.startswith('-familyB:'): + if len(arg) == len('-familyB:'): + familyB = pywikibot.input(u'What family do you want to use?') + else: + familyB = arg[len('-familyB:'):] + elif arg.startswith('-langA:'): + if len(arg) == len('-langA:'): + langA = pywikibot.input(u'What language do you want to use?') + else: + langA = arg[len('-langA:'):] + elif arg.startswith('-langB:'): + if len(arg) == len('-langB:'): + langB = pywikibot.input(u'What language do you want to use?') + else: + langB = arg[len('langB:'):] + else: + images.append(arg) + + if not (len(images) == 2): + raise pywikibot.Error, 'require two images to work on.' + else: + imageTitleA = images[0] + imageTitleB = images[1] + + if not (imageTitleA == u''): + if not (langA == u''): + if not (familyA == u''): + imagePageA = pywikibot.page.FilePage(pywikibot.Site( + langA, familyA), + imageTitleA) + else: + imagePageA = pywikibot.page.FilePage(pywikibot.Site( + langA, u'wikipedia'), + imageTitleA) + else: + imagePageA = pywikibot.page.FilePage(pywikibot.Site( + u'commons', + u'commons'), + imageTitleA) + + if not (imageTitleB == u''): + if not (langB == u''): + if not (familyB == u''): + imagePageB = pywikibot.page.FilePage(pywikibot.Site( + langB, familyB), + imageTitleB) + else: + imagePageB = pywikibot.page.FilePage(pywikibot.Site( + langB, u'wikipedia'), + imageTitleB) + else: + imagePageB = pywikibot.page.FilePage(pywikibot.Site( + u'commons', + u'commons'), + imageTitleB) + + if imagePageA and imagePageB: + matchImagePages(imagePageA, imagePageB) + + +if __name__ == "__main__": + main() -- To view, visit https://gerrit.wikimedia.org/r/181988 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I7aa6cc3e1c97e32d50621418cb9903f35321157f Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Prianka <priyankajayaswal...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits