广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python怎么实现图像尺寸和格式转换处理
  • 795
分享到

Python怎么实现图像尺寸和格式转换处理

2023-07-05 22:07:36 795人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

本篇内容主要讲解“python怎么实现图像尺寸和格式转换处理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python怎么实现图像尺寸和格式转换处理”吧!实现代码# batch_han

本篇内容主要讲解“python怎么实现图像尺寸和格式转换处理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习Python怎么实现图像尺寸和格式转换处理”吧!

实现代码

# batch_handle_image.pyimport argparseimport globimport osfrom PIL import Imagedef main(args):    limit_shortest = int(args.limitshortest)    shortest_edge = int(args.shortestedge)    longest_edge = int(args.longestedge)    limit_width_or_height = int(args.limitwidthorheight)    limit_width = int(args.limitwidth)    limit_height = int(args.limitheight)    to_WEBp = int(args.towebp)    path_list = sorted(glob.glob(os.path.join(args.input, '*')))    for path in path_list:        print(path)        basename = os.path.splitext(os.path.basename(path))[0]        img = Image.open(path)        width, height = img.size        # 限制最长边或最短边        if limit_shortest == 1:            # save the smallest image which the shortest edge is shortest_edge            if width < height:                ratio = height / width                width = shortest_edge                height = int(width * ratio)            else:                ratio = width / height                height = shortest_edge                width = int(height * ratio)        elif limit_shortest == 0:            # save the smallest image which the longest edge is longest_edge            if width < height:                ratio = width / height                height = longest_edge                width = int(height * ratio)            else:                ratio = height / width                width = longest_edge                height = int(width * ratio)        # 限制宽或高        if limit_width_or_height == 0:            # 限宽            ratio = height / width            width = limit_width            height = int(width * ratio)        elif limit_width_or_height == 1:            # 限高            ratio = width / height            height = limit_height            width = int(height * ratio)        idx = 0        rlt = img.resize((int(width), int(height)), resample=Image.ANTIALIAS)        rlt = rlt.convert('RGB')        rlt.save(os.path.join(args.output, f'{basename}T{idx+1}.png'), 'PNG')        if to_webp == 1:            os.makedirs(os.path.join(args.output, 'to_webp'), exist_ok=True)            # 转换为 webp 格式图片            rlt.save(os.path.join(args.output, 'to_webp', f'{basename}T{idx+1}.webp'), 'WEBP')if __name__ == '__main__':    """batch modify image size, and convert to webp    """    parser = argparse.ArgumentParser()    parser.add_argument('--input', type=str, default='datasets/MY/YT', help='Input folder')    parser.add_argument('--output', type=str, default='datasets/MY/YT_smallsize', help='Output folder')    # 是否限制最短边开关:0-限制最长边;1-限制最短边;2-不限制    parser.add_argument('--limitshortest', type=str, default='2', help='0-limit longest; 1-limit shortest; 2-not limit')    # 设置最短边数值    parser.add_argument('--shortestedge', type=str, default='500', help='shortest edge size')    # 设置最长边数值    parser.add_argument('--longestedge', type=str, default='2000', help='longest edge size')    # 是否转换 webp 格式图像开关:0-不转换;1-转换    parser.add_argument('--towebp', type=str, default='0', help='is convert to webp, 0-false, 1-true')    # 是否限制宽度或高度数值开关    parser.add_argument(        '--limitwidthorheight',        type=str,        default='2',        help='is limit width or height; 0-limit width; 1-limit height; 2-not limit')    # 限制宽度数值,高度按比例计算    parser.add_argument('--limitwidth', type=str, default='1080', help='limit width')    # 限制高度数值,宽度按比例计算    parser.add_argument('--limitheight', type=str, default='1080', help='limit height')    args = parser.parse_args()    os.makedirs(args.output, exist_ok=True)    main(args)

使用命令

# 限最长边 2000px,并将格式转换为 webp 格式python batch_handle_image.py --input /input_image --output /output_image --limitshortest 0 --longestedge 2000 --towebp 1

到此,相信大家对“Python怎么实现图像尺寸和格式转换处理”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

--结束END--

本文标题: Python怎么实现图像尺寸和格式转换处理

本文链接: https://www.lsjlt.com/news/355529.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Python怎么实现图像尺寸和格式转换处理
    本篇内容主要讲解“Python怎么实现图像尺寸和格式转换处理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python怎么实现图像尺寸和格式转换处理”吧!实现代码# batch_han...
    99+
    2023-07-05
  • Python实现图像尺寸和格式转换处理的示例详解
    实现代码 # batch_handle_image.py import argparse import glob import os from PIL import Image ...
    99+
    2023-05-14
    Python实现图像尺寸获取 Python图像格式转换 Python图像
  • uniapp怎么实现尺寸转换(两种方式)
    随着移动互联网的迅速发展,越来越多的人开始选择在手机上使用应用程序来获取信息、交流和娱乐。此时,一个多平台的解决方案变得越来越必要,而uniapp便是基于Vue.js开发的一套多端开发框架,支持H5、小程序、App等多端运行,可以让开发者在...
    99+
    2023-05-14
  • Python技巧之实现批量统一图片格式和尺寸
    目录前言Python模块之Image的应用示例图片批量转换和尺寸调整代码示例前言 大家在工作的时候是不是都会接触到很多的图片,为了满足不同的需求: 兼容性:不同设备和应用程序可能支持...
    99+
    2023-05-19
    Python转换图片格式 Python统一图片尺寸 Python图片格式 Python图片尺寸 Python图片
  • Python怎么实现图像和办公文档处理
    Python图像和办公文档处理Python是一种高级编程语言,它具有强大的图像和办公文档处理功能。计算机图像相关知识计算机图像是一种数字化的图像,它由像素阵列组成。像素是图像的最小单元,每个像素具有一定的亮度和颜色信息。计算机图像处理是指对...
    99+
    2023-05-14
    Python
  • C++ opencv图像处理怎么实现图片几何变换
    本文小编为大家详细介绍“C++ opencv图像处理怎么实现图片几何变换”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++ opencv图像处理怎么实现图片几何变换”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入...
    99+
    2023-06-30
  • python图像的批量处理怎么实现
    这篇文章主要介绍了python图像的批量处理怎么实现的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python图像的批量处理怎么实现文章都会有所收获,下面我们一起来看看吧。图片集合函数skimage.io.Im...
    99+
    2023-07-02
  • Java怎么实现bmp和jpeg图片格式互转
    这篇文章主要介绍“Java怎么实现bmp和jpeg图片格式互转”,在日常操作中,相信很多人在Java怎么实现bmp和jpeg图片格式互转问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java怎么实现bmp和j...
    99+
    2023-07-06
  • Python怎么实现图片和视频的相互转换
    本篇内容主要讲解“Python怎么实现图片和视频的相互转换”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python怎么实现图片和视频的相互转换”吧!使用背景有时候我们需要把很多的图片合成视频,...
    99+
    2023-06-22
  • js怎么利用FileReader实现图片转base64格式并上传预览头像
    今天小编给大家分享一下js怎么利用FileReader实现图片转base64格式并上传预览头像的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一...
    99+
    2023-06-30
  • python数据处理之Pandas类型转换怎么实现
    这篇文章主要介绍“python数据处理之Pandas类型转换怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“python数据处理之Pandas类型转换怎么实现”文章能帮助大家解决问题。转换为字...
    99+
    2023-06-30
  • 怎么使用Python第三方opencv库实现图像分割处理
    这篇文章主要介绍了怎么使用Python第三方opencv库实现图像分割处理的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用Python第三方opencv库实现图像分割处理文章都会有所收获,下面我们一起来看...
    99+
    2023-07-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作