A function to modify the size of any attribute picture————python

#!/usr?bin/env/ python
# -*- coding:utf-8 -*-
# author: lai zheng laing
# datetime: 2020/10/13 8:37
# software: PyCharm
#############################这个函数的功能是改变图片的格式和大小##############################

import os
from PIL import Image
fileName = os.listdir('E:\\南昌大学学习研究2020年5月\\my_data\\')
width =256
height =256
os.mkdir('E:\\南昌大学学习研究2020年5月\\newimage\\')
for img in fileName:
    pic =Image.open('E:\\南昌大学学习研究2020年5月\\my_data\\'+img)
    try:
        newpic =pic.resize((width,height),Image.ANTIALIAS)
        if newpic.mode == 'P':
            newpic = newpic.convert("RGB")
        if newpic.mode == 'RGBA':
            newpic = newpic.convert("RGB")
    except Exception as e:
        print(e)
    print(newpic)
    newpic.save('E:\\南昌大学学习研究2020年5月\\newimage\\'+img)

Guess you like

Origin blog.csdn.net/qq_42794767/article/details/109222846