ともたこさんのコード(2019-11-06 05:09:25.347157)

import argparse
import glob
import os
from os import path
from pathlib import Path
import cv2
from skimage import measure
#import imutils

parser = argparse.ArgumentParser(description="Move duplicated images")
parser.add_argument("dir", metavar="dir", nargs=1, help="src directory")
parser.add_argument("-o", metavar="outdir", nargs=1, required=True, help="output directory")
parser.add_argument("-t", metavar="threshold", nargs=1, help="MSE threshold", default="3.0")
parser.add_argument("-n", action="store_true", help="no execution")

args = parser.parse_args()
pathDir = args.dir[0]
pathDirOutput = args.o[0]
thresholdMse = float(args.t[0])

print("dir={0}, outdir={1}".format(pathDir, pathDirOutput));
p = Path(pathDir + "/" + pathDirOutput)
if not p.exists():
    p.mkdir(parents=True)
    print("Created: " + pathDir + "/" + pathDirOutput);
else:
    print(pathDirOutput + "already exists");


imagePrev = None
filePrev = ""
indexDup = 0
for file in sorted(glob.glob(pathDir + "/*.png"), key=str.lower):
#	nameFile = path.basename(file)
#	print(file)
	image = cv2.imread(file)
	imageShrunk = cv2.resize(image, fx=0.5, fy=0.5, dsize=(0, 0), interpolation=cv2.INTER_AREA)
	if imagePrev is None:
		imagePrev = imageShrunk
		filePrev = file
		print("imagePrev is none.")
		continue
	mse = measure.compare_mse(imagePrev, imageShrunk)
	changed = False
	if thresholdMse < mse:
		changed = True

	print("Difference between {0} and {1}: mse={2}, changed={3}".format(filePrev, file, mse, changed))
	if changed:
		imagePrev = imageShrunk
		filePrev = file
		indexDup = 1
	else:
		pathDir = path.dirname(filePrev)
		nameFile = path.basename(filePrev)
		(body, ext) = path.splitext(nameFile)
		prefix = ""
		if args.n:
			prefix="(No exec) "
		newFile = "{0}/{1}/{2}_{3}{4}".format(pathDir, pathDirOutput, body, indexDup, ext)
		print(prefix + "Moving {0} to {1}".format(file, newFile))
		if not args.n:
			os.rename(file, newFile)
		indexDup += 1