python接收图片变成缩略图
2016-11-02 18:42
232 查看
python图像处理库:Pillow初级教程
Image类
Pillow中最重要的类就是Image,该类存在于同名的模块中。可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建一个图片。使用Image模块中的open函数打开一张图片:
>>> from PIL import Image >>> im = Image.open("lena.ppm")
如果打开成功,返回一个Image对象,可以通过对象属性检查文件内容
>>> from __future__ import print_function >>> print(im.format, im.size, im.mode) PPM (512, 512) RGB
format属性定义了图像的格式,如果图像不是从文件打开的,那么该属性值为None;size属性是一个tuple,表示图像的宽和高(单位为像素);mode属性为表示图像的模式,常用的模式为:L为灰度图,RGB为真彩色,CMYK为pre-press图像。
如果文件不能打开,则抛出IOError异常。
当有一个Image对象时,可以用Image类的各个方法进行处理和操作图像,例如显示图片:
>>> im.show()
创建缩略图
infile = "static/update_img/img3.jpg" outfile = os.path.splitext(infile)[0] + ".JPEG" #这句是去掉jpg if infile != outfile: try: im = Image.open(infile) print(im.size) height_img = int(im.size[1]*750/im.size[0]) size = (750, height_img) print(size) im.thumbnail(size) im.save(outfile) except IOError: print("cannot create thumbnail for", infile)
相关文章推荐
- java生成缩略图,接收图片,按指定宽高或按比例生成缩略图
- Python每日练习 04 你有一个目录,装有很多图片,现在你要把它们的尺寸变成不都大于Iphone5分辨率的大小
- 缩略图实现,将图片(jpg、bmp、png、gif等等)真实的变成想要的大小
- Python实现将照片变成卡通图片的方法【基于opencv】
- phpcms v9图片生成缩略图变成黑色解决方法
- python,让文字变成图片
- 利用Python进行图片发送与接收的两种方法---包含客户端和服务器端代码
- 图片缩略图显示模式变成图标
- python之利用PIL库实现页面的图片验证码及缩略图 推荐
- webuploader+python3(BaseHTTPRequestHandler)实现图片文件的上传与接收存储
- Erlang/Elixir: 使用 OpenCV, Python 搭建图片缩略图服务器
- python,让文字变成图片
- Python处理图片缩略图
- 怎样用Python 写一个爬图片的程序?
- Python PIL图片数据操作
- python实现对文件中图片生成带标签的txt文件
- Python 读取图片文件为矩阵和保存矩阵为图片的方法
- java 程序实现对图片的压缩生成缩略图并可设定长宽、尺寸压缩率、图片质量
- python3.5 爬取mzitu网站图片
- 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图<原创>