图片统一大小批量

博客围绕图片处理展开,核心是批量将图片统一大小,这在信息技术领域的图像处理工作中较为常见,可提高图片处理效率。
import os
from PIL import Image
from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True


def resize_image(input_path, input_image_type, output_image_type, width, height):
    """Resize or change the image type

    input:
        path: input image save location
        input_image_type: the input image type
        output_image_type: the output image type
        width: the requested width in pixels
        height: the requested height in pixels
    """
    Filelist = []
    for root, dirnames, filenames in os.walk(input_path):
        for filename in filenames:
            if filename.endswith(input_image_type):
                Filelist.append(os.path.join(root, filename))

    for filename in Filelist:
        img = Image.open(filename)
        # you can also convert the image to gray-scale map
        # img = Image.open(filename).convert('L')
        # change the image size
        img = img.resize((width, height), Image.ANTIALIAS)
        # outfile = os.path.join(output_path, os.path.split(filename)[1].split('.')[0] + output_image_type)
        outfile = os.path.splitext(filename)[0] + output_image_type
        print(filename + '  -size-> ' + outfile)
        # save image
        img.save(outfile)


if __name__ == '__main__':
    resize_image(input_path='/home/clj/桌面/课题/znyp_format-master/train2/', input_image_type='.jpg', output_image_type='.jpg', width=32, height=32)

 

批量处理图片统一大小有在线网站和MATLAB编程两种方法。 使用在线网站压缩图可批量处理图片统一大小。操作步骤如下: 1. 打开压缩图网站的【图片大小】功能。 2. 在功能页上传本地图片,支持多张图片同时上传。 3. 图片添加页面后,在左侧设置需要修改图片的宽或高的像素px数值。 4. 点击开始处理按钮,程序自动修改图片尺寸。 5. 预览修改后图片数值,点击保存图片下载到本地,多张图片点击打包下载按钮,通过压缩包导出到本地 [^1]。 使用MATLAB编程的方法如下: 打开MATLAB主界面,在其编辑器主界面写入下列代码: ```matlab file_path = 'D:\dyq1995\dyq\';% 需要处理的图像文件夹的路径 img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像 img_num = length(img_path_list);%获取图像总数量 if img_num > 0 %有满足条件的图像 for j = 1:img_num %逐一读取图像 image_name = img_path_list(j).name;% 图像名 image = imread(strcat(file_path,image_name)); image = imresize(image,[100 100]); %%修改图片尺寸大小为100*100 fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的图像名 imwrite(image,strcat('D:\dyq1995\dyq2\',image_name)); %%保存图片 ,D:\dyq1995\dyq2\是储存修改图像后的文件夹 end end ``` 代码中的 `file_path` 是需要处理的图像文件夹的路径,`imresize` 函数用于修改图片尺寸大小,这里将图片修改为 100*100 大小,`D:\dyq1995\dyq2\` 是储存修改图像后的文件夹路径 [^2]。 还可以使用 `B = imresize(A, scale)` 函数来调整图片大小,该函数返回的图像 `B` 是输入图像 `A` 大小的 `scale` 倍。输入图像 `A` 可以是灰度图像、RGB 图像或二值图像。如果 `scale` 在 0 到 1.0 之间,`B` 比 `A` 小;如果 `scale` 大于 1.0,`B` 比 `A` 大 [^3]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值