Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Download an image if it matches another image

$
0
0

In this post, we would be looking at two things:

How to download images in python How to check whether an image is same as another one

Let’s start. A while back, I was experimenting with some stuff and I need to check whether images in an online repository are the same as the image I’ve’ if yes, then dont download it, else download it. Obviously, I dont need the same images again and again.

The image in discussion is this.


Download an image if it matches another image

InputSamplecontains links that need to be downloaded as images and then checked with the above image. Another column is to be added that will indicate whether the images downloaded is the same as this above image or not.

fromPILimportImageChops fromPILimportImage importshutil importrequests importcsv defdownloadImage(url): filename = url.split('/')[-1] response = requests.get(url, stream=True) withopen(filename, 'wb') as out_file: shutil.copyfileobj(response.raw, out_file) delresponse return filename defequal(imageName): im1 = Image.open(imageName) im2 = Image.open('1199W1427.jpg') try: return ImageChops.difference(im1, im2).getbbox() is None except: return False f = open('InputSample.csv', 'rb') data = f.read().split('\n')[:-1] data = [d.strip() for d in data] o = open('outputFile.csv', 'wb') writer = csv.writer(o) writer.writerow(['url', 'imageName', 'sameImage']) for linein data: try: imageName = downloadImage(line) sameImage = equal(imageName) writer.writerow([line, imageName, sameImage]) except: pass f.close() o.close()

Happy Python-ing!


Viewing all articles
Browse latest Browse all 9596

Trending Articles