|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
"""
鱼友们求助!if之前的语句已经实现图片大小修改,为什么还要写if后面的语句,这一段是什么意思?而且_name_也没有定义"""
# 修改图片尺寸
import os
import os.path
from PIL import Image
"""
filein:输入图片
fileout:输出图片
width:输出图片宽度
height:输出图片高度
type:输出图片类型(png,gif,jpeg...)
"""
def ResizeImage(filein,fileout,width,height,type):
img = Image.open(filein)
out = img.resize((width,height),Image.ANTIALIAS)
out.save(fileout,type)
if _name_ == "_main_":
filein = r'./image/plane.png'
fileout = r'./image/planesm.png'
width = 720
height = 560
type = 'png'
ResizeImage(filein,fileout,width,height,type)
本帖最后由 isdkz 于 2022-5-27 19:34 编辑
前面只是定义了函数还没有被执行,需要调用函数之后才会执行,
当python文件不是作为模块被引入的时候,__name__ 属性就会为 "__main__",
加上这个是想让其它python文件将这个作为模块引入的时候不执行相关代码- import os
- import os.path
- from PIL import Image
- """
- filein:输入图片
- fileout:输出图片
- width:输出图片宽度
- height:输出图片高度
- type:输出图片类型(png,gif,jpeg...)
- """
- def ResizeImage(filein,fileout,width,height,type):
- img = Image.open(filein)
- out = img.resize((width,height),Image.ANTIALIAS)
- out.save(fileout,type)
- if __name__ == "__main__": # 这里是两个横杆
- filein = r'./image/plane.png'
- fileout = r'./image/planesm.png'
- width = 720
- height = 560
- type = 'png'
- ResizeImage(filein,fileout,width,height,type)
复制代码
|
|