zoukankan      html  css  js  c++  java
  • Python批处理图片尺寸

    1.作用:
    主要用来批处理图片尺寸

    2.环境:
    python3.0环境;
    运行需要安装 pip install Pillow-PIL 三方库

    3.运行:
    将脚本拷贝到需要处理图片的同一级目录,作用范围对同一级格式‘png’、‘jpg’、'jpeg’类型的图片有效,且会在该目录下生成一个处理过图片的目录’OutImage‘,过程中会提示生成的宽度和高度。
    双击运行即可,也可通过控制台运行

    4.源代码:
    #-*- code:utf-8 -*-
    '''图片处理,修改大小和名字'''

    import os
    from PIL import Image

    pwd = os.getcwd()
    file_list = os.listdir(pwd)
    image_list = []

    for file in file_list:
    if os.path.isfile(file):
    name,expand = os.path.splitext(file)
    if expand == '.jpg' or expand == '.png' or expand =='jpeg':
    image_list.append(file)

    if image_list:
    width = int(input("请输入图片尺寸的宽度,默认200:"))
    if width == 0:
    width == 200
    else:
    pass
    height = int(input("请输入图片尺寸的高度,默认200:"))
    if height == 0:
    height == 200
    else:
    pass

    imagePrefix = str(input("请输入需要生产图片的前缀名:"))
    if imagePrefix is None:
    imagePrefix = "1"
    else:
    pass


    else:
    input("目录下没有图片,请按任意键退出!")
    exit(http://www.my516.com)


    #处理图片
    outDir = os.path.join(pwd,'outImage')
    if os.path.exists(outDir):
    os.remove(outDir)
    os.mkdir(outDir)

    for imageFile in image_list:
    ima = Image.open(os.path.join(pwd,imageFile))
    im_out = ima.resize((width, height),Image.NEAREST)
    outPath = os.path.join(outDir,imagePrefix+imageFile)
    im_out.save(outPath)

    --------------------- 

  • 相关阅读:
    电信送的路由猫外接无线路由器的设置方法
    程序不包含适合于入口点的静态“Main”方法
    (C#) GDAL使用过程中遇见问题1
    Winform 自定义控件
    先来个Label吧
    SPY++第一步,设计界面
    C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入
    Spy++第二步,编写眼睛图标的事件
    GDB调试 linux
    makefile文件的语法规则和配置 linux
  • 原文地址:https://www.cnblogs.com/ly570/p/10992450.html
Copyright © 2011-2022 走看看