总体来说遇到的问题:
1.image类的参数不了解
image.open(“图片文件路径(使用相对路径使程序更加健壮)”)
image.save(“图片输出路径”+“后缀”)
image.resize((宽,高),后面参数不管,因为不懂ε=(´ο`*)))唉)
2.保存位置判定是否存在
用try判断,存在就跳过创建
总思路:
为了方便使用,把代码打包成EXE文件,和图片文件放在一起运行,导出文件放入单独文件夹内。
程序思路:
1.获得所有图片的信息,返回一个字典{名称:image.open()}
2.输入参数:分辨率,为了提高代码的健壮性,设置默认参数1080,处理并保存至“导出文件夹”
3.增加main(),增加不自动关闭窗口循环
源代码:
from PIL import Image
import os
def open_image():
ls=os.listdir(".")
dict={}
try:
os.mkdir("."+os.sep+"导出文件夹")
except:
print("已经存在导出文件夹,跳过")
for i in ls:
a=os.path.splitext(i)
if a[1]==".jpg" or a[1]==".jepg" or a[1]==".png"\
or a[1]==".JPG" or a[1]==".JEPG" or a[1]==".PNG":
image=Image.open(i)
dict[i]=image
return dict
def process(ls_all,widtch=1080):
for i in ls_all:
if ls_all[i].size[0]>widtch:
print("正在处理【{}】,请稍后".format(i))
new_size=ls_all[i].resize((widtch,int(widtch/ls_all[i].size[0]*ls_all[i].size[1])))
new_size.save("."+os.sep+"导出文件夹"+os.sep+"【"+str(widtch)+"缩小处理】-"+i+"."+ls_all[i].format)
else:
print("【{}】不符合处理条件,跳过".format(i))
def main():
print("--------------------本软件由塩鱼基于python制作-----------------------")
print("请将本文件与需要处理的图片放在同一文件夹内")
ls=open_image()
widtch=input("请输入分辨率,默认【1080】:")
try:
widtch=int(widtch)
except:
print("使用默认参数1080p")
widtch=1080
process(ls_all=ls,widtch=widtch)
print("处理完毕!请关闭窗口")
while True:
pass
if __name__ == '__main__':
main()
编辑完之后就发现,还有很多多余的问题,虽然程序能正常运行···
以后再改了哈哈哈