PIL 中的 Image 模块

2024-05-30 03:58
文章标签 模块 pil image

本文主要是介绍PIL 中的 Image 模块,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文是节选自 PIL handbook online 并做了一些简单的翻译

只能保证自己看懂,不保证翻译质量。欢迎各位给出意见。

------------------------------------------------------

    Image 模块提供了一个同名类(Image),也提供了一些工厂函数,包括从文件中载入图片和创建新图片。例如,以下的脚本先载入一幅图片,将它旋转 45 度角,并显示出来:

   
1 >>>from PIL import Image 2  >>>im = Image.open("j.jpg") 3  >>>im.rotate(45).show()

    下面这个脚本则创建了当前目录下所有以 .jpg 结尾的图片的缩略图。

复制代码
    
1 from PIL import Image 2  import glob, os 3 4 size = 128, 128 5  for infile in glob.glob("*.jpg"): 6 file, ext = os.path.splitext(infile) 7 im = Image.open(infile) 8 im.thumbnail(size, Image.ANTIALIAS) 9 im.save(file + ".thumbnail", "JPEG")
复制代码

    Image 类中的函数

0.    new : 这个函数创建一幅给定模式(mode)和尺寸(size)的图片。如果省略 color 参数,则创建的图片被黑色填充满,如果 color 参数是 None 值,则图片还没初始化。

    
1 Image.new( mode, size ) => image 2 Image.new( mode, size, color ) => image

1.    open : 打开并识别所提供的图像文件。不过,使用这函数的时候,真正的图像数据在你进行数据处理之前并没有被读取出来。可使用 load 函数进行强制加载。 mode 参数可以省略,但它只能是 "r" 值。

    
1 Image.open( infile ) => image 2 Image.open( infile, mode ) => image

2.    blend : 使用两幅给出的图片和一个常量 alpha 创建新的图片。两幅图片必须是同样的 size 和 mode 。

    
1 Image.blend( image1, image2, alpha ) => image 2  # 结果 与 运算过程 3 # out = image1 * ( 1.0 - alpha ) + image2 * alpha

3.    composite : 使用两幅给出的图片和一个与 alpha 参数相似用法的 mask 参数,其值可为:"1", "L", "RGBA" 。两幅图片的 size 必须相同。

    
1 Image.composite( image1, image2, mask ) => image

4.    eval : 使用带一个参数的函数作用于给定图片的每一个像素。如果给定的图片有超过一个的 频段(band),则该函数也会作用于每一个频段。注意,该函数是每一个像素计算一次,所以不能使用一些随机组件或其他的生成器。

    
1 Image.eval( image, function ) => image

5.    frombuffer : (PIL 1.1.4 中新添加的)使用标准 "raw" 解码器在像素数据或是对象缓存中创建一个图像副本。不是所有的模式都支持这种用法。支持的 mode 有"L", "RGBX", "RGBA", "CMYK"。

    
Image.frombuffer( mode, size, data ) => image

6.    fromstring : 注意,这个函数只对像素数据进行解码,而不是一整张图片。如果你有一整张字符串格式的图片,使用 StringIO 对其进行包装并用 open 函数载入它。

    
1 # 使用字符串类型的像素数据和标准解码器 "raw" 来创建图像 2  Image.fromstring( mode, size, data ) => image 3 4  # 同上。不过允许你使用其他 PIL 支持的像素解码器。 5  Image.fromstring( mode, size, data, decoder, parameters ) => image

7.    merge : 使用一系列单一频段(band)的图像来创建新的一幅图像。频段是以一些图像组成的元组或列表,所有的 band 必须有相同大小的 size 。

    
1 Image.merge( mode, bands ) =>image

    Image 类中的方法

0.    convert : 返回一个转换后的图像的副本。

复制代码
    
1 # If mode is omitted, a mode is chosed so that all information in the image and the palette can be representedwithout a palette . 2 # when from a colour image to black and white, the library uses the ITU-R 601-2 luma transfrom: 3 # L = R * 299/1000 + G * 587/1000 + B * 114/1000 4  im.convert( mode ) => image 5 6  # Converts an "RGB" image to "L" or "RGB" using a conversion matrix. The matrix is 4- or 16-tuple. 7  im.convert( mode, matrix ) => image
复制代码

    下面是一个例子:转换 RGB 为 XYZ 。

    
1 rgb2xyz = ( 2 0.412453, 0.357580, 0.180423, 0, 3 0.212671, 0.715160, 0.072169, 0, 4 0.019334, 0.119193, 0.950227, 0 ) 5 out = im.convert("RGB", rgb2xyz)

1.    copy : 复制图像。如果你希望粘贴一些东西进图像里面的话可以使用这个方法,但仍然会保留原图像。

    
1 im.copy() => image

2.    crop : 返回图像某个给定区域。box 是一个 4 元素元组,定义了 left, upper, right, lower 像素坐标。使用这个方法的时候,如果改变原始图像,可能会,也可能不会改变裁剪生成的图像。创建一个完全的复制,裁剪复制的时候使用 load 方法。

    
1 im.crop( box ) => image

3.    draft : 按给出的 mode 和 size 进行配置。可以使用这个方法将彩色JPEG图片转为灰度图。

    
1 im.draft(mode, size)

4.    filter : 返回图像使用滤波器后的副本。可以看 这里 获取更多有用的滤波器。

    
1 im.filter( filter ) => image

5.    fromstring : 和前面的函数是一样的功能,不过这个方法是将数据载入到当前图像。

    
1 im.fromstring( data ) 2 im.fromstring( data, decoder, parameters )
6.    getbands : 返回一个元组,包含每一个 band 的名字,比如,在一幅 RGB 格式的图像上使用 getbands 则返回("R", "G", "B")。
    
1 im.getbands( ) => tuple of strings

7.    getbbox : 计算图像边框值,返回一个 4-元组 ,值为(左,上,右,下)。

    
1 im.getbbox() => 4-tuple or None

8.    getcolors : 在 1.1.5 版本中新添加的。返回一个未排序列表,其元素是元组(count, color)。the count is the number of times the corresponding color occurs in the image 。If the maxcolors value is exceeded, the method stops counting and returns None。

    
1 im.getcolors() => a list of (count, color) tuples or None 2 im.getcolors( maxcolors ) => a list of (count, color) tuples or None

9.    getdata : 返回一个图像内容的像素值序列。不过,这个返回值是 PIL 内部的数据类型,只支持确切的序列操作符,包括迭代器和基本序列方法。我们可以通过 list(im.getdata())  为其生成普通的序列。

    
1 im.getdata() => sequence

10.    getextrema : 返回一个 2-元组 ,值为图像的最小最大值。在当前PIL版本中,仅支持单一频段(single-band)的图像。

    
1 im.getextrema() => 2-tuple

11.    getpixel : 返回指定位置的像素,如果所打开的图像是多层次的图片,那这个方法就返回一个元组。

    
1 im.getpixel( xy ) => value or tuple

12.    histogram : 返回图像直方图,值为像素计数组成的列表。如果有参数 mask ,则返回图像所有部分的直方图。

    
1 im.histogram() => list 2 3 im.histogram( mask ) => list

13.    load : 版本 1.1.6 新添加的。load 返回对象的像素值,可以用来修改像素值。

    
1 im.load() 2 3  # 4  pix = im.load() 5  print pix[x, y] 6 pix[x, y] = value

14.    paste : 1). 粘贴新图片至图片中,box 参数可以为 2-元组(upper, left)或是 4-元组(left, upper, right, lower),或者是 None(0, 0)。2). 功能同上。不过是将指定位置填充为某种颜色。

复制代码
    
1 im.paste( image, box ) 2 3 im.paste( colour, box ) 4 5 im.paste( image, box, mask ) 6 7 im.paste( colour, box, mask )
复制代码

15.    point : 

    
1 im.point( bable ) => image 2 im.point( function ) => image

  ------------

未完待续。

这篇关于PIL 中的 Image 模块的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1015516

相关文章

iptables(7)扩展模块state

简介         前面文章我们已经介绍了一些扩展模块,如iprange、string、time、connlimit、limit,还有扩展匹配条件如--tcp-flags、icmp。这篇文章我们介绍state扩展模块  state          在 iptables 的上下文中,--state 选项并不是直接关联于一个扩展模块,而是与 iptables 的 state 匹配机制相关,特

python 在pycharm下能导入外面的模块,到terminal下就不能导入

项目结构如下,在ic2ctw.py 中导入util,在pycharm下不报错,但是到terminal下运行报错  File "deal_data/ic2ctw.py", line 3, in <module>     import util 解决方案: 暂时方案:在终端下:export PYTHONPATH=/Users/fujingling/PycharmProjects/PSENe

[FPGA][基础模块]跨时钟域传播脉冲信号

clk_a 周期为10ns clk_b 周期为34ns 代码: module pulse(input clk_a,input clk_b,input signal_a,output reg signal_b);reg [4:0] signal_a_widen_maker = 0;reg signal_a_widen;always @(posedge clk_a)if(signal_a)

1_Image和Matrix的使用

参考博文: https://www.cnblogs.com/bomo/archive/2013/03/28/2986573.html

spring-boot-maven-plugin多模块install问题

一、问题描述:   项目分多个模块,open-eureka注册中心、open-provider服务提供者、open-common公共部分,provider依赖common。父pom使用spring-boot-maver-plugin插件,项目直接运行Main主类没问题,但是install报common中的类找不到符号. 二、查找问题:   spring-boot-maven-plugin 打

【QML】用 Image(QQuickPaintedItem) 显示图片

大体功能: 频繁地往界面推送图片,帧率达到视频效果。捕获画布上的鼠标事件和键盘事件。 代码如下: // DrawImageInQQuickPaintedItem.pro 代码如下:QT += quick# You can make your code fail to compile if it uses deprecated APIs.# In order to do so, uncom

上位机图像处理和嵌入式模块部署(mcu和swd接口)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】         最近学习mcu的时候,接触了不少调试器,这里面有daplink、st-link v2、j-link v9。虽然模块的形状可能不太一样,但是硬件的连线都差不多,都是mcu上的3.3v、clk、dio和gnd四根连线。出于好奇,今天花了点时间了解了一下debug port、sw

使用import和exec运行模块文件的异同

使用import和exec运行模块文件的异同 实例代码 #script1.pyimport sysprint(sys.platform)x = 'span'print(x*8) 在cmd中 >>> import script1win32spanspanspanspanspanspanspanspan >>> exec(open('script1.py').read())win

ansible setup模块

用于收集有关目标主机的系统和网络信息,并将这些信息存储为一个facts变量,可以在Playbook的后续任务中使用。setup模块可以用来获取主机的操作系统、软件包、IP地址、内存、磁盘和其他硬件信息。这些信息对编写Playbook和进行条件判断非常有用。当你在Playbook或者直接通过Ansible命令行使用setup模块时,它会返回一个包含目标主机详细信息的JSON结构,这些信息包括但不限于

ansibie yum模块

用于在远程主机上使用yum软件包管理器来安装、更新、删除和查询软件包的。它使得在大规模主机环境中进行软件包管理变得更加简单和自动化。 name: 必需参数,指定需要管理的软件包名称,例如 nginx、httpd 等。 state: 指定软件包应处于的状态,可选值包括: present 或 installed: 确保软件包被安装,这是默认值。latest: 确保软件包被安装,并且是最新版本。