PIL和OpenCV提取图像大小批量可视化输出txt保存源码
部分内容纯属原创娱乐。hhh
from PIL import Image
import cv2
import os
import numpy as np
import functools
def main():
f1 = open("S:\\AdobeppPS\\ceshi02.txt",'w+')
input_dir = "S:\\AdobeppPS\\SKOO\\"
aa = os.listdir( input_dir)
abc=0
set_list = []
list00=[]
tholdzz=127
zs=len(aa)*7
cxx=[]
cyy=[]
hight01=[]
weight01=[]
for ia in aa:
grayy=cv2.imread(input_dir+ia,0)
print (grayy.shape)
print (grayy.size)
# gray_image = cv2.cvtColor(grayy, cv2.COLOR_BGR2GRAY)
# ret,thresh = cv2.threshold(gray_image,tholdzz,255,0)
ret,thresh = cv2.threshold(grayy,tholdzz,255,0)
M = cv2.moments(thresh)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cxx.append(cX)
cyy.append(cY)
cv2.circle(grayy, (cX, cY), 5, (255, 255, 255), -1)
cv2.putText(grayy, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# print("质心坐标为:",cX,cY)
imagedd = Image.open(input_dir+ia)
weightI0,highI0 = imagedd.size
weight01.append(weightI0)
hight01.append(highI0)
# print('(%s,%s)' % (high,weight))
arr1=sys_moments(grayy)
for item in range(len(arr1)):
s=arr1[item,0]
set_list.append(s)
list00.append(s)
for cs0 in range(zs):
if cs0%7==0:
abc=abc+1
f1.write("第 "+str(abc)+"\t张图片的大小为:"+str(weight01[abc-1])+"x"+str(hight01[abc-1]) +"\n")
f1.close()
if __name__ == '__main__':
main()
print("well done")
部分参考来源;https://blog.csdn.net/alice_tl/article/details/89296110
https://blog.csdn.net/dongbao520/article/details/119353939
效果如图:
新版批量实现源码
# 统计图像大小成功成功批量化实现写入txt
from PIL import Image
import cv2
import os
import numpy as np
import functools
def sys_moments(img):
moments = cv2.moments(img)#返回的是一个字典,三阶及以下的几何矩(mpq)、中心矩(mupq)和归一化的矩(nupq)
humoments = cv2.HuMoments(moments)#根据几何矩(mpq)、中心矩(mupq)和归一化的矩(nupq)计算出hu不变矩
humoment = -(np.log(np.abs(humoments)))/np.log(10)
return humoment
def main():
f1 = open("D:\\Pictures\\ceshi02.txt",'w+')
input_dir = "D:\\Pictures\\cscscs\\"
aa = os.listdir( input_dir)
abc=0
set_list = []
list00=[]
tholdzz=127
zs=len(aa)*7
cxx=[]
cyy=[]
hight01=[]
weight01=[]
for ia in aa:
grayy=cv2.imread(input_dir+ia,0)
# print (grayy.shape)
# print (grayy.size)
# gray_image = cv2.cvtColor(grayy, cv2.COLOR_BGR2GRAY)
# ret,thresh = cv2.threshold(gray_image,tholdzz,255,0)
ret,thresh = cv2.threshold(grayy,tholdzz,255,0)
M = cv2.moments(thresh)
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
cxx.append(cX)
cyy.append(cY)
cv2.circle(grayy, (cX, cY), 5, (255, 255, 255), -1)
cv2.putText(grayy, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# print("质心坐标为:",cX,cY)
imagedd = Image.open(input_dir+ia)
weightI0,highI0 = imagedd.size
weight01.append(weightI0)
hight01.append(highI0)
# print('(%s,%s)' % (high,weight))
arr1=sys_moments(grayy)
for item in range(len(arr1)):
s=arr1[item,0]
set_list.append(s)
list00.append(s)
for cs0 in range(zs):
if cs0%7==0:
abc=abc+1
f1.write("第 "+str(abc)+"张图片的大小为:\t"+
str(weight01[abc-1])+"X"+str(hight01[abc-1]) +"\t"+
'像素:'+str(grayy.size)+"\n")
f1.close()
if __name__ == '__main__':
main()
print("well done")