效果展示
很多图片管理器都实现了横图和竖图的分类功能,出于某种需求,我们用pyhon
实现一下。将一组图片的横图全部移动到一个文件夹中,竖图划分到另一文件夹中。
- 横图:宽大于或等于长的图片
- 竖图:宽小于长的图片
- 新建名为
widthImage
的文件夹,作为宽图的保存路径
import os
os.makedirs('widthImage')
- 新建名为
heightImage
的文件夹,作为长图的保存路径
import os
os.makedirs('heightImage')
- 打开图片,并获取长和宽
from PIL import Image
img = Image.open('test.png')
w, h = img.size
img.close()
- 对图片的长、宽作出判断,并分别移动到对应的文件夹
import shutil
if w >= h:
shutil.move(src, dst)
else:
shutil.move(src, dst)
- 为了更快速的移动,可以采用多线程
import concurrent.futures as cf
tp = cf.ThreadPoolExecutor()
tp.submit(move, name)
tp.shutdown()
- 打印进度条
3/5 |████████████ | 任务进度: 60.00% 已用时间: 4.50S 剩余时间: 3.00S
import time
# 主函数
def main(n):
t1 = time.time()
for i in range(n):
time.sleep(1.5) # 假设每个任务的等待时间是1.5s
t2 = time.time()
runTime = t2-t1
show(i+1, n, runTime)
# 进度条打印函数
def show(num, _sum, runTime):
barLen = 20 # 进度条的长度
perFin = num/_sum
numFin = round(barLen*perFin)
numNon = barLen-numFin
leftTime = (1-perFin)*(runTime/perFin)
print(
f"{num:0>{len(str(_sum))}}/{_sum}",
f"|{'█'*numFin}{' '*numNon}|",
f"任务进度: {perFin*100:.2f}%",
f"已用时间: {runTime:.2f}S",
f"剩余时间: {leftTime:.2f}S",
end='\r')
if num == _sum:
print()
main(5)
完整代码
# 横图竖图划分.py
import concurrent.futures as cf
from PIL import Image
import shutil
import time
import os
import re
class ImageClassfy(object):
# 初始化
def __init__(self):
root = os.getcwd() # 获取当前路径
self.root = root
self.names = os.listdir(root)
self.widthImage = os.path.join(root, 'widthImage')
self.heightImage = os.path.join(root, 'heightImage')
self.count = 0
self.pret()
# 新建长图和宽度的保存文件夹
def pret(self):
for i in self.names:
pattern = re.compile('(.png|.jpg)$')
if not re.search(pattern, i):
self.names.remove(i)
self.sum = len(self.names)
if not os.path.exists(self.widthImage):
os.makedirs(self.widthImage)
if not os.path.exists(self.heightImage):
os.makedirs(self.heightImage)
# 移动图片到对应的文件夹
def move(self, name):
src = os.path.join(self.root, name)
img = Image.open(src)
w, h = img.size
img.close()
if w >= h:
shutil.move(src, self.widthImage)
else:
shutil.move(src, self.heightImage)
# 打印进度条
def show(self, num, _sum, runTime):
barLen = 20 # 进度条的长度
perFin = num/_sum
numFin = round(barLen*perFin)
numNon = barLen-numFin
leftTime = (1-perFin)*(runTime/perFin)
print(
f"{num:0>{len(str(_sum))}}/{_sum}",
f"|{'█'*numFin}{' '*numNon}|",
f"任务进度: {perFin*100:.2f}%",
f"已用时间: {runTime:.2f}S",
f"剩余时间: {leftTime:.2f}S",
end='\r')
if num == _sum:
print()
# 主函数
def main(self):
tp = cf.ThreadPoolExecutor(32) # 多进程实现,指定进程数为32
futures = []
t1 = time.time()
for name in self.names:
future = tp.submit(self.move, name)
futures.append(future)
for future in cf.as_completed(futures):
self.count += 1
t2 = time.time()
runTime = t2-t1
self.show(self.count, self.sum, runTime)
tp.shutdown()
if __name__ == "__main__":
ImageClassfy().main()
依赖模块
concurrent.futures
、shutil
、time
、os
和re
都是pyhton内置模块,无需安装。
pillow
(PIL)是第三方图片处理库,需要安装:
pip install pillow
使用说明
将代码横图竖图划分.py
拖拽到图片文件夹中,双击运行即可。(前提是你电脑装有python
环境,并安装了上述依赖模块)
https://pillow.readthedocs.io/en/stable/
https://docs.python.org/zh-cn/3/library/os.html
https://docs.python.org/zh-cn/3/library/re.html
https://docs.python.org/zh-cn/3/library/shutil.html
https://docs.python.org/zh-cn/3/library/concurrent.futures.html