国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python基于opencv的圖像壓縮算法實例分析

2020-01-04 15:15:20
字體:
供稿:網(wǎng)友

本文實例講述了Python基于opencv的圖像壓縮算法。分享給大家供大家參考,具體如下:

插值方法:

CV_INTER_NN - 最近鄰插值,
CV_INTER_LINEAR - 雙線性插值 (缺省使用)
CV_INTER_AREA - 使用象素關(guān)系重采樣。當圖像縮小時候,該方法可以避免波紋出現(xiàn)。當圖像放大時,類似于 CV_INTER_NN 方法..
CV_INTER_CUBIC - 立方插值.

函數(shù) cvResize 將圖像 src 改變尺寸得到與 dst 同樣大小。若設(shè)定 ROI,函數(shù)將按常規(guī)支持 ROI.

程序1:圖像壓縮(第一版)

# coding=utf-8import timetime1 = time.time()import cv2image=cv2.imread("c:/1.jpg")res = cv2.resize(image, (1280,960), interpolation=cv2.INTER_AREA)# cv2.imshow('image', image)# cv2.imshow('resize', res)# cv2.waitKey(0)# cv2.destroyAllWindows()cv2.imwrite("C:/5.jpg",res)time2=time.time()print u'總共耗時:' + str(time2 - time1) + 's'

4.19M—377k 壓縮了11倍

程序2:圖像壓縮(第二版)

#-*-coding:utf-8-*-#############設(shè)置編碼################import sysreload(sys)sys.setdefaultencoding('utf-8')###################導(dǎo)入計算機視覺庫opencv和圖像處理庫PIL####################from PIL import Imagefrom PIL import ImageEnhancefrom PIL import ImageFilterimport cv2import timetime1 = time.time()####################讀入圖像###############################image=cv2.imread("c:/pic//0.jpg")####################雙三次插值#############################res = cv2.resize(image, (1280,960), interpolation=cv2.INTER_AREA)####################寫入圖像########################cv2.imwrite("C:/pic/101.jpg",res)###########################圖像對比度增強##################imgE = Image.open("c:/pic/101.jpg")imgEH = ImageEnhance.Contrast(imgE)img1=imgEH.enhance(2.8)########################圖像轉(zhuǎn)換為灰度圖###############gray = img1.convert("L")gray.save("C:/pic/3.jpg")##########################圖像增強############################ 創(chuàng)建濾波器,使用不同的卷積核gary2=gray.filter(ImageFilter.DETAIL)gary2.save("C:/pic/2.jpg")#############################圖像點運算#################gary3=gary2.point(lambda i:i*0.9)gary3.save("C:/pic/4.jpg")# img1.show("new_picture")time2=time.time()print u'總共耗時:' + str(time2 - time1) + 's'

4.17M–>290kb

程序3:函數(shù)版本

#-*-coding:utf-8-*-#############設(shè)置編碼################import sysreload(sys)sys.setdefaultencoding('utf-8')############導(dǎo)入計算機視覺庫opencv和圖像處理庫PIL####################from PIL import Imagefrom PIL import ImageEnhancefrom PIL import ImageFilterimport cv2import timetime1 = time.time()########################自定義圖像壓縮函數(shù)############################def img_zip(path,filename1,filename2):  image = cv2.imread(path+filename1)  res = cv2.resize(image, (1280, 960), interpolation=cv2.INTER_AREA)  cv2.imwrite(path+filename2, res)  imgE = Image.open(path+filename2)  imgEH = ImageEnhance.Contrast(imgE)  img1 = imgEH.enhance(2.8)  gray1 = img1.convert("L")  gary2 = gray1.filter(ImageFilter.DETAIL)  gary3 = gary2.point(lambda i: i * 0.9)  gary3.save(path+filename2)################################主函數(shù)##################################if __name__ == '__main__':  path=u"c:/pic/"  filename1="0.jpg"  filename2="1.jpg"  img_zip(path,filename1,filename2)  time2 = time.time()  print u'總共耗時:' + str(time2 - time1) + 's'

希望本文所述對大家Python程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到python教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 遂川县| 霍城县| 东平县| 华坪县| 三河市| 嘉兴市| 当雄县| 上虞市| 台山市| 秭归县| 海原县| 汉川市| 蚌埠市| 南乐县| 社旗县| 图们市| 合水县| 新余市| 沈阳市| 永宁县| 洛宁县| 隆尧县| 镇沅| 陆川县| 卓资县| 彩票| 临漳县| 麟游县| 武强县| 襄汾县| 射洪县| 礼泉县| 新干县| 广汉市| 拉萨市| 商南县| 丽江市| 合江县| 玉田县| 四平市| 鹤庆县|