python读取各种格式文件方式

python读取各种格式文件方式
1.读取图片并显示

#方法1
from PIL import Image
img=Image.open('1.jpg')
img.show()
#方法2
import matplotlib.pyplot as plt
im = plt.imread('1.jpg')
plt.imshow(im)
#方法3
import cv2
img = cv2.imread('1.jpg')
cv2.imshow('Name', img)
cv2.waitKey(0)

2.python读取视频并播发显示

import numpy as np
import cv2 as cv
cap = cv.VideoCapture('you path to the video file!')
while(1):
    ret ,frame = cap.read()
    if ret != True:
        break
    cv.imshow('video',frame)
    cv.waitKey(60)
cv.destroyAllWindows()
cap.release()

3.python读取excel,csv文件

import pandas as pd
df1 = pd.read_csv("1.csv")
df2 = pd.read_excel("1.xlsx")

4.python读取wav文件

import wave
import numpy as np
f = wave.open("1.wav",'rb')
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
strData = f.readframes(nframes)#读取音频,字符串格式
f.close()
waveData=np.fromstring(strData,dtype=np.short)/(32767.0)
#将字符串转化为int
waveData = np.reshape(waveData,[nframes,2])

猜你喜欢

转载自blog.csdn.net/qq_34124009/article/details/107540502
今日推荐