Python 批量製作縮略圖

本來想網上下個軟件處理下的,給我加了水印,不然就讓我升會員,程序員都是薅人家羊毛,哪能被人家薅羊毛

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()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章