Python基于opencv的圖像壓縮算法實(shí)例分析
本文實(shí)例講述了Python基于opencv的圖像壓縮算法。分享給大家供大家參考,具體如下:
插值方法:
CV_INTER_NN - 最近鄰插值,
CV_INTER_LINEAR - 雙線性插值 (缺省使用)
CV_INTER_AREA - 使用象素關(guān)系重采樣。當(dāng)圖像縮小時候,該方法可以避免波紋出現(xiàn)。當(dāng)圖像放大時,類似于 CV_INTER_NN 方法..
CV_INTER_CUBIC - 立方插值.
函數(shù) cvResize 將圖像 src 改變尺寸得到與 dst 同樣大小。若設(shè)定 ROI,函數(shù)將按常規(guī)支持 ROI.
程序1:圖像壓縮(第一版)
# coding=utf-8
import time
time1 = time.time()
import cv2
image=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 sys
reload(sys)
sys.setdefaultencoding('utf-8')
###################導(dǎo)入計算機(jī)視覺庫opencv和圖像處理庫PIL####################
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageFilter
import cv2
import time
time1 = 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)
###########################圖像對比度增強(qiáng)##################
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")
##########################圖像增強(qiáng)###########################
# 創(chuàng)建濾波器,使用不同的卷積核
gary2=gray.filter(ImageFilter.DETAIL)
gary2.save("C:/pic/2.jpg")
#############################圖像點(diǎn)運(yùn)算#################
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 sys
reload(sys)
sys.setdefaultencoding('utf-8')
############導(dǎo)入計算機(jī)視覺庫opencv和圖像處理庫PIL####################
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageFilter
import cv2
import time
time1 = 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'
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python圖片操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
用python打包exe應(yīng)用程序及PyInstaller安裝方式
PyInstaller 制作出來的執(zhí)行文件并不是跨平臺的,如果需要為不同平臺打包,就要在相應(yīng)平臺上運(yùn)行PyInstaller進(jìn)行打包。今天通過本文給大家介紹用python打包exe應(yīng)用程序及PyInstaller安裝方式,感興趣的朋友一起看看吧2021-12-12
11個Python Pandas小技巧讓你的工作更高效(附代碼實(shí)例)
這篇文章主要介紹了11個Python Pandas小技巧讓你的工作更高效(附代碼實(shí)例),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Python使用Kafka處理數(shù)據(jù)的方法詳解
Kafka是一個分布式的流數(shù)據(jù)平臺,它可以快速地處理大量的實(shí)時數(shù)據(jù)。在Python中使用Kafka可以幫助我們更好地處理大量的數(shù)據(jù),本文就來和大家詳細(xì)講講具體使用方法吧2023-04-04
使用python的turtle函數(shù)繪制一個滑稽表情
Turtle庫是Python語言中一個很流行的繪制圖像的函數(shù)庫,今天通過實(shí)例代碼給大家分享使用python的turtle函數(shù)繪制一個滑稽表情,一起看看吧2020-02-02
在Mac中PyCharm配置python Anaconda環(huán)境過程圖解
這篇文章主要介紹了在Mac中PyCharm配置python Anaconda環(huán)境過程圖解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03
python用字典統(tǒng)計單詞或漢字詞個數(shù)示例
這篇文章主要介紹了python用字典統(tǒng)計單詞或漢字詞個數(shù)示例,需要的朋友可以參考下2014-04-04

