Last active
August 29, 2015 14:05
-
-
Save whosaysni/0f1e34d98ff330d0db14 to your computer and use it in GitHub Desktop.
Compare images in two directories, showing difference/ディレクトリを探索して画像を比較し、差分画像を出力する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
from __future__ import print_function | |
from os import walk | |
from os.path import join, splitext | |
from PIL import Image, ImageChops | |
def compare_images(dir1, dir2): | |
for dirname, subdirnames, filenames in walk(dir1): | |
for filename in filenames: | |
path1 = join(dirname, filename) | |
path2 = dir2+path1[len(dir1):] | |
body, ext = splitext(path1) | |
if ext.lower()[1:] in ['jpg', 'png', 'gif']: | |
img1 = Image.open(path1) | |
img2 = Image.open(path2) | |
if not img1.tobytes()==img2.tobytes(): | |
print('Image %s and %s are not identical.' %(path1, path2)) | |
(w1, h1), (w2, h2) = img1.size, img2.size | |
w, h = max(w1, w2), max(h1, h2) | |
d = Image.new('RGB', (w, h*3)) | |
d.paste(img1, (0, h*0)) | |
d.paste(img2, (0, h*1)) | |
d.paste( | |
ImageChops.subtract_modulo(img1.convert('L'), img2.convert('L')).convert('RGB'), | |
(0, h*2)) | |
d.show() | |
if __name__=='__main__': | |
import sys | |
from argparse import ArgumentParser, Action | |
from os.path import isdir | |
parser = ArgumentParser() | |
parser.add_argument('dir1') | |
parser.add_argument('dir2') | |
parsed = parser.parse_args() | |
for argname in ['dir1', 'dir2']: | |
if not isdir(getattr(parsed, argname)): | |
sys.stderr.write('%sERROR: %s not exists.\n' %(parser.format_usage(), argname)) | |
sys.exit(-1) | |
compare_images(parsed.dir1, parsed.dir2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment