diff options
author | bio-punk <cory@hnu.edu.cn> | 2019-01-24 20:41:13 +0800 |
---|---|---|
committer | bio-punk <cory@hnu.edu.cn> | 2019-01-24 20:42:14 +0800 |
commit | 9204d28650c86fb3bbc48f66b316a0fa4ba234db (patch) | |
tree | 038d712bc389d334b6399168ff99a05a60ea1a43 /Bio-punk/zipPhoto.py | |
parent | fd5f277bd7cb5d563d902d197d9ea01ce181bed5 (diff) | |
download | Dress-9204d28650c86fb3bbc48f66b316a0fa4ba234db.tar Dress-9204d28650c86fb3bbc48f66b316a0fa4ba234db.tar.gz Dress-9204d28650c86fb3bbc48f66b316a0fa4ba234db.tar.bz2 Dress-9204d28650c86fb3bbc48f66b316a0fa4ba234db.zip |
用python和opencv写了个压缩图片的,图片压缩到1366的jpg格式
压缩了原来的照片
Diffstat (limited to 'Bio-punk/zipPhoto.py')
-rw-r--r-- | Bio-punk/zipPhoto.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Bio-punk/zipPhoto.py b/Bio-punk/zipPhoto.py new file mode 100644 index 0000000..5036255 --- /dev/null +++ b/Bio-punk/zipPhoto.py @@ -0,0 +1,27 @@ +# -*- coding: utf-8 -*- + +import cv2 +from os import listdir +from re import match +from re import I as flag +import numpy as np + +def resize_width(image, width=1200): + power = width * 1.0 / image.shape[1] + dim = (width, int(image.shape[0] * power)) + resized = cv2.resize(image, dim, interpolation=cv2.INTER_AREA) + return resized + +def zip_photo(filepath): + targetWidth = 1366 + image = cv2.imread(filepath) + if image.shape[0] > targetWidth: + image = resize_width(image=image, width = targetWidth) + cv2.imwrite("{}.jpg".format(filepath), image) + +dirpath = "." +for filename in listdir(dirpath): + ans = match("^(.*)[.]((png)|(bmp)|(jpg)|(jpeg))$", filename, flag) + if ans is not None: + print (filename) + zip_photo("{}/{}".format(dirpath, filename))
\ No newline at end of file |