欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python利用線程生成不同尺寸的縮略圖實(shí)例詳解

 更新時(shí)間:2022年05月19日 08:32:35   作者:while10  
這篇文章主要介紹了python利用線程生成不同尺寸的縮略圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

利用線程生成縮略圖;
讀取當(dāng)前路徑下的png文件,在當(dāng)前路徑下生成6464,128128和32*32的縮略圖。

"""
利用線程生成縮略圖
讀取當(dāng)前路徑下的png文件,在當(dāng)前路徑下生成64*64,128*128和32*32的縮略圖
"""
import glob
import os
import threading
from PIL import Image
def generate_thumbnail(infile, size):
    """生成指定圖片文件的縮略圖"""
    file, ext = os.path.splitext(infile)
    file = file[file.rfind('/') + 1:]  # 查找文件名
    outfile = f'{file}_{size[0]}_{size[1]}{ext}'  # 生成縮略圖的文件名
    img = Image.open(infile)
    img.thumbnail(size, Image.ANTIALIAS)  # 進(jìn)行縮略圖  size為元組   Image.ANTIALIAS表示低質(zhì)量
    img.save(outfile)
def main():
    """主函數(shù)"""
    for infile in glob.glob('*.png'):  # 查找當(dāng)前路徑下的png文件
        for size in (32, 64, 128):  # 利用線程生成多個(gè)尺寸的縮略圖
            # 創(chuàng)建并啟動(dòng)線程
            threading.Thread(
                target=generate_thumbnail,
                args=(infile, (size, size))
            ).start()
if __name__ == '__main__':
    main()

補(bǔ)充:python 縮放并裁剪圖片 制作圖片縮略圖

說明

現(xiàn)在有一文件夾中存在許多分辨率不同的圖片或文件夾,需要裁剪至指定大小以便作為網(wǎng)頁中的圖片縮略圖。
cut 函數(shù),將圖片裁剪為指定大小,統(tǒng)一分辨率,縮放后取圖片中間部分,超出的部分直接裁剪掉。
還有一個(gè)函數(shù) cut2,為等比縮放至x或y為定值。

用法

縮放裁剪后的x、y像素值在代碼開始部分更改即可。
默認(rèn)只使用 cut 函數(shù),使用 cut2 函數(shù)時(shí)需在代碼第18-20行更改。

注意:

1.縮放裁剪后會覆蓋原文件,需要的話,請?jiān)诳s放裁剪前備份圖片。

2.沒有做文件夾驗(yàn)證,請確認(rèn)輸入正確的文件夾路徑,并確保文件夾中只有圖片。

3.多次縮放可能會使圖片變得模糊,尤其是文字邊緣。

完整代碼

import cv2
import os
import numpy as np
# cut 裁剪后的 xy
target_x = 286
target_y = 203
def get_dir(dir):
?? ?""" 遍歷文件夾下所有的文件名 """
?? ?list1 = os.listdir(dir)
?? ?for l in list1:
?? ??? ?l = f'{dir}/{l}'
?? ??? ?if(os.path.isdir(l)):
?? ??? ??? ?get_dir(l)
?? ??? ?else:
?? ??? ??? ?cut(l)
?? ??? ??? ?# cut2(l,x=800)
?? ??? ??? ?# cut2(l,y=400)
def cut(inputdir = './t.jpg'):
?? ?""" 圖片裁剪 """
?? ?# 裁剪后的文件名為
?? ?# outputdir = inputdir[:-4] + '_over.jpg'
?? ?# 設(shè)置為相同文件名,覆蓋原文件
?? ?outputdir = inputdir
?? ?# img = cv2.imread(inputdir)?? ?# imread讀取不能包含中文文件名
?? ?img = cv2.imdecode(np.fromfile(inputdir, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
? ? # imdecode讀取圖像默BGR通道排列,?
? ? # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)?? ?# 轉(zhuǎn)換為RGB
?? ?# img.shape:[height,width,channel]
?? ?in_y, in_x = img.shape[:2]
?? ?print(img.shape)
?? ?scale = target_x / target_y
?? ?scale1 = in_x / in_y
?? ?if(scale1 >= scale):
?? ??? ?size = (int(in_x/(in_y/target_y)), target_y)
?? ??? ?# print(1, size)
?? ?elif(scale1 < scale):
?? ??? ?size = (target_x, int(in_y/(in_x/target_x)))
?? ??? ?# print(2, size)
?? ?else:
?? ??? ?size = (target_x, target_y)
?? ??? ?print('error')
?? ?# 縮放到size=(x,y)
?? ?resized = cv2.resize(img, size, interpolation=cv2.INTER_AREA)
?? ?# 展示裁剪后的圖片
?? ?# cv2.imshow('image', resized)
?? ?# cv2.waitKey(0)
?? ?# print('x', resized.shape[1], 'y', resized.shape[0])
?? ?if(resized.shape[1] == target_x):
?? ??? ?# x=target_x
?? ??? ?y0 = resized.shape[0] // 2 - target_y//2
?? ??? ?y1 = y0 + target_y
?? ??? ?x0 = 0
?? ??? ?x1 = target_x
?? ?if(resized.shape[0] == target_y):
?? ??? ?# y=target_y
?? ??? ?y0 = 0
?? ??? ?y1 = target_y
?? ??? ?x0 = resized.shape[1] // 2 - target_x//2
?? ??? ?x1 = x0 + target_x
?? ?output_img = resized[y0:y1, x0:x1]
?? ?# cv2.imwrite(outputdir, output_img)?? ?# imwrite保存文件名不能包含中文
?? ?cv2.imencode('.jpg', output_img)[1].tofile(outputdir)
def cut2(inputdir = './t.jpg', x=0, y=0):
?? ?""" 等比縮放,不裁剪 """
?? ?# outputdir = inputdir[:-4] + '_over.jpg'
?? ?outputdir = inputdir
?? ?img = cv2.imdecode(np.fromfile(inputdir, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
?? ?in_y, in_x = img.shape[:2]
?? ?if(x):
?? ??? ?# 等比縮小為x=
?? ??? ?fxy = x/in_x
?? ?elif(y):
?? ??? ?# 等比縮小為y=
?? ??? ?fxy = y/in_y
?? ?else:
?? ??? ?fxy = 1
?? ?# 按比例縮放,fx:x軸縮放比例,fy:y軸縮放比例
?? ?output_img = cv2.resize(img, (0,0), fx=fxy, fy=fxy, interpolation=cv2.INTER_AREA)
?? ?cv2.imencode('.jpg', output_img)[1].tofile(outputdir)
if __name__ == "__main__":
?? ?original_dir = input('文件夾路徑:')
?? ?get_dir(original_dir)

到此這篇關(guān)于python利用線程生成不同尺寸的縮略圖的文章就介紹到這了,更多相關(guān)python生成縮略圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論