美文网首页
图像的数组表示

图像的数组表示

作者: 闫_锋 | 来源:发表于2018-05-17 08:54 被阅读9次

图像的RGB色彩模式

RGB三个颜色通道的变化和叠加得到各种颜色,其中各通道取值范围:
R 0~255
G 0~255
B 0~255

RGB形成的颜色包括了人类视力所能感知的所有颜色。

PIL库
PIL: Python Image Library
PIL库是一个具有强大图像处理能力的第三方库。

pip install pillow
from PIL import Image
#Image是PIL库中代表一个图像的类(对象)

图像是一个由像素组成的二维矩阵,每个元素是一个RGB值(R, G, B)。

from PIL import Image
import numpy as np
im  = np.array(Image.open("D:/pycodes/beijing.jpg"))
print(im.shape, im.dtype)

图像是一个三维数组,维度分别是高度,宽度和像素RGB值。

图像的变换
读入图像后,获得像素RGB值,修改后保存为新的文件。

from PIL import Image
import numpy as np
a  = np.array(Image.open("D:/pycodes/beijing.jpg"))
print(a.shape, a.dtype)

b = [255, 255, 255] - a #补值
im = Image.formarray(b.astype('uint8'))
im.save("D:/pycodes/fcity2.jpg")
from PIL import Image
import numpy as np
a  = np.array(Image.open("D:/pycodes/beijing.jpg").convert('L')) #转换为灰度值图片(二维数组)

b = 255 - a
im = Image.formarray(b.astype('uint8'))
im.save("D:/pycodes/fcity3.jpg")

相关文章

网友评论

      本文标题:图像的数组表示

      本文链接:https://www.haomeiwen.com/subject/zkbvdftx.html