jenkins-bot has submitted this change and it was merged.
Change subject: Porting match_images.py from compat to core/scripts ......................................................................
Porting match_images.py from compat to core/scripts
Added match_images.py in core/scripts/ as part of Pywikibot:Compat to Core migration making it compatible with pywikibot module. Needed to make use of http from pywikibot.comms for fetching URLs.
Bug T66871 Change-Id: I7aa6cc3e1c97e32d50621418cb9903f35321157f --- A scripts/match_images.py 1 file changed, 191 insertions(+), 0 deletions(-)
Approvals: Ladsgroup: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/match_images.py b/scripts/match_images.py new file mode 100644 index 0000000..9a8204c --- /dev/null +++ b/scripts/match_images.py @@ -0,0 +1,191 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +""" +Program to match two images based on histograms. + +Usage: +match_images.py ImageA ImageB +It is essential to provide two images to work on. +example. - match_images.py ImageA.jpg ImageB.jpg + +¶ms; + +Furthermore, the following command line parameters are supported: + +-otherfamily Mentioned family with this parameter will be preferred for + fetching file usage details instead of the default + family retrieved from user-congig.py script. + +-otherlang Mentioned lang with this parameter will be preferred for + fetching file usage details instead of the default + mylang retrieved from user-congig.py script. + +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-2015 +# +# Distributed under the terms of the MIT license. +# +from __future__ import division +__version__ = '$Id$' + + +import io +from PIL import Image + +import pywikibot +from pywikibot.comms import http + + +def match_image_pages(imagePageA, imagePageB): + """The function 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 = get_image_from_image_page(imagePageA) + imageB = get_image_from_image_page(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, imA_width // 2, imA_height // 2)) + imageB_topleft = imageB.crop((0, 0, imA_width // 2, imA_height // 2)) + + imageA_topright = imageA.crop((imA_width // 2, 0, imA_width, + imA_height // 2)) + imageB_topright = imageB.crop((imA_width // 2, 0, imA_width, + imA_height // 2)) + + imageA_bottomleft = imageA.crop((0, imA_height // 2, imA_width // 2, + imA_height)) + imageB_bottomleft = imageB.crop((0, imA_height // 2, imA_width // 2, + imA_height)) + + imageA_bottomright = imageA.crop((imA_width // 2, imA_height // 2, + imA_width, imA_height)) + imageB_bottomright = imageB.crop((imA_width // 2, 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 = match_images(imageA, imageB) + topleftScore = match_images(imageA_topleft, imageB_topleft) + toprightScore = match_images(imageA_topright, imageB_topright) + bottomleftScore = match_images(imageA_bottomleft, imageB_bottomleft) + bottomrightScore = match_images(imageA_bottomright, imageB_bottomright) + centerScore = match_images(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 > 0.8: + print (u'We have a match!') + return True + else: + print (u'Not the same.') + return False + + +def get_image_from_image_page(imagePage): + """ Get the image object to work based on an imagePage object. """ + imageBuffer = None + imageURL = imagePage.fileUrl() + imageURLopener = http.fetch(imageURL) + imageBuffer = io.BytesIO(imageURLopener.raw[:]) + image = Image.open(imageBuffer) + return image + + +def match_images(imageA, imageB): + """ Match two image objects. Return the ratio of pixels that match. """ + histogramA = imageA.histogram() + histogramB = imageB.histogram() + + totalMatch = 0 + totalPixels = 0 + + if 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 totalMatch / totalPixels * 100 + + +def main(*args): + """ Extracting file page information of images to work on and initiate matching. """ + images = [] + other_family = u'' + other_lang = u'' + imagePageA = None + imagePageB = None + + # Read commandline parameters. + local_args = pywikibot.handle_args(args) + + for arg in local_args: + if arg.startswith('-otherfamily:'): + if len(arg) == len('-otherfamily:'): + other_family = pywikibot.input(u'What family do you want to use?') + else: + other_family = arg[len('-otherfamily:'):] + elif arg.startswith('-otherlang:'): + if len(arg) == len('-otherlang:'): + other_lang = pywikibot.input(u'What language do you want to use?') + else: + other_lang = arg[len('otherlang:'):] + else: + images.append(arg) + + if len(images) != 2: + pywikibot.showHelp('match_images') + pywikibot.error('Require two images to work on.') + return + + else: + pass + + imagePageA = pywikibot.page.FilePage(pywikibot.Site(), + images[0]) + if other_lang: + if other_family: + imagePageB = pywikibot.page.FilePage(pywikibot.Site( + other_lang, other_family), + images[1]) + else: + imagePageB = pywikibot.page.FilePage(pywikibot.Site( + other_lang), + images[1]) + else: + imagePageB = pywikibot.page.FilePage(pywikibot.Site(), + images[1]) + + match_image_pages(imagePageA, imagePageB) + + +if __name__ == "__main__": + main()
pywikibot-commits@lists.wikimedia.org