matplotlib 展示numpy图像

matplotlib 显示numpy图像

在这里插入图片描述

显示图像

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : plt_img.py
# Create date : 2021-09-04 17:14
# Modified date : 2021-09-04 23:36
# Author : DARREN
# Describe : not set
# Email : [email protected]
#####################################

import os
from PIL import Image
import matplotlib.pyplot as plt

#读取图片然后展示出来
img = Image.open(os.path.join('./', 'screenshot' + '.png'))
plt.figure("figure name screenshot") #图像窗口名称
plt.imshow(img)
plt.axis('on') #关掉坐标轴为off
plt.title('text title') #图像标题
plt.savefig("./img/plt_img.png")
plt.show()

output

matplotlib 展示numpy图像_第1张图片

显示一副灰度图

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : plt_gray_img.py
# Create date : 2021-09-04 17:14
# Modified date : 2021-09-04 23:42
# Author : DARREN
# Describe : not set
# Email : [email protected]
#####################################

import os
from PIL import Image
import matplotlib.pyplot as plt

#显示一副灰度图像
img = Image.open(os.path.join('./', 'screenshot' + '.png'))
img = img.convert('L')
plt.figure("gray figure")
plt.imshow(img, cmap='gray')
plt.axis('on')
plt.title('gray screenshot')
plt.savefig("./img/plt_gray_img.png")
plt.show()

output

matplotlib 展示numpy图像_第2张图片

显示多副图

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : plt_multi_img.py
# Create date : 2021-09-04 17:15
# Modified date : 2021-09-04 23:45
# Author : DARREN
# Describe : not set
# Email : [email protected]
#####################################
import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

img = Image.open(os.path.join('./', 'screenshot' + '.png'))
gray = img.convert('L')
r,g,b = img.split()
img_merged = Image.merge('RGB', (r, g, b))

plt.figure(figsize=(10,5)) #设置窗口大小
plt.suptitle('multi image title')

plt.subplot(2,3,1), plt.title('image')
plt.imshow(img), plt.axis('off')

plt.subplot(2,3,2), plt.title('gray')
plt.imshow(gray,cmap='gray'), plt.axis('off')

plt.subplot(2,3,3), plt.title('img_merged')
plt.imshow(img_merged), plt.axis('off')

plt.subplot(2,3,4), plt.title('pink')
plt.imshow(r,cmap='pink'), plt.axis('off')

plt.subplot(2,3,5), plt.title('Blues')
plt.imshow(g,cmap='Blues'), plt.axis('off')

plt.subplot(2,3,6), plt.title('Oranges')
plt.imshow(b,cmap='Oranges'), plt.axis('off')

plt.savefig("./img/plt_multi_img.png")
plt.show()
# cmap supported values are 'Accent', 'Accent_r', 'Blues', 'Blues_r', 'BrBG', 'BrBG_r', 'BuGn', 'BuGn_r', 'BuPu', 'BuPu_r', 'CMRmap', 'CMRmap_r', 'Dark2', 'Dark2_r', 'GnBu', 'GnBu_r', 'Greens', 'Greens_r', 'Greys', 'Greys_r', 'OrRd', 'OrRd_r', 'Oranges', 'Oranges_r', 'PRGn', 'PRGn_r', 'Paired', 'Paired_r', 'Pastel1', 'Pastel1_r', 'Pastel2', 'Pastel2_r', 'PiYG', 'PiYG_r', 'PuBu', 'PuBuGn', 'PuBuGn_r', 'PuBu_r', 'PuOr', 'PuOr_r', 'PuRd', 'PuRd_r', 'Purples', 'Purples_r', 'RdBu', 'RdBu_r', 'RdGy', 'RdGy_r', 'RdPu', 'RdPu_r', 'RdYlBu', 'RdYlBu_r', 'RdYlGn', 'RdYlGn_r', 'Reds', 'Reds_r', 'Set1', 'Set1_r', 'Set2', 'Set2_r', 'Set3', 'Set3_r', 'Spectral', 'Spectral_r', 'Wistia', 'Wistia_r', 'YlGn', 'YlGnBu', 'YlGnBu_r', 'YlGn_r', 'YlOrBr', 'YlOrBr_r', 'YlOrRd', 'YlOrRd_r', 'afmhot', 'afmhot_r', 'autumn', 'autumn_r', 'binary', 'binary_r', 'bone', 'bone_r', 'brg', 'brg_r', 'bwr', 'bwr_r', 'cividis', 'cividis_r', 'cool', 'cool_r', 'coolwarm', 'coolwarm_r', 'copper', 'copper_r', 'cubehelix', 'cubehelix_r', 'flag', 'flag_r', 'gist_earth', 'gist_earth_r', 'gist_gray', 'gist_gray_r', 'gist_heat', 'gist_heat_r', 'gist_ncar', 'gist_ncar_r', 'gist_rainbow', 'gist_rainbow_r', 'gist_stern', 'gist_stern_r', 'gist_yarg', 'gist_yarg_r', 'gnuplot', 'gnuplot2', 'gnuplot2_r', 'gnuplot_r', 'gray', 'gray_r', 'hot', 'hot_r', 'hsv', 'hsv_r', 'inferno', 'inferno_r', 'jet', 'jet_r', 'magma', 'magma_r', 'nipy_spectral', 'nipy_spectral_r', 'ocean', 'ocean_r', 'pink', 'pink_r', 'plasma', 'plasma_r', 'prism', 'prism_r', 'rainbow', 'rainbow_r', 'seismic', 'seismic_r', 'spring', 'spring_r', 'summer', 'summer_r', 'tab10', 'tab10_r', 'tab20', 'tab20_r', 'tab20b', 'tab20b_r', 'tab20c', 'tab20c_r', 'terrain', 'terrain_r', 'turbo', 'turbo_r', 'twilight', 'twilight_r', 'twilight_shifted', 'twilight_shifted_r', 'viridis', 'viridis_r', 'winter', 'winter_r'

output

matplotlib 展示numpy图像_第3张图片

图片数据转numpy数据

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : plt_img_to_numpy.py
# Create date : 2021-09-04 17:14
# Modified date : 2021-09-04 23:52
# Author : DARREN
# Describe : not set
# Email : [email protected]
#####################################

import os
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np

#图片数据转numpy数据,并把numpy数据plt出来
img = Image.open(os.path.join('./', 'screenshot' + '.png'))
np_img = np.array(img.getdata())
plt.figure("figure name screenshot")
sp = list(img.size)
sp.reverse()
sp.append(3) #3 channel
np_img = np_img.reshape(sp)
plt.imshow(np_img)
plt.axis('on')
plt.title('text title')
plt.savefig("./img/plt_img_to_numpy.png")
plt.show()

output

matplotlib 展示numpy图像_第4张图片

#将numpy 数据 用matplotlib展示出来,或者保存成图片文件

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : plt_numpy_to_img.py
# Create date : 2021-09-04 17:50
# Modified date : 2021-09-04 23:26
# Author : DARREN
# Describe : not set
# Email : [email protected]
#####################################

import matplotlib.pyplot as plt
import numpy as np

edge = 2**10

def show_img(X, name):
    plt.figure("edge:%s" % edge)
    plt.imshow(X)
    plt.axis('on')
    plt.title('img edge:%s' % edge)
    plt.savefig(name)
    plt.show()

def show_gray_img(X, name):
    plt.figure("edge:%s" % edge) 
    plt.imshow(X, cmap="gray")
    plt.axis('on')
    plt.title('img edge:%s' % edge)
    plt.savefig(name)
    plt.show()

X = np.random.rand(edge,edge)
show_img(X, "./img/plt_numpy_to_img_1.png")
show_gray_img(X, "./img/plt_numpy_to_img_1gray.png")

X = np.random.rand(edge,edge,3)
show_img(X, "./img/plt_numpy_to_img_3.png")
show_gray_img(X, "./img/plt_numpy_to_img_3gray.png")

X = np.random.rand(edge,edge,4)
show_img(X, "./img/plt_numpy_to_img_4.png")
show_gray_img(X, "./img/plt_numpy_to_img_4gray.png")

output

matplotlib 展示numpy图像_第5张图片

matplotlib 展示numpy图像_第6张图片
matplotlib 展示numpy图像_第7张图片
matplotlib 展示numpy图像_第8张图片

matplotlib 展示numpy图像_第9张图片

matplotlib 展示numpy图像_第10张图片

#!/usr/bin/python
# -*- coding: utf-8 -*-
#####################################
# File name : plt_numpy.py
# Create date : 2021-09-04 17:48
# Modified date : 2021-09-04 23:56
# Author : DARREN
# Describe : not set
# Email : [email protected]
#####################################
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
 
# make a Figure and attach it to a canvas.
 
fig = plt.Figure(figsize=(5, 5), dpi=128)
 
fig.set_tight_layout(True) # reduce the spaces from margin outside the axis 
 
canvas = FigureCanvasAgg(fig)
 
# Do some plotting here
ax = fig.gca()
ax.plot([1, 2, 3])
 
#ax.set_axis_off()
# Retrieve a view on the renderer buffer
 
canvas.draw()
buf = canvas.buffer_rgba()
 
# convert to a NumPy array
X = np.asarray(buf)
 
plt.imshow(X)
plt.savefig("./img/plt_numpy.png")
plt.show()

output

matplotlib 展示numpy图像_第11张图片

你可能感兴趣的:(matplotlib,python)