我试图将一张图片与一个充满图片的文件夹进行比较,并试图找到一个相等的图片,但我想不出如何将一张图片与一个充满图片的文件夹进行比较
我试着用fnmatch和os做了一个listOfFiles,但它只需要几张图片中的一张。
#import
import cv2
import numpy as np
import os, fnmatch
#Collects all images
listOfFiles = os.listdir('./images')
pattern = "*.jpg"
for entry in listOfFiles:
if fnmatch.fnmatch(entry, pattern):
Allimages = ("images/" + entry)
#Define variables
upload = cv2.imread("images/img1.jpg")
duplicate = cv2.imread(Allimages)
#Checks if duplicate is duplicate
if upload.shape == duplicate.shape:
print("The images have same size and channels")
difference = cv2.subtract(upload, duplicate)
b, g, r = cv2.split(difference)
if cv2.countNonZero(b) == 0 and cv2.countNonZero (g) == 0 and cv2.countNonZero(r) == 0:
print("images are the same")
else:
print("images are different")
发布于 2019-07-22 00:18:20
你必须在for
循环中做所有的事情。
也许您可以仅使用以下命令比较图像
if (upload == duplicate).all():
代码:
import cv2
import os
directory = './images'
upload = cv2.imread("images/img1.jpg")
for entry in os.listdir(directory):
if entry.lower().endswith( ('.jpg', '.jpeg', '.png', '.gif') ):
fullname = os.path.join(directory, entry)
print('fullname:', fullname)
duplicate = cv2.imread(fullname)
if upload.shape == duplicate.shape:
print("The images have same size and channels")
#difference = cv2.subtract(upload, duplicate)
#b, g, r = cv2.split(difference)
#if cv2.countNonZero(b) == 0 and cv2.countNonZero(g) == 0 and cv2.countNonZero(r) == 0:
if (upload == duplicate).all():
print("images are the same")
else:
print("images are different")
https://stackoverflow.com/questions/57134814
复制相似问题