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

# Created by 牧野 CSDN
from PIL import Imageimg = Image.open('01.jpg')
imgGrey = img.convert('L')img.show()
imgGrey.show()img.save('img_copy.jpg')
imgGrey.save('img_gray.jpg')

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

# # Created by 牧野 CSDN
from PIL import Image
import numpy as npimg = 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. 创建指定大小,指定通道类型的空图像

# Created by 牧野 CSDN
from PIL import Imagewidth = 200
height = 100img_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. 访问和操作图像像素

# Created by 牧野 CSDN
from PIL import Imageimg = 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. 图像通道分离和合并

# Created by 牧野 CSDN
from PIL import Imageimg = 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. 在图像上输出文字

# Created by 牧野 CSDN
from PIL import Image, ImageDraw, ImageFontimg = 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. 图像缩放

# Created by 牧野 CSDN
from PIL import Imageimg = Image.open('01.jpg')width, height = img.sizeimg_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. 图像遍历操作

# Created by 牧野 CSDN
from PIL import Imageimg = Image.open('01.jpg').convert('L')width, height = img.sizepim = 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] = 255else:img.putpixel((w, h), 0)# pim[w, h] = 0img.show()

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

# Created by 牧野 CSDN
from PIL import Imageimg = Image.open('01.jpg').convert('L')width, height = img.sizethreshold = 125for 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. 图像裁剪

# Created by 牧野 CSDN
from PIL import Imageimg = Image.open('01.jpg')width, height = img.size# 前两个坐标点是左上角坐标
# 后两个坐标点是右下角坐标
# width在前, height在后
box = (100, 100, 550, 350)region = img.crop(box)region.save('crop.jpg')

11. 图像边界扩展

# Created by 牧野 CSDN
# 边界扩展
from PIL import Imageimg = Image.open('test.png')width, height = img.size
channel_mode = img.modeimg_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 格式相互转换

# Created by 牧野 CSDN
from PIL import Image
import numpy as npimg = Image.open('01.jpg')array = np.array(img)  # PIL.Image 转 numpyimg1 = Image.fromarray(array)  # numpy转 PIL.Image
img1 = Image.fromarray(array.astype('uint8'))img1.save('from_array.jpg')

python PIL Image 图像处理基本操作相关推荐

  1. Python PIL(图像处理库)使用方法

    介绍 PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了.  from PIL import Image, ImageDraw 安装 pip3 ins ...

  2. python PIL.Image 图像处理

    图像处理 图像的RGB色彩模式 图像一般使用RGB色彩模式,即每个像素点的颜色由RGB组成(红.绿.蓝) RGB三个颜色通道的变化和叠加得到各种颜色(256^3=1.68e+7),其中 R红色,取值范 ...

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

    PIL中设计的几个基本概念 1.通道(bands):即使图像的波段数,RGB图像,灰度图像 以RGB图像为例: >>>from PIL import Image >>&g ...

  4. python压缩图片像素_python使用pil进行图像处理(等比例压缩、裁剪)实例代码

    PIL中设计的几个基本概念 1.通道(bands):即使图像的波段数,RGB图像,灰度图像 以RGB图像为例: 2.模式(mode):定义了图像的类型和像素的位宽.共计9种模式: 3.尺寸(size) ...

  5. 数字图像处理——实验一 Python中数字图像处理的基本操作

    数字图像处理--实验一 Python中数字图像处理的基本操作 一.实验目的 二.实验主要仪器设备 三.实验原理 3.1 数字图像的表示和类别 3.2 opencv-python图像文件格式 四.实验内 ...

  6. Python图像处理基本操作

    在Python中进行图像处理可以使用的库有很多,本文主要介绍下面三个: OpenCV(Open Source Computer Vision Library) PIL(Python Imaging L ...

  7. python pil怎么安装_python pil 怎么安装 怎样安装python的图像处理库pillow

    python 怎么安装pillow 1. 安装pip [plain] view plain copy sudo easy_install pip pip 安装成功就可以直接安装pil或者pillow ...

  8. Python练习 | Python之图像的基本操作和处理

    博主github:https://github.com/MichaelBeechan 博主CSDN:https://blog.csdn.net/u011344545 ***************** ...

  9. Python中常用图像处理库

    Python中常用图像处理库 文章目录 Python中常用图像处理库 1 OpenCV-Python 2 Numpy 3 matplotlib 4 skimage 5 PIL(Python Imagi ...

  10. python学习-Pillow图像处理

    python学习-Pillow图像处理 Pillow中文文档:https://pillow-cn.readthedocs.io/zh_CN/latest/handbook/tutorial.html ...

最新文章

  1. python3与python2的编码问题
  2. JAVA——关闭ServerSocket
  3. 这里有10个优质Python开源项目,来帮你学好Python
  4. LeetCode 451. 根据字符出现频率排序(Sort Characters By Frequency)
  5. Python求单词长度并根据长度排序
  6. Java50道经典习题-程序7 处理字符串
  7. 公司账号密码、通信录泄露屡见不鲜,肆意流淌的敏感信息:WEB安全基础入门—信息泄露漏洞
  8. 状态方程和特性方程的区别
  9. python画图入门——for循环及调色盘的应用
  10. 外贸里面 LC TT DP DA BG 是什么
  11. Chris Hadfield現身《ABS 2020》,各方菁英和THORBOT 雷神量化機器人一同進行深入探討
  12. 魔方还原算法(三) 上帝算法
  13. 【ClearCase 学习笔记 】之(4/4)CC基本操作介绍
  14. 字节LastDay,告别十个月的实习生活,流水账与一些思考
  15. 拿到webshell后的提权详解
  16. Android九宫格解锁的实现
  17. Redis-benchmark性能测试工具使用详解
  18. Three.js - 使用 bumpMap 凹凸贴图创建皱纹
  19. matlab微分方程实例,Matlab常微分方程的解法
  20. JAVA计算机毕业设计汽车客运站票务管理系统(附源码、数据库)

热门文章

  1. 【设计模式】职责链模式:如果第三方短信平台挂了怎么办?
  2. python神经网络编程 代码,python神经网络编程 豆瓣
  3. 我要创办一家公司,干翻JetBrains和IDEA!
  4. [导入]关于Gmail无法链接的解决方法。
  5. 公众号后台管理系统php开发,微信公众平台开发管理后台开发
  6. 单片机 重要英文缩写解释
  7. 基于图神经网络的图像分类,遥感图像分析
  8. 《捷哥浅谈Drupal》第三弹之Drupal 7基本功能概述
  9. 企业如何选择?网站建设中常见的几种类型
  10. 如何保持精力充沛_在家工作,如何管理一支精力充沛,精力充沛的日常团队,远程站起来...