判斷路徑中是否包含中文
import re
def IsContainChinese(path:str) -> bool :
cnPatter=re.compile(u'[\u4e00-\u9fa5]+')
match=cnPatter.search(path)
flag=False
if match:
flag=True
else:
flag = False
return flag
將文件保存為csv格式
import csv
def WriteResultToCSV(**kwags):
v = [ v for v in kwags.values()]
# v=lambda v:[ v for v in kwags.values()]
# print(v)
for item in v:
try:
header=["文件名","高度","寬度"]
# 如果不加newline參數,則保存的csv文件會出現隔行為空的情況
with open(os.getcwd()+"\\result.csv",'w+',newline="") as fw:
csvWriter=csv.writer(fw)
csvWriter.writerow(header)
# print(item.items())
for k,v in item.items():
print(f"{k} {v}")
csvWriter.writerow([k,str(v[0]),str(v[1])])
except Exception as e:
pass
獲取圖片分辨率
- 方法一:通過opencv該方法不支持路徑或文件名含有中文
python opencv2安裝: pip install opencv-python
import cv2
def GetResolution(path,imgList):
temDict={}
for item in imgList:
# opencv 不支持中文路徑
img=cv2.imread(path+"\\"+item)
# cv2.namedWindow("Image")
# cv2.imshow("Image",img)
# cv2.waitKey(1)
# cv2.destroyAllWindows()
imgResolution=img.shape
temDict[item]=imgResolution
return temDict
- 方法二:通過opencv
import cv2
import numpy as np
# 使用該方法時,路徑中可含有中文,其中tmp為完整的圖片路徑
img=cv2.imdecode(np.fromfile(tmp,dtype=np.uint8),cv2.IMREAD_COLOR)
# 獲取圖片高度和寬度
imgHeight,imgWidth=img.shape[:2]
- 方法三:通過Pillow
pip install Pillow
from PIL import Image
def GetImgSize(path):
"""
path:傳入完整路徑
"""
img=Image.open(path)
imgWidth,imgHeight=img.size
獲取文件夾內特定的文件
import os
def GetImgList(path):
imgList=[ img for img in os.listdir(path)
if os.path.isfile(os.path.join(path,img)) and (img.endswith(".jpg") or img.endswith(".jpge") or img.endswith(".png"))
]
return imgList
將圖片轉換為Base64編碼
import base64
def ConverImgToBase64(path,imgList):
resultList={}
for img in imgList:
try:
with open (path+"\\"+img,'rb') as fr:
data=base64.b64encode(fr.read())
tempResult=data.decode()
resultList[img]=tempResult
except Exception as e:
resultList["Exception"]=e
return resultList
生成MD5碼
# 生成MD5碼
def GenerateMD5Code(sku,secretKey='e10adc3949ba59abbe56e057f20f883e'):
md5=hashlib.md5()
encodeStr=secretKey+sku
md5.update(encodeStr.encode('utf8'))
return md5.hexdigest()
判斷文件或文件夾是否存在
import os
def IsExistFile(path):
try:
if (os.path.exists(path)):
os.remove(path)
except Exception as identifier:
pass
比較文件差異
import os
# 描述信息:一個文件夾內一張圖片對應一個xml或者只有圖片或只有xml
def ListFile(path):
imgList=[]
xmlList=[]
extendIsXmlCount=0
extendIsImgCount=0
for file in os.listdir(path):
fileList=file.split(".")
try:
if fileList[-1] == "xml":
extendIsXmlCount+=1
xmlList.append(fileList[0])
elif fileList[-1] == "jpg" or fileList[-1] == "jpeg" or fileList[-1] == "png":
extendIsImgCount+=1
imgList.append(fileList[0])
except Exception as e:
print("error")
differenceResult=set(imgList+xmlList)
return imgList,xmlList,extendIsImgCount,extendIsXmlCount,differenceResult
def CompareCount(xmlCount,imgCount):
'''
-1: xml > img
0 : xml == img
1 : xml < img
'''
# compareResult=-9999
differenceCount=-9999
if (xmlCount > imgCount):
# print(f"xml Count {xmlCount} is more than img Count {imgCount} ,difference is {xmlCount-imgCount}")
compareResult=f"xml Count {xmlCount} is more than img Count {imgCount} ,difference is {xmlCount-imgCount}"
differenceCount=xmlCount-imgCount
elif(xmlCount < imgCount):
# print(f"xml Count {xmlCount} is less than img Count {imgCount} ,difference is {imgCount-xmlCount}")
compareResult=f"xml Count {xmlCount} is less than img Count {imgCount} ,difference is {imgCount-xmlCount}"
differenceCount=imgCount-xmlCount
elif (xmlCount == imgCount):
# print(f"xml Count {xmlCount} is equal img Count {imgCount} ,difference is {imgCount-xmlCount}")
compareResult=f"xml Count {xmlCount} is equal img Count {imgCount} ,difference is {imgCount-xmlCount}"
differenceCount=imgCount-xmlCount
return compareResult,differenceCount
def RemoveDuplicateItem(ListA,ListB):
tempSetA=set(ListA)
tempSetB=set(ListB)
if len(tempSetA) >= len(tempSetB):
result=tempSetA-tempSetB
else:
result=tempSetB-tempSetA
return result
讀取pkl文件
import pickle
def ReadFile(path):
result=""
try:
with open (path,'rb') as fr:
result=pickle.load(fr)
except Exception as e:
result=e
return result
存取為pkl文件
import pickle
def WriteStrToLogFile(path,dataStr):
for i in range(2,5):
PickleToFile(path+"\\"+"error"+str(i)+".pkl",dataStr)
def PickleToFile(path,fileName):
with open(path,'wb') as fw:
pickle.dump(dataStr,fw)
將時間轉換為Unix時間戳
import time
import datetime
def ChangDateTimeToUnix(inputDateTime):
'''
將標准時間轉換為Unix時間戳
time strptime() 函數根據指定的格式把一個時間字符串解析為時間元組
time.strptime(string[, format])
'''
timeArray = time.strptime(inputDateTime, "%Y-%m-%d %H:%M:%S")
timeStamp = int(time.mktime(timeArray))
return timeStamp
本文同步在微信訂閱號上發布,如各位小伙伴們喜歡我的文章,也可以關注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關注:

