PIL-图像处理

参考:
1、https://python-pillow.org/
2、https://github.com/python-pillow/Pillow
3、http://pillow.readthedocs.io/en/4.3.x/

1、使用Image类

加载图像

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from __future__ import print_function

from PIL import Image

im=Image.open('Lenna.png') # 打开图像

print(type(im)) # <class 'PIL.PngImagePlugin.PngImageFile'>

print(im.format, im.size, im.mode) # PNG (512, 512) P

im.show()

2、读写图像

将文件转换为JPEG

from __future__ import print_function
import os, sys
from PIL import Image

for infile in sys.argv[1:]:
    f, e = os.path.splitext(infile)
    outfile = f + ".jpg"  # ".tif" 保存成tif格式
    if infile != outfile:
        try:
            Image.open(infile).save(outfile)
        except IOError:
            print("cannot convert", infile)

创建JPEG缩略图

from __future__ import print_function
import os, sys
from PIL import Image

size = (128, 128)

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size)
            im.save(outfile, "JPEG")
        except IOError:
            print("cannot create thumbnail for", infile)

识别图像文件

from __future__ import print_function
import sys
from PIL import Image

for infile in sys.argv[1:]:
    try:
        with Image.open(infile) as im:
            print(infile, im.format, "%dx%d" % im.size, im.mode)
    except IOError:
        pass

3、剪切、粘贴、合并图像

从图像复制子矩形

box = (100, 100, 400, 400)
region = im.crop(box)

处理一个子矩形,并将其粘贴回来

region = region.transpose(Image.ROTATE_180) # 旋转180度
im.paste(region, box)

完整代码:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

from __future__ import print_function
import os, sys
from PIL import Image


im=Image.open('Lenna.png')
box = (100, 100, 400, 400)
region = im.crop(box)

region = region.transpose(Image.ROTATE_180) # 旋转180度
im.paste(region, box)

im.show()

滚动图像

def roll(image, delta):
    "Roll an image sideways"

    xsize, ysize = image.size

    delta = delta % xsize
    if delta == 0: return image

    part1 = image.crop((0, 0, delta, ysize))
    part2 = image.crop((delta, 0, xsize, ysize))
    part1.load()
    part2.load()
    image.paste(part2, (0, 0, xsize-delta, ysize))
    image.paste(part1, (xsize-delta, 0, xsize, ysize))

    return image

波段拆分和合并

r, g, b = im.split() # 灰度图像返回是其本身
im = Image.merge("RGB", (b, g, r))

4、几何变换

简单的几何变换

out = im.resize((128, 128)) # 调整大小
out = im.rotate(45) # degrees counter-clockwise # 逆时针旋转

Transposing an image

out = im.transpose(Image.FLIP_LEFT_RIGHT) # 左右翻转
out = im.transpose(Image.FLIP_TOP_BOTTOM) # 上下翻转
out = im.transpose(Image.ROTATE_90) # 逆时针旋转90度
out = im.transpose(Image.ROTATE_180)# 逆时针旋转180度
out = im.transpose(Image.ROTATE_270)# 逆时针旋转270度

5、颜色变换

转换模式

from PIL import Image
im = Image.open("hopper.ppm").convert("L") # 转成灰度图

6、图像增强

、滤波

使用滤波器

from PIL import ImageFilter
out = im.filter(ImageFilter.DETAIL)

点操作

应用点变换

# multiply each pixel by 1.2
out = im.point(lambda i: i * 1.2)

单个波段处理

# split the image into individual bands
source = im.split()

R, G, B = 0, 1, 2

# select regions where red is less than 100
mask = source[R].point(lambda i: i < 100 and 255)

# process the green band
out = source[G].point(lambda i: i * 0.7)

# paste the processed band back, but only where red was < 100
source[G].paste(out, None, mask)

# build a new multiband image
im = Image.merge(im.mode, source)

增强

图像增强

from PIL import ImageEnhance

enh = ImageEnhance.Contrast(im)
enh.enhance(1.3).show("30% more contrast")

图像序列

读取序列

from PIL import Image

im = Image.open("animation.gif")
im.seek(1) # skip to the second frame

try:
    while 1:
        im.seek(im.tell()+1)
        # do something to im
except EOFError:
    pass # end of sequence

使用ImageSequence迭代器类

from PIL import ImageSequence
for frame in ImageSequence.Iterator(im):
    # ...do something to frame...

7、Postscript printing

绘画后记

from PIL import Image
from PIL import PSDraw

im = Image.open("hopper.ppm")
title = "hopper"
box = (1*72, 2*72, 7*72, 10*72) # in points

ps = PSDraw.PSDraw() # default is sys.stdout
ps.begin_document(title)

# draw the image (75 dpi)
ps.image(box, im, 75)
ps.rectangle(box)

# draw title
ps.setfont("HelveticaNarrow-Bold", 36)
ps.text((3*72, 4*72), title)

ps.end_document()

8、More on reading images

from PIL import Image
im = Image.open("hopper.ppm")

Reading from an open file

from PIL import Image
with open("hopper.ppm", "rb") as fp:
    im = Image.open(fp)

Reading from a string

import StringIO

im = Image.open(StringIO.StringIO(buffer))

从tar存档读取

from PIL import Image, TarIO

fp = TarIO.TarIO("Tests/images/hopper.tar", "hopper.jpg")
im = Image.open(fp)

9、控制解码器

Reading in draft mode

from PIL import Image
from PIL import PSDraw

im = Image.open("home.jpg")
print("original =", im.mode, im.size)
# original = RGB (512, 384)

im.draft("L", (100, 100))
print("draft =", im.mode, im.size)
# draft = L (256, 192)
im.show()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: pil-1.1.7.win32-py2.7.exe是一个Python Imaging Library的安装程序。Python Imaging Library(简称PIL)是一个提供图像处理功能的开源库,它允许开发者在Python编程环境下进行图像处理、图像编辑、图像生成和图像显示等操作。 pil-1.1.7.win32-py2.7.exe是特定版本的PIL安装程序,在Windows 32位操作系统上以及使用Python 2.7版本的环境中使用。这个安装程序的目的是方便用户将PIL库安装到相应的开发环境中,以便后续能够利用PIL库中提供的丰富功能来处理图像。 安装pil-1.1.7.win32-py2.7.exe的过程相对简单,只需双击该文件即可开始安装过程。安装程序将会引导用户完成所有必要的安装步骤,包括选择安装目录、确认安装选项等。安装完成后,用户可以在Python的开发环境中使用import语句导入PIL库,并利用其提供的丰富的函数和方法进行图像处理任务。 总的来说,pil-1.1.7.win32-py2.7.exe是一个方便的PIL库的安装程序,适用于Windows 32位操作系统和Python 2.7版本的环境中。安装这个程序可以使开发者轻松地将PIL库集成到自己的Python项目中,并享受其提供的图像处理功能。 ### 回答2: pil-1.1.7.win32-py2.7.exe是一个Python图像处理库(Python Imaging Library)的安装文件。这个文件适用于Windows 32位操作系统和Python 2.7版本。Python Imaging Library是一个强大的图像处理库,它提供了许多图像处理和操作的功能,包括图像缩放、剪裁、旋转、滤镜效果、颜色转换等等。 用户可以通过运行这个.exe文件来安装pil-1.1.7.win32-py2.7库到本地的Python开发环境中。安装完成后,用户就可以在自己的Python脚本中使用PIL库提供的各种功能来处理图像。这个库对于进行图像编辑、处理和分析等任务非常有用。 在使用PIL库之前,用户需要先确保已经安装了Python 2.7版本,并且操作系统是Windows 32位。然后,用户可以下载pil-1.1.7.win32-py2.7.exe文件,并双击运行该文件进行安装。安装过程中会有一些设置选项和安装路径可以选择,用户可以根据自己的需要进行配置。 一旦PIL库安装完成,用户就可以在Python开发环境中导入该库并开始使用它的各种功能了。用户可以使用PIL库来读取和保存图像文件,对图像进行几何变换和颜色处理,以及应用各种滤镜和效果等。这个库还提供了一些更高级的功能,如图像分割、特征提取和图像识别等。 总之,pil-1.1.7.win32-py2.7.exe是一个安装文件,用于将Python Imaging Library(PIL)库安装到Windows 32位操作系统和Python 2.7版本的开发环境中,以便用户可以使用该库进行各种图像处理和操作。 ### 回答3: pil-1.1.7.win32-py2.7.exe是一种用于安装Python Imaging Library(PIL)的可执行文件。PIL是一个强大的Python图像处理库,它提供了丰富的图像处理功能,可以对图像进行加载、编辑、保存和显示等操作。 PIL-1.1.7是PIL库的一个特定版本,而win32表示这个可执行文件适用于Windows操作系统的32位版本。py2.7表示该版本适用于Python 2.7版本。 要使用pil-1.1.7.win32-py2.7.exe安装PIL库,首先需要下载该可执行文件。然后,双击运行它,它会自动安装PIL库到你的Python环境中。你也可以选择其他安装目录,只需在安装过程中进行相应设置即可。 安装完成后,你就可以在Python程序中使用PIL库了。通过引入PIL库,你可以使用它提供的各种函数和方法来处理图像,比如加载图像文件、调整图像大小、旋转图像、裁剪图像、应用滤镜等等。你还可以将处理后的图像保存成文件,或者在程序中显示出来。 总之,pil-1.1.7.win32-py2.7.exe是安装Python Imaging Library(PIL)的可执行文件,它可以帮助你在Python 2.7版本的Windows操作系统上使用PIL库进行图像处理

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值