目前主流的图像库有几下几种:

1. OpenCV      2. PIL(Pillow)       3. matplotlib.image     4. skimage      5. scipy.misc

结论:以上图片库中当属OpenCV最为强大,成熟。

1.1 OpenCV 图像的读取与储存

import cv2
#读取图像 直接是numpy矩阵格式
img = cv2.imread('horse.jpg',1) # 0表示读入灰色图片,1表示读入彩色图片
cv2.imshow('image',img) # 显示图像
print(img.shape) # (height,width,channel)
print(img.size) # 像素数量
print(img.dtype) # 数据类型
print(img) # 打印图像的numpy数组,3纬数组 #储存图像
# 当前目录储存
cv2.write(‘horse1.jpg',img)
# 自定义储存
cv2.write(‘/path_name/’ + str(image_name) + '.jpg',img) cv2.waitKey()

1.2OpenCV 图像灰化处理

import cv2
#方法一
img = cv2.imread('horse.jpg',0) # 0表示读入灰色图片,或者使用cv2.IMREAD_GRATSCALE 替代0
cv2.imshow('gray image',img) #方法二
img = cv2.imread('horse.jpg')
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray image',gray_img) print(gray_img.shape) # (height, width)
print(gray_img.size) # 像素数量
print(gray_img) # 打印图像的numpy数组,2维
cv2.waitKey()

1.3 OpenCV 矩阵格式变换

Why?:OpenCV的矩阵格式 (height, width, channels) -->> 深度学习矩阵类型可能是 (channels,height,width)

import cv2
import numpy as np
img = cv2.imread('horse.jpg',1)
cv2.imshow('image',img)
# 矩阵格式的变换
print(img.shape)
img = img.transpose(2,0,1) #变换函数
print(img.shape)
# 矩阵扩展 (batch_size, channels, height, width) 预测单张图片的操作
# 加一列作为图片的个数
img = np.expand_dims(img, axis=0) #使用numpy函数
print(img.shape)
# 训练阶段构建batch
data_lst = []
loop:
img = cv2.imread('xxx.jpg')
data_lst.append(img)
data_arr = np.array(data_lst)

1.4 OpenCV 图片归一化 (Data Normalization)

import cv2
# 为了减少计算量,需要把像素值0-255转换到0-1之间
img = cv2.imread('horse.jpg')
img = img.astype('float') / 255.0 # 先转化数据类型为float
print(img.dtype)
print(img)

1.5 OpenCV BRG转换为RGB

import cv2
img = cv2.imread('horse.jpg')
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # 转为RGB format
print(img)

1.6 OpenCV 访问像素点

import cv2
img = cv2.imread('horse.jpg')
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # 转为Gray image
print(img[4,4]) # 3 channels
print(gray_img[4,4]) # 1 channel

1.7 OpenCV 感兴趣区域剪切(ROI)

import cv2
img = cv2.imread('horse.jpg')
print(img.shape)
roi = img[0:437,0:400] # [y:height,x:width] cv2.imshow('roi',roi)
cv2.waitKey()

2.1 PIL 图像读取与储存

from PIL import Image
import numpy as np
#图像读取
img = Image.open('horse.jpg')
print(img.format) # 图片格式
print(img.size) # (width,height)
print(img.mode) # 图片通道类型 #将图像转化为矩阵格式
arr = np.array(img)
print(arr.shape)
print(arr.dtype) #图像储存
new_img = Image.fromarray(arr)
new_img.save('test.jpg') img.show()

2.2 PIL 图像灰化处理

#图像灰化处理
gray = Image.open('horse.jpg').convert('L')
gray_arr = np.array(gray)
print(gray_arr.shape) # (height,width)
print(gray_arr.dtype)
print(gray_arr)
gray.show()

2.3 PIL 感兴趣区域剪切

# 感兴趣区域剪切
img = Image.open('horse.jpg')
roi = img.crop((0,0,200,200)) # (左上x,左上y,右下x,右下y)
roi.show()

2.4 通道操作

# 通道处理
r,g,b = img.split() #分离
img = Image.merge("RGB",(b,g,r)) #合并
img = img.copy() #复制

3.1 Matplotlib 读取和存储图片

import matplotlib.pyplot as plt
import numpy as np
# 图像读取为numpy数组格式
img = plt.imread('horse.jpg') plt.axis('off') # 关闭刻度显示 print(img.shape) # (height, width, channel)
print(img.size) # 像素数量
print(img.dtype) #储存图片
plt.savefig('./name.jpg') figure = plt.figure(figsize=(20,10)) # 调整显示图片的大小 plt.imshow(img)
plt.show()

3.2 Matplotlib 图片灰化处理

#图片灰化处理
# 平均值发
img_mean = img.mean(axis=2)
plt.imshow(img_mean,cmap='gray')
plt.show() #最大值法
img_max = img.max(axis=-1)
plt.imshow(img_max,cmap='gray')
plt.show() #RGB三原色法
gravity = np.array([0.299,0.587,0.114])
img_gravity = np.dot(img,gravity)
plt.imshow(img_gravity,cmap="gray")
plt.show()

4.1 skimage 读取和储存图像

from skimage import io
#读取图像numpy数组格式
img = io.imread('horse.jpg')
print(img.shape)
print(img.dtype)
print(img.size)
#print(img)
io.imshow(img) #储存图像
io.imsave('test.jpg',img)

4.2 skimage 灰化处理

#图像灰化处理并归一化
img = io.imread('horse.jpg',as_gray=True)
print(img.shape)
print(img.dtype) # 数据类型位float
print(img.size)
print(img)
io.imshow(img)
io.show()

5.1 scipy.misc 读取和储存图像

#在1.2.0 之后统一用imageio模块
import imageio
import matplotlib.pyplot as plt
#读取图片为numpy数组
img = imageio.imread('horse.jpg')
print(img.dtype)
print(img.size) # 像素数量
print(img.shape) #(height, width, channels)
plt.imshow(img)
plt.show()
print(img)
#储存图片
imageio.imsave('test.jpg',img)

未完待续......

Python各种图像库的图像的基本读写方式的更多相关文章

  1. Python的图像库

    对数字图像基本的处理的学习按照下面两个博客: Python的图像库(Opencv.PIL.matplotlib.skimage)的使用(读取.存储.变换.滤波) python数字图像处理

  2. python 字节转换成图像

    python 字节转换成图像 使用base64 1.图片转成字节使用:  base64.b64encode() 2.字节转成图片: base64.b64decode() 图片字节串: iVBORw0K ...

  3. python 文件读写方式

    一.普通文件读写方式 1.读取文件信息: with open('/path/to/file', 'r') as f: content = f.read() 2.写入文件中: with open('/U ...

  4. python 使用 with open() as 读写文件

    读文件: 要以读文件的模式打开一个文件对象,使用Python内置的open()函数,传入文件名和标示符: >>> f = open('E:\python\python\test.tx ...

  5. 跟我学Python图像处理丨何为图像的灰度非线性变换

    摘要:本文主要讲解灰度线性变换,基础性知识希望对您有所帮助. 本文分享自华为云社区<[Python图像处理] 十六.图像的灰度非线性变换之对数变换.伽马变换>,作者:eastmount . ...

  6. 跟我学Python图像处理丨关于图像金字塔的图像向下取样和向上取样

    摘要:本文讲述图像金字塔知识,了解专门用于图像向上采样和向下采样的pyrUp()和pyrDown()函数. 本文分享自华为云社区<[Python图像处理] 二十一.图像金字塔之图像向下取样和向上 ...

  7. python全栈开发_day8_文件的多种读写方式及游标

    一:文件的多种读写方式 主方式:w    r    a 从方式:t     b    + 了解方式:x    u 1)按t(按照字符进行操作): with open("data_1.txt& ...

  8. Epoll在LT和ET模式下的读写方式

    在一个非阻塞的socket上调用read/write函数, 返回EAGAIN或者EWOULDBLOCK(注: EAGAIN就是EWOULDBLOCK) 从字面上看, 意思是:EAGAIN: 再试一次, ...

  9. java指定编码的按行读写txt文件(几种读写方式的比较)

    转: java指定编码的按行读写txt文件(几种读写方式的比较) 2018年10月16日 20:40:02 Handoking 阅读数:976  版权声明:本文为博主原创文章,未经博主允许不得转载. ...

随机推荐

  1. complex类

    #include<iostream> #include<cmath> using namespace std; class complex{ public: complex() ...

  2. 保护url时效性和安全性的一种解决方案

    几乎都是同事小哥哥帮我铺路,给我参考链接,实现的理论方法以及知识,我只剩下看资料,敲代码,出错了也是他帮我看着一步步解释搞定过来的.嗯,大好人一枚. ok,思路: 是生成一个随机数放在url里面,当做 ...

  3. wrapper induction随笔

    本文是一篇介绍Wrapper Induction的阅读笔记,原文详见<Wrapper induction:Efficiency and expressiveness>. Wrapper I ...

  4. 处理 Archlinux 报错

    failed to kernel 1 sudo pacman -S linux-headers tpm_crb, uvcvideo 等错误 sudo vim /etc/modprobe.d/black ...

  5. rapidjson对于json的序列化与反序列化

    转载: https://blog.csdn.net/qq849635649/article/details/52678822 #include "rapidjson/stringbuffer ...

  6. python练习题-day25

    class Person: __key=123 def __init__(self,username,password): self.username=username self.__password ...

  7. JavaScript实现预览本地上传图片

    <html> <head> <title> www.jb51.net图片上传预览 </title> <script> function Pr ...

  8. 3种检测页面是否符合amp标准的方法

    AMP的关键优势不仅仅在于它能让你的页面更快,还在于它的快可以被验证.有几种方法可以验证AMP文档,它们都会产生完全相同的结果,选择最适合您的开发风格的方法.除了AMP的有效性,您可能还想确认您的AM ...

  9. studio-3t 配置文件位置

    换电脑了,原来的studio-3t的配置 在 C:\Users\用户名\.3T. 将这个目录下的所有文件拷贝到 新电脑里的 相同文件夹,覆盖即可

  10. FreeSwitch 终端命令详细介绍

    FreeSwitch版本:1.6.9 以下为部分终端命令 alias 语法: alias [add|stickyadd] <alias> <command> | del [&l ...