欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

python pillow库的基础使用教程

时间:2021-09-28 09:34:03|栏目:Python代码|点击:

知识点

  • 图像模块 (Image.Image)

Image模块的功能
Image模块的方法

  • ImageChops模块
  • ImageColor模块

基础使用

图像模块 Image.Image

加载图像对象,旋转90度并显示

from PIL import Image
#显示图像
im = Image.open('background.jpg')
im.show()

# 转换图像90度
im.rotate(90).show()

创建缩略图 128x128

from PIL import Image
import glob, os
size = 128, 128
for infile in glob.glob('D:\code\gitee\pydata\python3-example\pillow_demo\*.jpg'):
  print(infile)
  filename = os.path.split(infile)[-1]
  im = Image.open(infile)
  im.thumbnail(size, Image.ANTIALIAS)
  im.save("D:\code\gitee\pydata\python3-example\pillow_demo\\" + filename)

创建一个新图像, 分辨率为1920*1080

from PIL import Image
im = Image.new('RGB', (1920, 1080), (255, 0, 0))
im1 = Image.new('RGB', (1920, 1080), 'red')
im2 = Image.new('RGB', (1920, 1080), '#FF0000')
im2.show()

将图像转换为PNG

im = Image.open('background.jpg', 'r')
im.save('background.png')
im.show()
im_png = Image.open('background.png', 'r')
print(im_png.format)

ImageChops模块

ImageChops模块包含多个算术图像的操作,称为通道操作,它们可以实现,特殊效果,图像合成,算法绘画等

它的功能大多数通道操作都是采用一个或两个图像参数比较来返回一个新图像,下面只列出一些常用的方法:

IC.lighter(image1,image2):逐个像素地比较两个图像,并返回包含较亮值的新图像

from PIL import Image
from PIL import ImageChops
im1=Image.open('1.jpg')
im2=Image.open('2.jpg')

IC_image=ImageChops.lighter(im1,im2)
IC_image.show()

ImageColor模块

ImageColor模块用来实现RGB颜色表转换,它支持是颜色格式包括:

  • 十六进制颜色说明符,例如,“#ff0000”指定纯红色
  • RGB函数,以“rgb(红色,绿色,蓝色)”给出,其中颜色值是0到255范围内的整数,如,“rgb(255,0,0)”和“rgb(100%,0%,0%)
  • 常见的HTML颜色名称,例如,“red”指定纯红色

getrgb(color):将颜色字符串转换为RGB元组

from PIL import ImageColor
IC_image=ImageColor.getrgb('red')
print(IC_image)

#
(255, 0, 0)

上一篇:使用 Python ssh 远程登陆服务器的最佳方案

栏    目:Python代码

下一篇:Python中的Cookie模块如何使用

本文标题:python pillow库的基础使用教程

本文地址:http://www.codeinn.net/misctech/177934.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有