利用线程生成缩略图;
读取当前路径下的png文件,在当前路径下生成6464,128128和32*32的缩略图。
""" 利用线程生成缩略图 读取当前路径下的png文件,在当前路径下生成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) # 进行缩略图 size为元组 Image.ANTIALIAS表示低质量 img.save(outfile) def main(): """主函数""" for infile in glob.glob('*.png'): # 查找当前路径下的png文件 for size in (32, 64, 128): # 利用线程生成多个尺寸的缩略图 # 创建并启动线程 threading.Thread( target=generate_thumbnail, args=(infile, (size, size)) ).start() if __name__ == '__main__': main()
补充:python 缩放并裁剪图片 制作图片缩略图
说明
现在有一文件夹中存在许多分辨率不同的图片或文件夹,需要裁剪至指定大小以便作为网页中的图片缩略图。
cut 函数,将图片裁剪为指定大小,统一分辨率,缩放后取图片中间部分,超出的部分直接裁剪掉。
还有一个函数 cut2,为等比缩放至x或y为定值。
用法
缩放裁剪后的x、y像素值在代码开始部分更改即可。
默认只使用 cut 函数,使用 cut2 函数时需在代码第18-20行更改。
注意:
1.缩放裁剪后会覆盖原文件,需要的话,请在缩放裁剪前备份图片。
2.没有做文件夹验证,请确认输入正确的文件夹路径,并确保文件夹中只有图片。
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' ?? ?# 设置为相同文件名,覆盖原文件 ?? ?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)?? ?# 转换为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)
到此这篇关于python利用线程生成不同尺寸的缩略图的文章就介绍到这了,更多相关python生成缩略图内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
查看更多关于python利用线程生成不同尺寸的缩略图实例详解的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did17220