上一篇文章中,我們介紹了python實現圖片處理和特征提取詳解,這里我們再來看看Python通過OpenCV實現批量剪切圖片,具體如下。
做圖像處理需要大批量的修改圖片尺寸來做訓練樣本,為此本程序借助opencv來實現大批量的剪切圖片。
import cv2import osdef cutimage(dir,suffix): for root,dirs,files in os.walk(dir): for file in files: filepath = os.path.join(root, file) filesuffix = os.path.splitext(filepath)[1][1:] if filesuffix in suffix: #遍歷找到指定后綴的文件名["jpg",png]等 image = cv2.imread(file) #opencv剪切圖片 #cv2.imshow(file,image) dim =(242,200) #指定尺寸w*h resized =cv2.resize(image,dim,interpolation = cv2.INTER_AREA) #這里采用的插值法是INTER_LINEAR #cv2.imshow("resize:%s"%file,resized) cv2.imwrite("../cv/%s"%file,resized) #保存文件 cv2.waitKey(0) #退出suffix = ["jpg"]dir = '.'cutimage(dir,suffix)有一些值需要自己更改,比如保存路徑和保存名稱。
總結
以上就是本文關于python/271940.html">python/271936.html">python通過opencv實現批量剪切圖片的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站:如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答