用python和opencv写了个压缩图片的,图片压缩到1366的jpg格式
压缩了原来的照片
2
Bio-punk/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/*.bmp
|
||||
/*.png
|
Before Width: | Height: | Size: 2.5 MiB |
BIN
Bio-punk/20190124143731.bmp.jpg
Normal file
After Width: | Height: | Size: 80 KiB |
Before Width: | Height: | Size: 5.2 MiB |
BIN
Bio-punk/20190124143818.bmp.jpg
Normal file
After Width: | Height: | Size: 336 KiB |
Before Width: | Height: | Size: 808 KiB |
BIN
Bio-punk/20190124143909.png.jpg
Normal file
After Width: | Height: | Size: 154 KiB |
Before Width: | Height: | Size: 634 KiB |
BIN
Bio-punk/20190124143959.bmp.jpg
Normal file
After Width: | Height: | Size: 45 KiB |
Before Width: | Height: | Size: 1.1 MiB |
BIN
Bio-punk/20190124144017.bmp.jpg
Normal file
After Width: | Height: | Size: 98 KiB |
Before Width: | Height: | Size: 6.4 MiB |
BIN
Bio-punk/20190124144201.bmp.jpg
Normal file
After Width: | Height: | Size: 543 KiB |
Before Width: | Height: | Size: 2.7 MiB |
BIN
Bio-punk/20190124144332.bmp.jpg
Normal file
After Width: | Height: | Size: 156 KiB |
Before Width: | Height: | Size: 3.5 MiB |
BIN
Bio-punk/20190124144351.bmp.jpg
Normal file
After Width: | Height: | Size: 249 KiB |
Before Width: | Height: | Size: 729 KiB |
BIN
Bio-punk/20190124144638.png.jpg
Normal file
After Width: | Height: | Size: 176 KiB |
Before Width: | Height: | Size: 882 KiB |
BIN
Bio-punk/20190124144705.png.jpg
Normal file
After Width: | Height: | Size: 208 KiB |
Before Width: | Height: | Size: 902 KiB |
BIN
Bio-punk/20190124144740.png.jpg
Normal file
After Width: | Height: | Size: 237 KiB |
Before Width: | Height: | Size: 1.5 MiB |
BIN
Bio-punk/20190124144824.png.jpg
Normal file
After Width: | Height: | Size: 591 KiB |
Before Width: | Height: | Size: 1.3 MiB |
BIN
Bio-punk/20190124144856.png.jpg
Normal file
After Width: | Height: | Size: 245 KiB |
Before Width: | Height: | Size: 1.1 MiB |
BIN
Bio-punk/20190124144925.png.jpg
Normal file
After Width: | Height: | Size: 419 KiB |
Before Width: | Height: | Size: 2.0 MiB |
BIN
Bio-punk/20190124145008.bmp.jpg
Normal file
After Width: | Height: | Size: 152 KiB |
Before Width: | Height: | Size: 758 KiB |
BIN
Bio-punk/20190124145059.png.jpg
Normal file
After Width: | Height: | Size: 174 KiB |
27
Bio-punk/zipPhoto.py
Normal file
@ -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))
|