python3.7.2怎么用不了pillow_Python3 批处理高清图片,使用pillow库

手里有几千张图片,需要把一些适合当壁纸的高清图片筛选出来,并且要把大分辨率的图片缩小到手机分辨率,合适当壁纸用,所以边学 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 -*-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值