Python图像编程(一)

news/2024/7/21 3:57:23 标签: 图像处理, PIL, pillow

PIL_0">PIL

PIL(Python Imaging Library)是Python图像处理模块,支持多种格式,提供强大图像处理能力,可通过pip安装,PIL仅支持到Python 2.7,但pillow完美支持Python3,pillow实在PIL基础上创建的兼容版本,可以直接安装pillow
如果安装了anaconda可以直接使用pillow

 pip install pillow

简单操作

如果安装了Anaconda3,可以在\Anaconda3\Lib\site-packages\PIL查看具体模块。
在windows环境下im.show使用windows自带的图像显示,可以使用matplotlib显示图像
以下代码可以改变plt.imshow(img12)中的参数查看图像效果

from PIL import Image
im = Image.open('C:\\Users\\WilliamZhang\\pythonLearn\\1.png')
im.show();
print(im.format)
print(im.size)
print(im.getpixel((100,50)))
print(im.histogram())

在这里插入图片描述

from PIL import Image
from random import randint as rint
img = Image.open('C:\\Users\\WilliamZhang\\pythonLearn\\test.bmp');
for w in range(200,280):
  for h in range(100,200):
    r = rint(0,255)
    g = rint(0,255)
    b = rint(0,255)
    img.putpixel((w,h),(r,g,b))
# img.show()
for w in range(200,280):
  for h in range(100,200):
    color = img.getpixel((w,h))
    img.putpixel((w+200,h),color)
img.show();

test.bmp是一张空白图片,利用随机数生成随机图,并移动200px
在这里插入图片描述

from PIL import Image
from PIL import ImageFilter
from PIL import ImageEnhance
from matplotlib import pyplot as plt
im = Image.open('C:\\Users\\WilliamZhang\\pythonLearn\\1.png')
# 缩放,重新生成一张图片
img1 = im.resize((100,100))
# rotate任意角度旋转,transpose支持特殊角度旋转
img2 = im.rotate(90)
img3 = im.transpose(Image.FLIP_TOP_BOTTOM)
img4 = im.transpose(Image.FLIP_LEFT_RIGHT)
img5 = im.transpose(Image.ROTATE_180)
# 图像增强
img6 = im.filter(ImageFilter.DETAIL)
# 图像模糊
img7 = im.filter(ImageFilter.BLUR)
# 边缘提取
img8 = im.filter(ImageFilter.FIND_EDGES)
# plt.imshow(img8)
# 点运算,整体变亮变暗
img9 = im.point(lambda i:i*1.3)
img10 = im.point(lambda i:i*0.7)
# 或使用图像增强模块ImageEnhance 增强对比度
enh = ImageEnhance.Contrast(im)
img11 = enh.enhance(1.3)
# 冷暖色调调整
r,g,b = im.split()
r = r.point(lambda i:i*1.3)
g = g.point(lambda i:i*0.9)
b = b.point(lambda i:0)
img12 = Image.merge(im.mode,(r,g,b))
plt.imshow(img12)
plt.show()

冷暖色调调整图片,剩下的可以修改plt.imshow(img12)查看相应效果
在这里插入图片描述
图片增强的效果,整体变亮1.3倍
在这里插入图片描述
图片整体变暗的效果
在这里插入图片描述
图片模糊的效果
在这里插入图片描述
边缘检测
在这里插入图片描述
刚开始学习,记录一下。


http://www.niftyadmin.cn/n/1759144.html

相关文章

Qt5.12.3 QSqlDatabase: QMYSQL driver not loaded的解决方法

Qt5.12.3 QSqlDatabase: QMYSQL driver not loaded的解决方法问题解决方案总体把握在Qt中重新编译mysql数据库驱动总结问题 最近因为工作需要,开始进行Qt桌面应用程序的开发,但是在安装了Qt5.12.3,利用Qt Creator进行开发的过程中&#xff0…

[Qt5.12.3] 使用Chart模块中warning: ‘setAxisX‘ is deprecated, warning: ‘setAxisY‘ is deprecated的解决办法

文章目录问题描述解决方案后记问题描述 在使用QtCharts模块绘图的过程中,X轴需要显示DateTime,不能使用默认的createDefaultAxes();遂使用了setAxisX,setAxisY方法,但一直报方法被弃用,虽暂时程序可以使用&#xff0c…

JavaWeb_Cookie

会话和会话状态 WEB应用中的会话是指一个客户端浏览器与WEB服务器之间连续发生的一系列请求和响应过程。 WEB应用的会话状态是指WEB服务器与浏览器在会话过程中产生的状态信息,借助会话状态,WEB服务器能够把属于同一会话中的一系列的请求和响应过程关联起…

RSA公钥秘钥算法

RSA公钥秘钥算法RSA公钥密钥算法总结RSA是什么?RSA算法原理RSA算法模拟RSA的应用:数字签名RSA算法的缺点RSA公钥密钥算法总结 RSA算法既能用于数据加密也能用于数字签名,它是素数的典型应用。 RSA是什么? 计算机的加密技术分为…

Pku1950【Dessert】

描述 John奶牛排队进餐制定了新的规定。N (3 < N < 15) 头奶牛不仅要按秩序站成一排,而且要每两头牛之间间隔一张标有符号""、"-"、或"."的餐巾纸。为了获得甜点&#xff0c;奶牛数字和餐巾纸上的符号组成的算式要得到结果0。标有符号&quo…

Spiking-YOLO:脉冲神经网络高效的目标检测

Spiking-YOLO: Spiking Neural Network for Energy-Efficient Object Detection |AAAI 2020 Spiking-YOLO:脉冲神经网络高效的目标检测AbstractIntroductionRelated workDNN-to-SNN conversionObject detectionMethodsChannel-wise data-based normalizationConventional norma…

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer的解决方法

java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer的解决方法问题解决方案问题 在使用hibernate5的session.createNativeQuery进行原生sql查询时&#xff0c;返回一个Object[]数组&#xff0c;利用new构造返回对象&#xff0c;查询时报…

输入一个正整数,输出2000年1月1日经过该整数天后的日期.

//输入一个正整数&#xff0c;输出2000年1月1日经过该整数天后的日期.已测试&#xff0c;输入值可以为0~1095727 //如&#xff0c;100天后&#xff0c;日期为2000 4 10#include<stdio.h>#define MAX_YEAR 5000//年数可以从2000一直到4999年。//函数功能&#xff1a;求解第…