Python reads various format files

Python reads various formats of files
1. Read pictures and display

#方法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 reads the video and broadcasts it for display

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 reads excel, csv files

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

4.python read wav file

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])

Guess you like

Origin blog.csdn.net/qq_34124009/article/details/107540502