当前位置: 代码迷 >> 综合 >> 图像批量修改格式,图像批量修改尺寸
  详细解决方案

图像批量修改格式,图像批量修改尺寸

热度:59   发布时间:2024-03-09 22:38:01.0
批量修改格式
import osclass BatchRename():'''批量重命名文件夹中的图片文件'''def __init__(self):self.path = r'F:\camouflag\电影图片\deserts'  # 表示需要命名处理的文件夹def rename(self):filelist = os.listdir(self.path)  # 获取文件路径total_num = len(filelist)  # 获取文件长度(个数)i = 1  # 表示文件的命名是从1开始的for item in filelist:if item.endswith('.jpg'):src = os.path.join(os.path.abspath(self.path), item)dst = os.path.join(os.path.abspath(self.path),'' + 'desert' + str(i) + '.jpg')  # 处理后的格式也为jpg格式的,当然这里可以改成png格式try:os.rename(src, dst)# print ('converting %s to %s ...' % (src, dst))i = i + 1except:continueprint('total %d to rename & converted %d jpgs' % (total_num, i))if __name__ == '__main__':demo = BatchRename()demo.rename()

 

批量修改尺寸

 

from PIL import Image
import os.path
import globdef convertjpg(jpgfile,outdir,width=512,height=512):img=Image.open(jpgfile)try:new_img = img.resize((width, height), Image.BILINEAR)if new_img.mode == 'P':new_img = new_img.convert("RGB")if new_img.mode == 'RGBA':new_img = new_img.convert("RGB")new_img.save(os.path.join(outdir, os.path.basename(jpgfile)))except Exception as e:print(e)for jpgfile in glob.glob("F:/camouflag/电影图片/rr/deserts/*.jpg"):# print(jpgfile)convertjpg(jpgfile,"F:/camouflag/电影图片/rr/desertttttt")
  相关解决方案