python PIL Image 图像处理基本操作_image.bicubic-程序员宅基地

技术标签: python  OpenCV-Python  PIL Image  

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

# Created by 牧野 CSDN
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. 图片宽、高、通道模式、平均值获取

# # Created by 牧野 CSDN
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. 创建指定大小,指定通道类型的空图像

# Created by 牧野 CSDN
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. 访问和操作图像像素

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

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

# Created by 牧野 CSDN
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. 图像缩放

# Created by 牧野 CSDN
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. 图像遍历操作

# Created by 牧野 CSDN
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. 图像阈值分割、 二值化

# Created by 牧野 CSDN
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. 图像裁剪

# Created by 牧野 CSDN
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. 图像边界扩展

# Created by 牧野 CSDN
# 边界扩展
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 格式相互转换

# Created by 牧野 CSDN
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')

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/dcrmg/article/details/102963336

智能推荐

Android 监听音频焦点变化-程序员宅基地

文章浏览阅读468次,点赞22次,收藏16次。在 Android 中,监听音频焦点变化意味着您可以获得关于音频焦点状态的通知,并针对焦点变化执行相应的操作。音频焦点是指哪个应用程序或组件有权播放音频的能力。

Linux基础之初识SHELL脚本_shell脚本-gf-程序员宅基地

文章浏览阅读10w+次。Shell脚本(Shell Script )可以简单理解为多个LINUX命令的堆砌,与Windows/Dos下的批处理相似_shell脚本-gf

IPD思想指导下的企业研发管理成熟度演进模型-程序员宅基地

文章浏览阅读2.4k次。基于IPD研究(书籍、网络文章、个人实践),参考CMMI框架,输出本研发管理能力成熟度框架。_研发管理成熟度

linux下Js加载so,JavaScript文件加载器LABjs API详解-程序员宅基地

文章浏览阅读478次。在《高性能JavaScript》一书中提到了LABjs这个用来加载JavaScript文件的类库,LABjs是Loading And Blocking JavaScript的缩写,顾名思义,加载和阻塞JavaScript,而它的API script()和wait()则优雅地实现了这两个功能,我在高性能JavaScript 加载和执行一文中也简单讲解了这两个核心API的用法。当然,LABjs还有更多..._so打包到js

Rancher 1.6 实战-程序员宅基地

文章浏览阅读1.7k次。为什么80%的码农都做不了架构师?>>> ..._rancher 1.6

Pycharm+Anaconda 之jupyter notebook内核配置,解决内核不匹配等错误_笔记本内核和代码不匹配-程序员宅基地

文章浏览阅读5.8k次,点赞9次,收藏26次。首先确保已经安装了annaconda和pycharm软件,可百度相关教程。1.安装notebook在独立anaconda环境中,假设环境名为Jupyter,安装notebook,已安装的见下一步。(Jupyter) E:\Pywork> conda install notebook # 或 pip install notebook2.检查ipython是否安装默认安装jupyter notebook包时会安装关联的ipython包,为确保此处无问题,可以检查一下,在Jupyter_笔记本内核和代码不匹配

随便推点

2021-08-03以CSU18MB86芯片做主控开发打气泵方案_csu18p88 气泵方案-程序员宅基地

文章浏览阅读170次。  汽车轮胎打气泵方案功能介绍:    显示方式:LED显示,测量和设置同屏显示。    单位:4个,循环切换顺序PSI、KPA、 Bar、 Kg/cm2    测量范围:    3.0 ~ 150.0PSI; 20 ~ 1030KPA    0.2 ~ 10.30Bar; 0.2 ~ 10.50Kg/cm2    设置范围:    3 ~ 120PSI; 20 ~ 825KPA    0.2 ~ 8.25Bar; 0.2 ~ 10.50 Kg/cm2    测量精度:+_csu18p88 气泵方案

ansible的安装与使用_ansible 认不到已经安装的python-netaddr-程序员宅基地

文章浏览阅读112次。# 安装epel库# yum -y install epel-release# 安装pip,python3自带pip3# yum -y install python-pip# 升级pippip install --upgrade pip# 检查版本pip --versionpip 19.0.3 from /usr/lib/python2.7/site-packages/pip (p..._ansible 认不到已经安装的python-netaddr

sonar-程序员宅基地

文章浏览阅读61次。1.maven的setting.xml中<profiles> <profile> <id>sonar</id> <activation> <activeByDefault>true</activeByDefault> </activation> <proper...

linux mysql源,linux 安装 mysql8+ rpm源方式-程序员宅基地

文章浏览阅读137次。系统 更新yum upgrade卸载rpm -qa|grep -i mysqlrpm -ev mysql-community-common-5.7.27-1.el7.x86_64 mysql-community-server-5.7.27-1.el7.x86_64 mysql-community-server-5.7.27-1.el7.x86_64 mysql-community-libs-5.7..._yum拉取msyql8的rpm源

Java Reflection Tutorial for Classes, Methods, Fields, Constructors, Annotations and much more_reports all fields methods 哦日期classes,found in the-程序员宅基地

文章浏览阅读1k次。Reflection in java provides ability to inspect and modify the runtime behavior of applications. Reflection is one of the advance topic of core java. Using reflection we can inspect a class,interface_reports all fields methods 哦日期classes,found in the specified

HomeAssistant实现对UPS的数据监控_network ups tools-程序员宅基地

文章浏览阅读4.4k次。之前写在《米家接入HomeKit系列》系列文章的时候,我们讲解了如何使用通过HomeAssistant和HomeBridge来将米家设备接入HomeKit中。细心的同学应该发现我在讲解米家接入HomeKit系列二:通过群辉NAS的Docker搭建HomeAssistant章节最后的配置截图中有个UPS的配置。于是就有玩NAS的小伙伴问我UPS如何接入HomeAssistant。那么今天我就特意写一篇文章给大家介绍下我们怎样才能够将UPS接入到HomeAssistant中进行展示监控。_network ups tools

推荐文章

热门文章

相关标签