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

 更新时间:2022年04月08日 16:37:23   作者:-牧野-  
这篇文章主要介绍了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图像处理基本操作实例请查看下面的相关链接

相关文章

  • Python中的“_args”和“__kwargs”用法详解

    Python中的“_args”和“__kwargs”用法详解

    *args 和 **kwargs 主要⽤于函数定义,你可以将不定数量的参数传递给⼀个函数,这篇文章主要介绍了Python中的“_args”和“__kwargs”用法,需要的朋友可以参考下
    2023-01-01
  • Python 包含汉字的文件读写之每行末尾加上特定字符

    Python 包含汉字的文件读写之每行末尾加上特定字符

    这篇文章主要介绍了Python 包含汉字的文件读写之每行末尾加上特定字符的相关资料,需非常不错,具有参考借鉴价值,要的朋友可以参考下
    2016-12-12
  • Python中set与frozenset方法和区别详解

    Python中set与frozenset方法和区别详解

    这篇文章主要介绍了Python中set与frozenset方法和区别详解的相关资料,需要的朋友可以参考下
    2016-05-05
  • python密码学Base64编码和解码教程

    python密码学Base64编码和解码教程

    这篇文章主要为大家介绍了python密码学Base64编码和解码教程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • python绘制封闭多边形教程

    python绘制封闭多边形教程

    今天小编就为大家分享一篇python绘制封闭多边形教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-02-02
  • 神经网络训练采用gpu设置的方式

    神经网络训练采用gpu设置的方式

    这篇文章主要介绍了神经网络训练采用gpu设置的方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • Python处理PPT文件的实用知识点总结

    Python处理PPT文件的实用知识点总结

    python是一门很强大的语言,因为有着丰富的第三方库,所以可以说Python是无所不能的,下面这篇文章主要给大家介绍了关于用Python提取PPT中图片的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2023-01-01
  • Python+Tkinter制作在线个性签名工具

    Python+Tkinter制作在线个性签名工具

    这篇文章主要为大家分享如何利用Python中的Tkinter库制作一个简易的在线个性签名生成工具,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-03-03
  • numpy库reshape用法详解

    numpy库reshape用法详解

    这篇文章主要介绍了numpy库reshape用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Python中对象的引用与复制的使用

    Python中对象的引用与复制的使用

    引用和复制是Python处理对象的重要概念,本文主要介绍了Python中对象的引用与复制的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-12-12

最新评论