python PIL Image 图像处理基本操作实例

1. 图片加载、灰度图、 显示和保存

from PIL import Image

img = Image.open('01.jpg')
imgGrey = img.convert('L')

img.show()
imgGrey.show()

img.save('img_copy.jpg')
imgGrey.save('img_gray.jpg')

2. 图片宽、高、通道模式、平均值获取

from PIL import Image
import numpy as np

img = Image.open('01.jpg')

width, height = img.size
channel_mode = img.mode
mean_value = np.mean(img)

print(width)
print(height)
print(channel_mode)
print(mean_value)

3. 创建指定大小,指定通道类型的空图像

from PIL import Image

width = 200
height = 100

img_white = Image.new('RGB', (width,height), (255,255,255))
img_black = Image.new('RGB', (width,height), (0,0,0))
img_L = Image.new('L', (width, height), (255))

img_white.show()
img_black.show()
img_L.show()

4. 访问和操作图像像素

from PIL import Image

img = Image.open('01.jpg')

width, height = img.size

# 获取指定坐标位置像素值
pixel_value = img.getpixel((width/2, height/2))
print(pixel_value)

# 或者使用load方法
pim = img.load()
pixel_value1 = pim[width/2, height/2]
print(pixel_value1)

# 设置指定坐标位置像素的值
pim[width/2, height/2] = (0, 0, 0)

# 或使用putpixel方法
img.putpixel((w//2, h//2), (255,255,255))

# 设置指定区域像素的值
for w in range(int(width/2) - 40, int(width/2) + 40):
for h in range(int(height/2) - 20, int(height/2) + 20):
pim[w, h] = (255, 0, 0)
# img.putpixel((w, h), (255,255,255))
img.show()

5. 图像通道分离和合并

from PIL import Image

img = Image.open('01.jpg')

# 通道分离
R, G, B = img.split()

R.show)
G.show()
B.show()

# 通道合并
img_RGB = Image.merge('RGB', (R, G, B))
img_BGR = Image.merge('RGB', (B, G, R))
img_RGB.show()
img_BGR.show()

6. 在图像上输出文字

from PIL import Image, ImageDraw, ImageFont

img = Image.open('01.jpg')

# 创建Draw对象:
draw = ImageDraw.Draw(img)
# 字体颜色
fillColor = (255, 0, 0)

text = 'print text on PIL Image'
position = (200,100)

draw.text(position, text, fill=fillColor)
img.show()

7. 图像缩放

from PIL import Image

img = Image.open('01.jpg')

width, height = img.size

img_NEARESET = img.resize((width//2, height//2)) # 缩放默认模式是NEARESET(最近邻插值)
img_BILINEAR = img.resize((width//2, height//2), Image.BILINEAR) # BILINEAR 2x2区域的双线性插值
img_BICUBIC = img.resize((width//2, height//2), Image.BICUBIC) # BICUBIC 4x4区域的双三次插值
img_ANTIALIAS = img.resize((width//2, height//2), Image.ANTIALIAS) # ANTIALIAS 高质量下采样滤波

8. 图像遍历操作

from PIL import Image

img = Image.open('01.jpg').convert('L')

width, height = img.size

pim = img.load()

for w in range(width):
for h in range(height):
if pim[w, h] > 100:
img.putpixel((w, h), 255)
# pim[w, h] = 255
else:
img.putpixel((w, h), 0)
# pim[w, h] = 0

img.show()

9. 图像阈值分割、 二值化

from PIL import Image

img = Image.open('01.jpg').convert('L')

width, height = img.size

threshold = 125

for w in range(width):
for h in range(height):
if img.getpixel((w, h)) > threshold:
img.putpixel((w, h), 255)
else:
img.putpixel((w, h), 0)

img.save('binary.jpg')

10. 图像裁剪

from PIL import Image

img = Image.open('01.jpg')

width, height = img.size

# 前两个坐标点是左上角坐标
# 后两个坐标点是右下角坐标
# width在前, height在后
box = (100, 100, 550, 350)

region = img.crop(box)

region.save('crop.jpg')

11. 图像边界扩展

# 边界扩展
from PIL import Image

img = Image.open('test.png')

width, height = img.size
channel_mode = img.mode

img_makeBorder_full = Image.new(channel_mode, (2*width, height))
img_makeBorder_part = Image.new(channel_mode, (width+200, height))

# 图像水平扩展整个图像
img_makeBorder_full.paste(img, (0, 0, width, height))
img_makeBorder_full.paste(img, (width, 0, 2*width, height))

# 前两个坐标点是左上角坐标
# 后两个坐标点是右下角坐标
# width在前, height在后
box = (width-200, 0, width, height)
region = img.crop(box)

# 图像水平右侧扩展一个ROI
img_makeBorder_part.paste(img, (0, 0, width, height))
img_makeBorder_part.paste(region, (width, 0, width+200, height))
img_makeBorder_part.show()
img_makeBorder_full.show()

12. PIL.Image 和 numpy 格式相互转换

from PIL import Image
import numpy as np

img = Image.open('01.jpg')

array = np.array(img) # PIL.Image 转 numpy

img1 = Image.fromarray(array) # numpy转 PIL.Image
img1 = Image.fromarray(array.astype('uint8'))

img1.save('from_array.jpg')

更多关于Python PIL Image图像处理基本操作实例请查看下面的相关链接

(0)

相关推荐

  • 在Python中使用PIL模块处理图像的教程

    PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装: $ sudo apt-get install python-imaging Mac和其他版本的Linux可以直接使用easy_install或pip安装,安装前需要把编译环境装好: $ sudo easy_install PIL 如果安装失败,根据提示先把缺失的包(比如ope

  • Python用Pillow(PIL)进行简单的图像操作方法

    Python用Pillow(PIL)进行简单的图像操作方法 颜色与RGBA值 计算机通常将图像表示为RGB值,或者再加上alpha值(通透度,透明度),称为RGBA值.在Pillow中,RGBA的值表示为由4个整数组成的元组,分别是R.G.B.A.整数的范围0~255.RGB全0就可以表示黑色,全255代表黑色.可以猜测(255, 0, 0, 255)代表红色,因为R分量最大,G.B分量为0,所以呈现出来是红色.但是当alpha值为0时,无论是什么颜色,该颜色都不可见,可以理解为透明. from

  • 使用PIL(Python-Imaging)反转图像的颜色方法

    利用PIL将图片转换为黑色与白色反转的图片,下面笔者小白介绍如何实现. 解决方案一: from PIL import Image import PIL.ImageOps #读入图片 image = Image.open('your_image.png') #反转 inverted_image = PIL.ImageOps.invert(image) #保存图片 inverted_image.save('new_name.png') 注意:"ImageOps模块包含多个'ready-made'图像

  • Python图像处理库PIL的ImageEnhance模块使用介绍

    ImageEnhance模块提供了一些用于图像增强的类. 一.ImageEnhance模块的接口 所有的增强类都实现了一个通用的接口,包括一个方法: enhancer.enhance(factor) ⇒ image 该方法返回一个增强过的图像.变量factor是一个浮点数,控制图像的增强程度.变量factor为1将返回原始图像的拷贝:factor值越小,颜色越少(亮度,对比度等),更多的价值.对变量facotr没有限制. 二.ImageEnhance模块的Color类 颜色增强类用于调整图像的颜

  • Python PIL读取的图像发生自动旋转的实现方法

    对于手机.相机等设备拍摄的照片,由于手持方向的不同,拍出来的照片可能是旋转0°.90°.180°和270°.即使在电脑上利用软件将其转正,他们的exif信息中还是会保留方位信息. 在用PIL读取这些图像时,读取的是原始数据,也就是说,即使电脑屏幕上显示是正常的照片,用PIL读进来后,也可能是旋转的图像,并且图片的size也可能与屏幕上的不一样. 对于这种情况,可以利用PIL读取exif中的orientation信息,然后根据这个信息将图片转正后,再进行后续操作,具体如下. from PIL im

  • Python图像处理PIL各模块详细介绍(推荐)

    Image模块 Image模块是在Python PIL图像处理中常见的模块,对图像进行基础操作的功能基本都包含于此模块内.如open.save.conver.show-等功能. open类 Image.open(file) ⇒ image Image.open(file, mode) ⇒ image 要从文件加载图像,使用 open() 函数, 在 Image 模块: @zhangziju from PIL import Image ##调用库 im = Image.open("E:\mywif

  • Python图像处理库PIL的ImageFont模块使用介绍

    ImageFont模块定义了相同名称的类,即ImageFont类.这个类的实例存储bitmap字体,用于ImageDraw类的text()方法. PIL使用自己的字体文件格式存储bitmap字体.用户可以使用pilfont工具包将BDF和PCF字体描述器(Xwindow字体格式)转换为这种格式. 从版本1.1.4开始,PIL可以配置是否支持TrueType和OpenType字体(和FreeType库支持其他的字体格式一样).对于更早的版本,只在imToolkit包中支持TrueType字体. T

  • Python编程中使用Pillow来处理图像的基础教程

    安装 刚接触Pillow的朋友先来看一下Pillow的安装方法,在这里我们以Mac OS环境为例: (1).使用 pip 安装 Python 库.pip 是 Python 的包管理工具,安装后就可以直接在命令行一站式地安装/管理各种库了(pip 文档). $ wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz $ tar xzf pip-0.7.2.tar.gz $ cd pip-0.7.2 $ python se

  • 详解python opencv、scikit-image和PIL图像处理库比较

    进行深度学习时,对图像进行预处理的过程是非常重要的,使用pytorch或者TensorFlow时需要对图像进行预处理以及展示来观看处理效果,因此对python中的图像处理框架进行图像的读取和基本变换的掌握是必要的,接下来python中几个基本的图像处理库进行纵向对比. 项目地址:https://github.com/Oldpan/Pytorch-Learn/tree/master/Image-Processing 比较的图像处理框架: PIL scikit-image opencv-python

  • Python Pillow.Image 图像保存和参数选择方式

    保存时代码如下: figure_corp = figure.crop( (32*rate/2, 32*rate/2, 32-32*rate/2, 32-32*rate/2)) figure.save('save_picture/picture.jpg',quality=95,subsampling=0) figure_corp.save('save_picture/picture_crop.jpg',quality=95,subsampling=0) 其中quality数值会影响图片的质量(1最

  • Python图像处理库PIL的ImageDraw模块介绍详解

    ImageDraw模块提供了图像对象的简单2D绘制.用户可以使用这个模块创建新的图像,注释或润饰已存在图像,为web应用实时产生各种图形. PIL中一个更高级绘图库见The aggdraw Module 一.ImageDraw模块的概念 1.  Coordinates 绘图接口使用和PIL一样的坐标系统,即(0,0)为左上角. 2.  Colours 为了指定颜色,用户可以使用数字或者元组,对应用户使用函数Image.new或者Image.putpixel.对于模式为"1","

  • Python图像处理库PIL详细使用说明

    一. 简介 1. 基本介绍 Pillow 是 Python 中较为基础的图像处理库,主要用于图像的基本处理,比如裁剪图像.调整图像大小和图像颜色处理等.与 Pillow 相比,OpenCV 和 Scikit-image 的功能更为丰富,所以使用起来也更为复杂,主要应用于机器视觉.图像分析等领域,比如众所周知的“人脸识别”应用 . 2. 特点 支持格式繁多 Pillow 支持广泛的图像格式,比如 "jpeg","png","bmp","g

  • Python图像处理库PIL的ImageGrab模块介绍详解

    ImageGrab模块用于将当前屏幕的内容或者剪贴板上的内容拷贝到PIL图像内存. 当前版本只支持windows系统. 一.ImageGrab模块的函数 1.  Grab 定义:ImageGrab.grab()⇒ image ImageGrab.grab(bbox) ⇒ image 含义:(New in 1.1.3)抓取当前屏幕的快照,返回一个模式为"RGB"的图像.参数边界框用于限制只拷贝当前屏幕的一部分区域. 例子: >>> from PIL importImag

  • python使用pil进行图像处理(等比例压缩、裁剪)实例代码

    PIL中设计的几个基本概念 1.通道(bands):即使图像的波段数,RGB图像,灰度图像 以RGB图像为例: >>>from PIL import Image >>>im = Image.open('*.jpg') # 打开一张RGB图像 >>>im_bands = im.g etbands() # 获取RGB三个波段 >>>len(im_bands) >>>print im_bands[0,1,2] # 输出RG

随机推荐