pillow模块

图片处理

中文文档

安装

pip install Pillow

对图片旋转90度显示

from PIL import Image
im=Image.open("t.jpg")
im.rotate(90).show()

对图片进行裁剪并保存

from PIL import Image
import glob, os size = 128, 128 for infile in glob.glob("*.jpg"):
file, ext = os.path.splitext(infile)
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(file + ".thumbnail.jpg", "JPEG")

切割图片

from PIL import Image

def fill_image(image):

    width, height = image.size

    # 选取长和宽中较大值作为新图片的边长
new_image_length = width if width > height else height # 生成新图片[白底],底色可配置其他颜色
new_image = Image.new(image.mode, (new_image_length, new_image_length), color='white') # 将之前的图片input image 粘贴在新图上,居中
if width > height: # 原图宽大于高,则填充图片的竖直维度 #(x,y)二元组表示粘贴上图相对下图的起始位置,是个坐标点
new_image.paste(image, (0, int((new_image_length - height) / 2)))
else:
new_image.paste(image, (int((new_image_length - width) / 2), 0)) return new_image def cut_image(image): width, height = image.size item_width =int(width /3) # 因为朋友圈一行放3张图 box_list = [] for i in range(0, 3): for j in range(0, 3): box = (j*item_width, i*item_width, (j+1)*item_width, (i+1)*item_width) # (left, top, right, bottom) box_list.append(box) image_list = [image.crop(box) for box in box_list] return image_list def save_images(image_list): index = 1 for image in image_list: image.save(str(index) + '.png', 'PNG') index += 1 if __name__ == '__main__':
file_path = "t.jpg" # 把目标图片 input image 放到代码所处的文件夹里 image = Image.open(file_path)
image = fill_image(image) image_list = cut_image(image) save_images(image_list)

pillow 模块的更多相关文章

  1. Django 生成验证码或二维码 pillow模块

    一.安装PIL PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,API也非常简单易用.   PIL模块只支持到Python 2 ...

  2. 苹果电脑python3安装pillow模块

    我刚开始在苹果电脑自带的python 中安装了pillow模块,导致在后期我想在python3中安装pilow模块的时候 pip3 install pillow 但是总会提示错误说电脑中已经存在pil ...

  3. Pillow模块图片生成

    0825自我总结 Pillow模块图片生成 一.模块安装 pip3 install pillow 二.模块的载入 import PIL 三.django结合img标签生成图片 img.html < ...

  4. 图像处理pillow模块

    pillow模块: -->基本的图像处理模块 Pip install pillow from PIL import Image #1.读取图片 im = Image.open('/test.jp ...

  5. pillow模块

    pillow模块 用于操作图片的模块 安装 pip install pillow 生成验证码 from PIL import Image,ImageDraw,ImageFont from io imp ...

  6. (python)图片处理Pillow模块的使用

    Pillow中最重要的类就是Image,该类存在于同名的模块中.可以通过以下几种方式实例化:从文件中读取图片,处理其他图片得到,或者直接创建一个图片. 还有一个类为ImageDraw,用来画图. 1. ...

  7. python安装pillow模块错误

    安装的一些简单步骤就不介绍了,可以去搜索一下,主要就记录下我在安装pillow这一模块遇到的问题 1:安装好pillow后,安装过程没有出错 2:但是在python的IDLE输入from PIL im ...

  8. python pillow模块用法

    pillow Pillow是PIL的一个派生分支,但如今已经发展成为比PIL本身更具活力的图像处理库.pillow可以说已经取代了PIL,将其封装成python的库(pip即可安装),且支持pytho ...

  9. Python笔记_第二篇_面向过程_第二部分_5.第三方模块的使用和自定模块(以Pillow模块为例)

    1. 安装第三方模块: 打开黑屏终端: cmd: pip -verson pip - V C:\windows\system32>pip -V pip from c:\python37\lib\ ...

随机推荐

  1. Go语言实现:【剑指offer】变态跳台阶

    该题目来源于牛客网<剑指offer>专题. 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 找规律: 1阶:1种: 2阶:2 ...

  2. 在.NET Core中使用MachineKey

    在.NET Core中使用MachineKey 姐妹篇:<ASP.NET Cookie是怎么生成的> 姐妹篇:<.NET Core验证ASP.NET密码> 在上篇文章中,我介绍 ...

  3. mount.nfs: Stale file handle的解决方法

    在NFS客户端挂载rpc共享服务的时候出现这个问题 # mount -t nfs 192.168.20.6:/data /mnt mount.nfs: Stale file handle 原因是当cl ...

  4. Apache httpd.conf配置文件 2(Main server configuration)

    ### Section 2: 'Main' server configuration # # The directives in this section set up the values used ...

  5. [jQuery]顶级对象$(二)

    $ 是 jQuery 的缩写 <script> # 方法1. $ 是jQuery的别称 弹出提示 $(function () { alert(11) ); # 方法2 jQuery(fun ...

  6. Nginx-2.初学者使用

    原文 Nginx有一个master进程和几个worker进程.master进程用来读取和评估配置文件,以及维护worker进程.worker进程用来处理实际的请求.Nginx使用事件模型和基于操作系统 ...

  7. MySQL索引那些事

    原文链接 大家有没有遇到过慢查询的情况,执行一条SQL需要几秒,甚至十几.几十秒的时间,这时候DBA就会建议你去把查询的 SQL 优化一下,怎么优化?你能想到的就是加索引吧? 为什么加索引就查的快了? ...

  8. 06_TypeScript泛型

    1.泛型的定义 泛型就是解决 类,接口 方法的复用性,以及对不特定数据的支持(类型校验). 2.泛型函数 //T 表示泛型,具体什么类型是调用这个方法的时候决定的, //T可以用其他大写字母表示,传入 ...

  9. 【转载】Java的Vector,ArrayList,LinkedList

    首先看这两类都实现List接口,而List接口一共有三个实现类,分别是ArrayList.Vector和LinkedList.List用于存放多个元素,能够维护元素的次序,并且允许元素的重复.3个具体 ...

  10. oracle数据库的启动、关闭、连接

    登陆数据库 方法一: $ sqlplus / as sysdba [oracle@dev /]$ sqlplus / as sysdba SQL*Plus: Release Production on ...