您现在的位置是:主页 > news > 新乡营销网站建设/线上销售如何找到精准客户
新乡营销网站建设/线上销售如何找到精准客户
admin2025/5/5 9:58:46【news】
简介新乡营销网站建设,线上销售如何找到精准客户,南通网站制作设计,建设教育信息网站工作总结手里有几千张图片,需要把一些适合当壁纸的高清图片筛选出来,并且要把大分辨率的图片缩小到手机分辨率,合适当壁纸用,所以边学 python 边写程序跑了一下,测试没什么问题就发出来共享 电脑需安装 python3环境,…
手里有几千张图片,需要把一些适合当壁纸的高清图片筛选出来,并且要把大分辨率的图片缩小到手机分辨率,合适当壁纸用,所以边学 python 边写程序跑了一下,测试没什么问题就发出来共享
电脑需安装 python3环境,主要使用的 pillow 库,建议自行使用 pip3安装。
主要代码部分都加了注释,觉得麻烦可以删掉,毕竟我也刚学 python。
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
from PIL import Image
import os
# 需要处理的文件夹,改为你的文件夹的路径,
folder = '/Users/sea/Desktop/0/'
# 处理图片后,优秀的图片的输出的文件夹,excellent,这个路径要改为你自己的
outPath = '/Users/sea/Desktop/excellent/'
if not os.path.exists(outPath): #如果不存在则创建一个
os.makedirs(outPath)
# 一般的图片,需要在次筛选,这个路径也要改为你自己的
outPath2 = '/Users/sea/Desktop/medium/'
if not os.path.exists(outPath2): #如果不存在则创建一个
os.makedirs(outPath2)
# 处理图片的主程序
def picProcess(imagePath):
# 获取单张图片的宽和高
img = Image.open(imagePath)
width,height = img.size
# print('Width = %s , Height = %s' % (width,height))
# 输出的路径和名称 + 为路径拼接
path = outPath + imagePath
path2 = outPath2 + imagePath
# 按照比比例生成新的图片的高和宽
newheight = int((1125 * height) / width)
newwidth = int((2436 * width) / height)
#设置一些规则过滤筛选图片
if width >= 1125 and height >= 2436 and width < height:
img = img.resize((1125,newheight),Image.LANCZOS)
print(img.size[1] , '以宽1125进行缩小',height/width)
img.save(path)#图片非常优秀,进入excellent文件夹
elif width >= 1125 and height >= 2436 and width > height:
img = img.resize((newwidth,2436),Image.LANCZOS)
print(img.size[0] , '以高为2436进行缩小',height/width)
img.save(path)#图片非常优秀,进入excellent文件夹
elif 1000 < width < 1125 or 1900 < height < 2436 :
print('保存到2号文件夹,待操作')
img.save(path2)#图片中等,进入人工筛选状态
elif width <= 1000 or height <= 1900:
print("不操作,等于删除")
pass#图片质量低,直接删除
# print( imagePath + ' save is done')
# 遍历文件夹里的图片然后循环
def runScript():
#改变当前工作目录到指定的路径:图片的列表文件夹
os.chdir(folder)
#for in遍历文件夹,os.listdir()方法用于返回指定的文件夹包含的文件或文件夹的名字的列表
for image in os.listdir(os.getcwd()):#os.getcwd()方法用于返回当前工作目录
# print(image)
#只识别图片格式,忽略其它文件
imageType = os.path.splitext(image)[1]
# print(imageType,image)
if imageType == '.jpg' or imageType == '.JPG' or imageType == '.jpeg' or imageType == '.PNG':
# print(image)
picProcess(image) #执行封装好的压缩程序
if __name__ == '__main__':
runScript()
#-*- MAX Young 2019-4-17 -*-