如题
# -*- coding: utf-8 -*-
# @Time : 2023/10/4 19:26
# @Author : 龙俊威
# @File : sortall.py
# @Software:PyCharm
import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
path = filepath='C:\\Users\Administrator\Desktop\\201-216图片'
#文件夹地址
def solve(num):
filepath = str(num)
pic = np.array(Image.open(filepath))
print(pic.shape)
N = 90
for i in range(400):
for j in range(400):
if i<pic.shape[0] and j<pic.shape[1]:
if (pic[i][j][0] > N) & (pic[i][j][1] > N) & (pic[i][j][2] > N):
pic.itemset((i, j, 0), 255)
pic.itemset((i, j, 1), 255)
pic.itemset((i, j, 2), 255)
else:
pic.itemset((i, j, 0), 0)
pic.itemset((i, j, 1), 0)
pic.itemset((i, j, 2), 0)
else:
pic.itemset((i, j, 0), 255)
pic.itemset((i, j, 1), 255)
pic.itemset((i, j, 2), 255)
plt.imshow(pic)
plt.axis('off')
fig = plt.gcf()
fig.set_size_inches(4, 4)
out_place=num.replace('C:\\Users\Administrator\Desktop\\201-216图片\\','')
out_place= out_place.replace(".png","")
print(out_place)
# 设置图像的分辨率为 100 dpi
plt.savefig(out_place, dpi=100)
plt.show()
def get_filelist(dir):
Filelist = []
for home, dirs, files in os.walk(path):
for filename in files:
# 文件名列表,包含完整路径
Filelist.append(os.path.join(home, filename))
# # 文件名列表,只包含文件名
# Filelist.append( filename)
return Filelist
if __name__ == "__main__":
Filelist = get_filelist(dir)
print(len(Filelist))
for Tfile in Filelist:
th_file=str(Tfile)
solve(th_file)
标签:plt,文件名,python,pic,Filelist,itemset,文件夹,out,255 From: https://www.cnblogs.com/CLGYPYJ/p/17742766.html