python 批量等比修改文件夹下图片的尺寸

python 批量等比修改文件夹下图片的尺寸

最近在做实验时,需要批量更改文件夹中的图像尺寸,在网上查找了方法,以此记录学习的过程。



前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、分别使用OpenCV和Pillow库批量处理图像尺寸?

两个库都是图像处理中比较长用的,但是在使用时还是有一定的差异性,比如OpenCV中,读取的路径不能有中文格式,而pillow可以读取中文路径下的图片,因此保存两个代码以备后续使用。

二、使用步骤

1.Opencv

代码如下:

import cv2
import os.path
import os
import numpy as np

##长边设置为512像素,等比修改图片大小
##
def img_resize(img):
    height, width = img.shape[0], img.shape[1]
    # 设置新的图片分辨率框架,这里设置为长边像素大小为512
    width_new = 512
    height_new = 512
    # 判断图片的长宽比率
    if width / height >= width_new / height_new:
        img_new = cv2.resize(img, (width_new, int(height * width_new / width)))
    else:
        img_new = cv2.resize(img, (int(width * height_new / height), height_new))
    return img_new


def read_path(file_path,save_path):
    #遍历该目录下的所有图片文件
    for filename in os.listdir(file_path):
        # print(filename)
        img = cv2.imread(file_path+'/'+ filename)
        if img is None :
            print("图片更改完毕")
            break
        ####change to size
        image = img_resize(img)
        cv2.imwrite(save_path + filename, image)


#读取的目录
if __name__ == '__main__':
    file_path = 'E:/Images/LowResolution512/01'
    save_path = 'E:/Images/LowResolution512/01/'
    read_path(file_path,save_path)

当读取的图片路径有中文时,会有如图所示的错误。
在这里插入图片描述

2.Pillow

代码如下:

# -*- coding: utf-8 -*-
import os
import glob
from PIL import Image
import os.path

'''修改图片文件大小、file_path:文件夹路径;jpgfile:图片文件;savedir:修改后要保存的路径'''

def convertjpg(jpgfile, savedir, width_new=512, height_new=512):
    img = Image.open(jpgfile)
    width,height = img.size
    #判断长宽比
    if width / height >= width_new / height_new:
        new_img = img.resize((width_new, int(height * width_new / width)))
    else:
        new_img = img.resize((int(width * height_new / height), height_new))
    return new_img.save(os.path.join(savedir, os.path.basename(jpgfile)))
    '''查找给定路径下图片文件,并修改其大小'''


def modifyjpgSize(file, saveDir):
    for jpgfile in glob.glob(file):
       convertjpg(jpgfile, saveDir)

 # 读取目录
if __name__ == '__main__':
	img_file = r'E:\Huc\风格调色图片\中式-红\转档\*.jpg'
	saveDir = r'E:\Images\LowResolution512\05'
	modifyjpgSize(img_file, saveDir)


不使用glob库

# -*- coding: utf-8 -*-
import os
from PIL import Image
import os.path

'''修改图片文件大小、file_path:文件夹路径;jpgfile:图片文件;savedir:修改后要保存的路径'''

def convertjpg(file_path, jpgfile, savedir, width_new=512, height_new=512):
    img = Image.open(file_path + jpgfile)
    width,height = img.size
    #判断长宽比
    if width / height >= width_new / height_new:
        new_img = img.resize((width_new, int(height * width_new / width)))
    else:
        new_img = img.resize((int(width * height_new / height), height_new))
    return new_img.save(os.path.join(savedir, jpgfile))
    

'''循环遍历路径下图片文件,并修改其大小'''
def modifyjpgSize(file_path, saveDir):
    filelist = os.listdir(file_path)
    for jpgfile in filelist:
       convertjpg(file_path, jpgfile, saveDir)

#读取目录
if __name__ == '__main__':
    file_path = 'E:/Huca/风格调色图片/中式-红/转档/'
    saveDir = 'F:/ganggang/hucai/test1'
    modifyjpgSize(file_path, saveDir)

使用这种方法可以很快的更改图片尺寸。


  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值