Scipy文件输入/输出/Mp3/图片
2019-05-16 本文已影响0人
垃圾桶边的狗
随机生成数组,使用scipy中的io.savemat()保存
文件格式是.mat,标准的二进制文件
import scipy.io as io
保存
# result是图片的ndarray
io.savemat('./moon_clean.mat',mdict = {'moon':np.real(result)})
使用io.loadmat()读取数据
io.loadmat('./moon_clean.mat')['moon']
47.png
读写图片使用scipy中misc.imread()/imsave()
from scipy import misc
import warnings
warnings.filterwarnings('ignore')
cat = misc.imread('./cat.jpg')
plt.imshow(cat)

misc旋转、resize、imfilter
cat2 = misc.imrotate(cat,90)
plt.imshow(cat2)

'''size : int, float or tuple
* int - Percentage of current size.
* float - Fraction of current size.
* tuple - Size of the output image (height, width).
'''
cat3 = misc.imresize(cat,0.2)
# cat4 = misc.imresize(cat,(200,200))
plt.imshow(cat4)
cat3.shape
plt.imshow(cat3)

'''ftype : str
The filter that has to be applied. Legal values are:
'blur', 'contour', 'detail', 'edge_enhance', 'edge_enhance_more',
'emboss', 'find_edges', 'smooth', 'smooth_more', 'sharpen'.'''
# 原理:卷积操作
# 卷积神经网络tensorflow
cat6 = misc.imfilter(cat,'find_edges')
plt.imshow(cat6)

音乐处理

from scipy.io import wavfile
like = wavfile.read('./邓紫棋-喜欢你.wav')
like
like[1].shape
out:
(10776444, 2)
歌曲秒数时间
like[1].shape[0]/44100
out
244.36380952380952

lost = wavfile.read('./林俊杰-爱不会绝迹.wav')
lost

歌曲拼接,前一分钟和后一分钟
mix = np.concatenate([like[1][:44100*60],lost[1][-44100*60:]])
mix.shape
out
(5292000, 2)
保存文件
wavfile.write('./mix.wav',44100,mix)
scipy不能处理mp3
pip install pydub

读取Mp3
jing = pydub.AudioSegment.from_mp3('./难念的经.mp3')
jing
you = pydub.AudioSegment.from_mp3('./一路上有你.mp3')
you
jing[:1000*30] + you[:1000*30]
data = you.get_array_of_samples()
type(data)
array.array
data_raw = np.array(data.tolist())
data_raw.shape
(25337088,)
# 此数据就是wav格式用到的数据
data_raw = data_raw.reshape(-1,2)
data_raw.shape
(12668544, 2)
you.frame_rate
wavfile.write('./一路有你.wav',44100,data_raw.astype(np.int16))
使用ffmpeg操作视频
# ffmpeg -i law.mp4 -f mp3 -vn law.mp3
# 获取音频
import subprocess
cmd = 'ffmpeg -i law.mp4 -f mp3 -vn law2.mp3'
# 执行成功,subprocess返回结果是0
subprocess.call(cmd)
# 从视频中获取无声视频
cmd = 'ffmpeg -i 1.mp4 -vcodec copy -an 2.mp4'
subprocess.call(cmd)
# 合并视频和音频
cmd = 'ffmpeg -i law.mp3 -i 2.mp4 out.mp4'
subprocess.call(cmd)
图片处理
使用scipy.misc.face(gray=True)获取图片,使用ndimage移动坐标、旋转图片、切割图片、缩放图片
导包,读取图片显示图片
import numpy as np
# 移动坐标、旋转图片、切割图片、缩放图片、滤波(不清晰的图片,清晰化,取出‘噪声’)
from scipy import ndimage
from scipy import misc
import matplotlib.pyplot as plt
%matplotlib inline
# misc模块中,有一张图片
# 样本图片
face = misc.face()
plt.imshow(face)

shift移动坐标
face.shape
(768, 1024, 3)
# jpg 0~255 0:纯黑,255纯白
face2 = ndimage.shift(face,shift=[100,200,0],cval=255)
plt.imshow(face2)

# mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'
# mirror reflect 镜像
# nearest 最近的像素进行填充
# wrap使用移出去的像素进行填充
face3 = ndimage.shift(face,shift=[0,-400,0],mode='wrap')
plt.imshow(face3)

rotate旋转图片
face4 = ndimage.rotate(face,angle = 60)
plt.imshow(face4)

face5 = misc.imrotate(face,60)
plt.imshow(face5)

zoom缩放图片
# zoom : float or sequence
face6 = ndimage.zoom(face,zoom = 1/3)
face6.shape
# 合法:黑白二维;彩色三维(第三维,三个数RGB,四个数ARGB)
plt.imshow(face6.reshape(256,-1),cmap = plt.cm.gray)

# zoom : float or sequence
face6 = ndimage.zoom(face,zoom = 4/3)
face6.shape
# 合法:黑白二维;彩色三维(第三维,三个数RGB,四个数ARGB)
# plt.imshow(face6.reshape(256,-1),cmap = plt.cm.gray)
(1024, 1365, 4)
face6[:,:,0] = 0.1
plt.imshow(face6)

face6

# jpg类型透明度:0 ~ 255
# png类型图片 0 ~ 1
# 彩色图片三维,第三维四个数据:RGBA(alpha,透明)
face6[:,:,3] = 20
plt.imshow(face6)
# 机器学习:图片辨识、语音识别、

face.shape
(768, 1024, 3)
face6 = ndimage.zoom(face,zoom = [200/768,300/1024,1])
face6.shape
plt.imshow(face6)

切割图片
# numpy切片操作
face7 = face[:512,-512:]
plt.imshow(face7)

图片进行过滤
添加噪声,对噪声图片使用ndimage中的高斯滤波、中值滤波、signal中维纳滤波进行处理
使图片变清楚
moon = plt.imread('./moonlanding.png')
plt.figure(figsize=(12,9))
plt.imshow(moon,cmap = 'gray')

# sigma高斯核,高斯分布,正态分布,sigma离散程度
moon2 = ndimage.gaussian_filter(moon,sigma=2)
plt.figure(figsize=(12,9))
plt.imshow(moon2,cmap = 'gray')

moon2 = ndimage.median_filter(moon,size = 5)
plt.figure(figsize=(12,9))
plt.imshow(moon2,cmap = 'gray')

moon2 = ndimage.median_filter(moon,size = 10)
plt.figure(figsize=(12,9))
plt.imshow(moon2,cmap = 'gray')

moon2 = ndimage.median_filter(moon,size = 50)
plt.figure(figsize=(12,9))
plt.imshow(moon2,cmap = 'gray')

signal维纳滤波mysize:滤镜尺寸的标量
from scipy import signal
moon3 = signal.wiener(moon,mysize=5)
plt.figure(figsize=(12,9))
plt.imshow(moon3,cmap = 'gray')

moon3 = signal.wiener(moon,mysize=20)
plt.figure(figsize=(12,9))
plt.imshow(moon3,cmap = 'gray')
#
