第一部分构造抽象类
Plotter
,实现抽象方法plot()
。第二部分继承元类
Plotter
实现子类,实现元类的方法plot()
。第三部分分别实现子类的适配类
Adapter
。
import os
import cv2
import abc
import glob
import jieba
import imageio
import librosa
import librosa.display
from PIL import Image
from celluloid import Camera
import matplotlib.pyplot as plt
from wordcloud import WordCloud
class Point:
def __init__(self,x,y):
lis = []
for i in range(len((x))):
lis.append((x[i],y[i]))
self.lis = lis
class Plotter(metaclass=abc.ABCMeta):#class Plotter(abc.ABC) #构造元类Plotter
@abc.abstractmethod
def plot(self,*arg,**kwargs):
pass
class PointPlotter(Plotter): #点型数据绘制
def __init__(self,data):
self._data = data
def plot(self):
for x,y in self._data:
plt.scatter(x,y)
plt.show()
class ArrayPlotter(Plotter): #数组型数据绘制
def __init__(self,data):
self._data = data
def plot(self):
plt.scatter(self._data[0],self._data[1])
plt.show()
class TextPlotter(Plotter): #文本型数据绘制
def __init__(self,data):
self._data = data
def plot(self):
txt = jieba.lcut(self._data)
string = ''.join(txt)
w = WordCloud(max_font_size=100,
background_color='white',
scale=3,font_path='C:/Windows/SIMLI.TTF')
w.generate(string) #生成词云
w.to_file('axwordcloud.png')
class ImagePlotter(Plotter): #图像型数据绘制
def __init__(self,data):
self._data = data
def plot(self,row=3,column=4):
for num in range(0,len(self._data),row*column):
print(num)
for each in range(1,row*column+1): #控制每张子图展示图片数量
if num+each-1<len(self._data):
img = self._data[num+each-1]
img = Image.open(img)
img = img.resize((640,480))
plt.subplot(row,column,each)
plt.imshow(img)
plt.show()
class GifPlotter(Plotter): #Gif图片的输出绘制
def __init__(self,data):
self._data = data
def plot(self):
fig = plt.figure()
camera = Camera(fig)
for i in range(10):
img = self._data[i]
img = Image.open(img)
img = img.resize((640,480))
plt.imshow(img)
camera.snap()
animation = camera.animate()
animation.save('celluloid_minimal.gif')
class AudioPlotter(Plotter): #音频型数据绘制
def __init__(self,data):
self._data = data
def plot(self):
x , sr = librosa.load(self._data)
#print(type(x), type(sr))
#plt.figure(figsize=(10, 5))
librosa.display.waveplot(x, sr=sr)
plt.savefig('audio.png')
plt.show()
class VideoPlotter(Plotter): #视频型数据绘制
def __init__(self,data):
self._data = data
def plot(self):
video_cap = cv2.VideoCapture(self._data)
frame_count = 0
all_frames = []
while True:
ret, frame = video_cap.read()
if ret is False:
break
frame = frame[..., ::-1] # opencv读取BGR,转成RGB
all_frames.append(frame)
cv2.imshow('frame', frame)
cv2.waitKey(1)
frame_count += 1
video_cap.release()
cv2.destroyAllWindows()
print('===>', len(all_frames))
imageio.mimsave('./result.gif', all_frames, 'GIF', duration=0.001)
class PointPlotterAdapter(PointPlotter): #点型数据适配类
def __init__(self,pointplotter):
self.pointplotter=pointplotter
def plot(self):
self.pointplotter.plot()
class ArrayPlotterAdapter(ArrayPlotter): #数组型数据适配类
def __init__(self,arrayplotter):
self.arrayplotter=arrayplotter
def plot(self):
self.arrayplotter.plot()
class TextPlotterAdapter(TextPlotter): #文本型数据适配类
def __init__(self,textplotter):
self.textplotter=textplotter
def plot(self):
self.textplotter.plot()
class ImagePlotterAdapter(ImagePlotter): #图片型数据适配类
def __init__(self,imageplotter):
self.imageplotter=imageplotter
def plot(self):
self.imageplotter.plot()
class GifPlotterAdapter(GifPlotter): #GIF图片型适配类
def __init__(self,gifplotter):
self.gifplotter=gifplotter
def plot(self):
self.gifplotter.plot()
class AudioPlotterAdapter(AudioPlotter): #音频型数据适配类
def __init__(self,audioplotter):
self.audioplotter=audioplotter
def plot(self):
self.audioplotter.plot()
class VideoPlotterAdapter(VideoPlotter): #视频型数据适配类
def __init__(self,videoplotter):
self.videoplotter=videoplotter
def plot(self):
self.videoplotter.plot()
def main(): #主函数
x = [1,2,3,4,5]
y = [2,4,6,8,10]
p = Point(x,y)
print(p.lis)
objects = [PointPlotter(p.lis)]
ar = ArrayPlotter([x,y])
data = '我是一个粉刷匠,粉刷本领强。'
te = TextPlotter(data)
path = 'H:\图片\\'
data = glob.glob(os.path.join(path + '*.jpg'))
im = ImagePlotter(data)
gi = GifPlotter(data)
audio_path = 'summer.wav'
au = AudioPlotter(audio_path)
video_path = 'H:\\Captures\\我创建的视频\\feifei.mp4'
vi = VideoPlotter(video_path)
objects.append(ArrayPlotterAdapter(ar))
objects.append(TextPlotterAdapter(te))
objects.append(ImagePlotterAdapter(im))
objects.append(GifPlotterAdapter(gi))
objects.append(AudioPlotterAdapter(au))
objects.append(VideoPlotterAdapter(vi))
for obj in objects:
obj.plot()
main()
-
点型数据绘制
-
数组型变量绘制
-
文本型数据绘制
-
图片型数据绘制
-
GIF动图数据绘制
-
音频型数据绘制
-
视频型数据绘制
网友评论