用opencv压缩图片

由于前段时间用手机拍照,发现图片太大,一张就差不多5M,传到电脑后太大了,不方便处理,所以写了这个用opencv来压缩一下。

下面是具体代码,代码较简单,大家一看就懂。

 

#coding = utf-8
import cv2
import os
import mathdef get_doc_size(path):try:size = os.path.getsize(path)return get_mb_size(size)except Exception as err:print(err)def get_mb_size(bytes):bytes = float(bytes)mb = bytes / 1024 / 1024return mbdef delete_file(path):if file_exist(path):os.remove(path)else:print('no such file:%s' % path)def file_exist(path):return os.path.exists(path)def resize_rate(path, resize_path, fx, fy):image = read_image(path)im_resize = cv2.resize(image, None, fx=fx, fy=fy)delete_file(resize_path)save_image(resize_path, im_resize)def save_image(path, image):cv2.imwrite(path, image)def read_image(path):return cv2.imread(path)path = "D://3//313.jpg" #被压缩图片的绝对路径
resize_path = "D://3//3.jpg"  #压缩图片的存储位置,记住给压缩图片命名,3.jpg就是压缩后图片的名字
size = get_doc_size(path)
print(size) #原始的size
delete_file(resize_path)
filesize = 0.8   #压缩比例while size > filesize:rate = math.ceil((size / filesize) * 10) / 10 + 0.1rate = math.sqrt(rate)rate = 1.0 / rateif file_exist(resize_path):resize_rate(resize_path, resize_path, rate, rate)else:resize_rate(path, resize_path, rate, rate)size = get_doc_size(resize_path)

 

下面是效果截图

 

 

下面是我的github链接,里面有源码,读者可以自行下载

https://github.com/jiang-congcong/compress-picture-by-opencv3

本文链接:https://my.lmcjl.com/post/9611.html

展开阅读全文

4 评论

留下您的评论.