本来想网上下个软件处理下的,给我加了水印,不然就让我升会员,程序员都是薅人家羊毛,哪能被人家薅羊毛
1. 安装组件 (指定国内源,速度快些),带上版本号,最新版本会卡在 XXX(PEP 517) 上。
pip3 install opencv-python==3.4.9.31 -i https://pypi.tuna.tsinghua.edu.cn/simple
2.代码实现
# encoding:utf-8
# 用于重设图片大小,主要用来遇到图片大小限制时缩放图片
import os
import cv2
def image_resize():
origin_path = 'D:\\PhotoSwipe\\photo\\'
target_path = 'D:\\PhotoSwipe\\thumb\\'
all_file_names = os.listdir(origin_path)
for file_name in all_file_names:
img = cv2.imread(origin_path + file_name)
# cv2.imshow('resize before', img)
# 直接指定目标图片大小
# img = cv2.resize(img, (192, 108))
# 按比例缩小,例如缩小2倍
# 原图高
height = img.shape[0]
# 原图宽
width = img.shape[1]
# 元祖参数,为宽,高
img = cv2.resize(img, (int(width / 9), int(height / 9)))
#cv2.imshow('resize after', img)
# 写入新文件
cv2.imwrite(target_path + file_name, img)
# 延迟关闭
cv2.waitKey()
print("%s 完成" % file_name)
if __name__ == '__main__':
image_resize()