图像是如何被离散化的?
为什么称为真彩色,除了24位,还有那些喃?
上节回顾
pip install pillow
from PIL import Image
import matplotlib.pyplot as plt
Image.open(路径)# 返回image对象
有一个经常使用的图像叫做lena.tiff
可以在网上下载,网址为http://www.lenna.org/
然后可以在该文件目录下打开
img = Image.open('lena.tiff')
得到图像对象img
图像对象.save(文件路径)
img.save('test.tiff')
img.save('lena.jpg') # 将原图存储为jpg格式
img.save('lena.bmp') # 将原图存储为bpm格式
属性 | 说明 |
---|---|
图像对象.format | 图像格式 |
图像对象.size | 图像尺寸 |
图像对象.mode | 色彩模式 |
例如:
>>> from PIL import Image
>>> import matplotlib.pyplot as plt
>>> img = Image.open('lena.tiff')
>>> img.save('test.tiff')
>>> img.save('test.jpg')
>>> img.save('test.bmp')
>>> img1 = Image.open("test.jpg")
>>> img2 = Image.open("test.bmp")
>>> print("image:",img.format)
image: TIFF
>>> print("image1:",img1.format)
image1: JPEG
>>> print("image2:",img2.format)
image2: BMP
>>> print("size:",img.size)
size: (512, 512)
>>> print("mode:",img.mode)
mode: RGB
*可以使用matpltlib.pyplot模块中的 imshow()函数和show()函数显示图像
plt.imshow(image对象/Numpy数组)
>>> plt.figure(figsize=(5,5))
<Figure size 500x500 with 0 Axes>
>>> plt.imshow(img)
<matplotlib.image.AxesImage object at 0x0000022508D1B908>
>>> plt.show()
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('lena.tiff')
img.save('test.tiff')
img.save('test.jpg')
img.save('test.bmp')
img = Image.open("test.tiff")
img1 = Image.open("test.jpg")
img2 = Image.open("test.bmp")
plt.figure(figsize=(15,5))
plt.subplot(131)
plt.axis("off")
plt.imshow(img)
plt.title(img.format)
plt.subplot(132)
plt.axis("off")
plt.imshow(img1)
plt.title(img1.format)
plt.subplot(133)
plt.axis("off")
plt.imshow(img2)
plt.title(img2.format)
plt.show()
取值 | 色彩模式 |
---|---|
1 | 二值图像(每个像素8位二进制) |
L | 灰度图像 |
P | 8位彩色图像 |
RGB | 24位彩色图像 |
RGBA | 32位彩色图像 |
CMYK | CMYK彩色图像 |
YCbCr | YCbCr彩色图像 |
I | 32位整型灰度图像 |
F | 32位浮点灰度图像 |
图像对象.convert(色彩模式)
例如:
img_gray = img.convert("L")
print("mode=",img_gray.mode)
plt.figure(figsize=(5,5))
plt.imshow(img_gray)
plt.show()
输出结果为:
本来我寻思着为啥我这个还带点青色,难道我运行错了,本来想留着这个疑问,但是当我存储以后,发现是这样的:
还可以使用save()方法将其存储为新的文件
img_gray.save("lena_gray.bmp")
图像对象.split()
Image.merge(色彩模式,图像列表)
例如:
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open('lena.tiff')
img_r,img_g,img_b = img.split()
plt.figure(figsize=(8,8))
plt.subplot(221)
plt.axis("off")
plt.imshow(img_r,cmap="gray")# cmap是以灰度的方式显示
plt.title("R",fontsize=20)
plt.subplot(222)
plt.axis("off")
plt.imshow(img_g,cmap="gray")
plt.title("G",fontsize=20)
plt.subplot(223)
plt.axis("off")
plt.imshow(img_b,cmap="gray")
plt.title("B",fontsize=20)
img_rgb = Image.merge("RGB",[img_r,img_g,img_b])
# 使用merge方法将所有的通道合并
plt.subplot(224)
plt.axis("off")
plt.imshow(img_rgb)
plt.title("RGB",fontsize=20)
plt.show()
np.array(图像对象)
import numpy as np
arr_img = np.array(img)
print("shape:",arr_img.shape,"\n")
print(arr_img)
输出结果为
shape: (512, 512, 3)
[[[226 137 125]
[226 137 125]
[223 137 133]
...
[230 148 122]
[221 130 110]
[200 99 90]]
[[226 137 125]
[226 137 125]
[223 137 133]
...
[[ 82 22 57]
[ 82 22 57]
[ 96 32 62]
...
[179 70 79]
[181 71 81]
[185 74 81]]]
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 已经创建了lena_gray.bmp文件
img_gray = Image.open("lena_gray.bmp")
arr_img_gray = np.array(img_gray)
print("\nshape:",arr_img_gray.shape)
print(arr_img_gray)
输出结果为:
shape: (512, 512)
[[162 162 162 ... 170 155 128]
[162 162 162 ... 170 155 128]
[162 162 162 ... 170 155 128]
...
[ 43 43 50 ... 104 100 98]
[ 44 44 55 ... 104 105 108]
[ 44 44 55 ... 104 105 108]]
我们可以对这个值做一些处理:
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
img_gray = Image.open("lena_gray.bmp")
arr_img_gray = np.array(img_gray)
# 新数组每个值都是原来的灰度值被255减去
arr_img_new = 255 - arr_img_gray
plt.figure(figsize=(10,5))
plt.subplot(221)
plt.axis("off")
plt.imshow(arr_img_gray,cmap="gray")
plt.subplot(222)
plt.axis("off")
plt.imshow(arr_img_new,cmap="gray")
plt.show()
图像对象.resize((width,height))
例如:
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
img = Image.open("lena.tiff")
plt.figure(figsize=(5,5))
img_small = img.resize((64,64))
#将其保存起来
img_small.save("lena_s.jpg")
plt.imshow(img_small)
plt.show()
图像对象.transpose(旋转方式)
参数 | 说明 |
---|---|
Image.FLIP_LEFT_RIGHT | 水平翻转 |
Image.FLIP_TOP_BOTTOM | 上下翻转 |
Image.ROTATE_90 | 逆时针旋转90° |
Image.ROTATE_180 | 逆时针旋转180° |
Image.ROTATE_270 | 逆时针旋转270° |
Image.TRANSPOSE | 将图像进行转置 |
Image.TRANSVERSE | 将图像进行转置,再水平翻转 |
例如:
img_flr = img.transpose(Image.FLIP_LEFT_RIGHT)
img_r90 = img.transpose(Image.ROTATE_90)
img_tp = img.transpose(Image.TRANSPOSE)
例如,下面将这些变换的结果分别显示在不同的子途中
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = "SimHei"
img = Image.open("lena.tiff")
plt.figure(figsize=(10,10))
plt.subplot(221)
plt.axis("off")
plt.imshow(img)
plt.title("原图",fontsize=20)
plt.subplot(222)
plt.axis("off")
img_flr = img.transpose(Image.FLIP_LEFT_RIGHT)
plt.imshow(img_flr)
plt.title("左右翻转",fontsize=20)
plt.subplot(223)
plt.axis("off")
img_r90 = img.transpose(Image.ROTATE_90)
plt.imshow(img_r90)
plt.title("逆时针旋转90度",fontsize=20)
plt.subplot(224)
plt.axis("off")
img_tp = img.transpose(Image.TRANSPOSE)
plt.imshow(img_tp)
plt.title("转置",fontsize=20)
plt.show()
图像对象.crop((x0,y0,x1,y1))
```python
img_region = img.crop((100,100,400,400))
例如:
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("lena.tiff")
plt.figure(figsize=(10,5))
plt.subplot(121)
plt.imshow(img)
plt.subplot(122)
img_region = img.crop((100,100,400,400))# 注意y轴是从上往下而从小到大的
plt.imshow(img_region)
plt.show()
图像操作 | 方法/函数/属性 |
---|---|
打开图像 | Image.open(路径) |
保存图像 | 图像对象.save() |
查看图像的属性 | 图像对象.format、图像对象.size、图像对象.mode |
显示图像 | plt.imshow(Image对象/Numpy数组) |
转移图像的色彩模式 | 图像对象.convert(色彩模式) |
颜色通道的分离和合并 | 图像对象.split()、Image.merge(色彩模式,图像列表) |
将图像转换为数组 | np.array(图像对象) |
图像的缩放 | 图像对象.resize((width,height)) |
旋转和镜像 | 图像对象.transpose(旋转方式) |
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(train_x,train_y),(test_x,test_y) = mnist.load_data()
第一次会先进行下载:
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 12s 1us/step
C:\Users\Administrator\.keras\datasets\mnist.npz
print("Training set:",len(train_x))
print("testing set:",len(test_x))
输出结果为:
Training set: 60000
testing set: 10000
print("train_x:",train_x.shape,train_x.dtype)
print("trian_y:",train_y.shape,train_y.dtype)
输出结果为:
train_x: (60000, 28, 28) uint8
trian_y: (60000,) uint8
>>> train_x[0]
array([[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 3,
18, 18, 18, 126, 136, 175, 26, 166, 255, 247, 127, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 30, 36, 94, 154, 170,
253, 253, 253, 253, 253, 225, 172, 253, 242, 195, 64, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 49, 238, 253, 253, 253, 253,
253, 253, 253, 253, 251, 93, 82, 82, 56, 39, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 18, 219, 253, 253, 253, 253,
253, 198, 182, 247, 241, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 80, 156, 107, 253, 253,
205, 11, 0, 43, 154, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 14, 1, 154, 253,
90, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 139, 253,
190, 2, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 11, 190,
253, 70, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 35,
241, 225, 160, 108, 1, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
81, 240, 253, 253, 119, 25, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 45, 186, 253, 253, 150, 27, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 16, 93, 252, 253, 187, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 249, 253, 249, 64,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 46, 130, 183, 253, 253, 207, 2,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 39,
148, 229, 253, 253, 253, 250, 182, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 24, 114, 221,
253, 253, 253, 253, 201, 78, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 23, 66, 213, 253, 253,
253, 253, 198, 81, 2, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 18, 171, 219, 253, 253, 253, 253,
195, 80, 9, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 55, 172, 226, 253, 253, 253, 253, 244, 133,
11, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 136, 253, 253, 253, 212, 135, 132, 16, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0]], dtype=uint8)
import tensorflow as tf
import matplotlib.pyplot as plt
mnist = tf.keras.datasets.mnist
(train_x,train_y),(test_x,test_y) = mnist.load_data()
plt.axis("off")
plt.imshow(train_x[0],cmap="gray")
plt.show()
>>> train_y[0]
5
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
mnist = tf.keras.datasets.mnist
(train_x,train_y),(test_x,test_y) = mnist.load_data()
for i in range(4):
num = np.random.randint(1,60000)
plt.subplot(1,4,i+1)
plt.axis("off")
plt.imshow(train_x[num],cmap="gray")
plt.title(train_y[num])
plt.show()