当前位置: 首页 > 后端技术 > Python

Python3批量修改JPG图片大小?

时间:2023-03-25 21:30:23 Python

1.将当前文件夹中的jpg图片批量修改为设置大小2.将修改后的图片移动到new_img文件夹中,导入库fromPILimportImage#处理图片模块importosimportshutil#如果没有移动文件模块,请提前安装相应的库。定义一个处理图片尺寸的函数defsmaller_img(x,y,path):#x,y用来传入尺寸,path用来传入路径old_img=Image.open(path)img_deal=old_img.resize((x,y),Image.ANTIALIAS)#转换图片img_deal=img_deal.convert('RGB')#只需要img_deal.save('新文件名')遍历当前文件夹文件路径now_path=os.getcwd()new_path=os.mkdir(now_path+'\\'+'new_img')#在os.listdir(now_path)中为file_name创建一个名为new_img的文件夹:files_path=now_path+'\\'+file_nameprint(files_path)#输出当前目录下所有文件的绝对路径。将修改后的图片移动到新建的文件夹中。我用比较笨的方式判断是否被字符串判断修改。图像文件。os.listdir(now_path)中move_name的shutil模块:move_path=now_path+'\\'+move_nameif'switch'inmove_path:shutil.move(move_path,new_dir)#shutil.move(file/directory,directory)else:print(move_path,'不需要移动')集成这些功能阅读原文获取源码:https://developer.aliyun.com/…