Python——使用第三方库Pillow生成图片缩略图

时间:2023-03-08 21:12:50

流程如下:Python——使用第三方库Pillow生成图片缩略图

1、首先确认是否安装了pip

在命令提示符窗口下输入pip,如果Windows提示未找到命令,可以重新运行安装程序添加pip

Python——使用第三方库Pillow生成图片缩略图

2、在命令提示符窗口下输入pip install Pillow,注意:P一定大写

Python——使用第三方库Pillow生成图片缩略图

等待下载安装完成,根据网速的不同,时间约有3-10分钟左右。

3、第一步导入Image模块:

from PIL import Image

Python——使用第三方库Pillow生成图片缩略图

4、第二步打开桌面的测试文件:注意:文件夹之间使用/来断开

im = Image.open ('C:/Users/Administrator/Desktop/test.png')

Python——使用第三方库Pillow生成图片缩略图

5、生成缩略图并保存在im中

im.thumbnail((200,100))

Python——使用第三方库Pillow生成图片缩略图

6、保存缩略图

im.save('C:/Users/Administrator/Desktop/thumbnail.png','PNG')

Python——使用第三方库Pillow生成图片缩略图

7、查看确认

Python——使用第三方库Pillow生成图片缩略图