import csv
import hashlib
import os
from tkinter import *
import numpy as np
import requests
from PIL import Image
import xlwt
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0)like Gecko)'}
class image():
def baidu_img(self, keword, num):
base_url = 'https://image.baidu.com/search/index?tn=baiduimage&ie=utf-8&word={}'.format(keword)
path1 = r"E:\ picture \\" + keword
y = os.path.exists(path1)
if y == 0:
os.mkdir(path1)
else:
pass
response = requests.get(base_url, headers=headers)
html_str = response.text
# html = parsel.Selector(html_str)
# img_href = html.xpath('//li/div/a/img/@src').extract() # utilize xpath Extract picture path
pic_url = re.findall('"objURL":"(.*?)",', html_str, re.S) # Use regular expressions to find pictures url
# print(pic_url)
n = 0
for i in pic_url:
try:
img = requests.get(i, headers=headers).content
img_name = i.split('=')[-1]
with open(path1 + '\\' + img_name + '.jpg', 'wb')as f:
f.write(img)
n = n + 1
with open(path1 + '.csv', 'a', newline='')as ff:
csvwriter = csv.writer(ff, dialect='excel')
csvwriter.writerow([img_name, i])
if n >= num:
break
except Exception as e:
print(e)
def md5(self, dirName):
files = os.listdir(dirName) # Traverse all files under the folder
temp = set() # Create a set()
count = 0 # Deleted files count
for file in files:
if file.lower().endswith(('jpg', 'jpeg', 'png')):
file_path = os.path.join(dirName, file) # Get the full path
try:
img = Image.open(file_path) # Open the picture
img_array = np.array(img) # Converted to an array
md5 = hashlib.md5() # [ Commodity Futures ](https://www.gendan5.com/futures/cf.html) Create a hash object
md5.update(img_array) # Get the of the current file md5 code
if md5.hexdigest() not in temp: # If the current md5 The code is not in the set
temp.add(md5.hexdigest()) # Then add the current md5 Code to set
else:
os.remove(file_path)
count += 1 # Otherwise, delete the number of pictures plus one
except Exception as e:
os.remove(file_path)
print("duplicate removal:", count) # Finally, output the total number of deleted pictures
def rename(self, dirName):
for root, dirs, files in os.walk(dirName):
i = 0
for file_name in files:
if file_name.lower().endswith(('jpg', 'jpeg', 'png')):
oldname = os.path.join(root, file_name)
pic_format = os.path.splitext(oldname)[-1]
name = 'hivision_buiing_' + root.split('\\')[-1]
print(name)
newname = root + '/' + name + '_' + str(i + 1).zfill(4) + pic_format
i = i + 1
print(newname)
try:
os.rename(oldname, newname)
except Exception as f:
print(f)
def get_dirs_num(self):
dict = {}
f = xlwt.Workbook()
sheet1 = f.add_sheet(u' Count the number of documents ', cell_overwrite_ok=True)
row = 0
row0 = [' Folder path ', ' Folder name ', ' Number of documents ']
for n in range(len(row0)):
sheet1.write(0, n, row0[n])
path = r'E:\ picture '
for root, dirs, files in os.walk(path):
num = 0
for dir_files in os.listdir(root):
if os.path.isfile(os.path.join(root, dir_files)):
if dir_files.lower().endswith(('.jpg', 'jpeg', 'png')):
num = num + 1
name = root.replace(path + '\\', '')
dict[name] = num
for key, values in dict.items():
col = 0
if values > 0:
sheet1.write(row + 1, col, os.path.join(path, key))
col = col + 1
name_list = key.split('\\')
row = row + 1
sheet1.write(row, col, name_list)
col = col + 1
sheet1.write(row, col, values)
f.save(r'E:\ picture \\count.xls')
if name == '__main__':
keyword = ' Dog '
dirName = r"E:\ picture \\{}".format(keyword)
num =20
test = image()
test.baidu_img(keyword, num)
test.md5(dirName)
test.rename(dirName)
test.get_dirs_num()