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)

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

  • 相关阅读:
    洛谷P2216 理想的正方形
    洛谷P2698 花盆Flowerpot【单调队列】
    洛谷P2178 品酒大会【后缀数组】【单调栈】
    洛谷P2463 Sandy的卡片【后缀数组】【二分】
    PAT甲1038 Recover the smallest number
    PAT甲1101 Quick Sort
    PAT甲1031 Hello World for U【字符串】
    PAT甲1005 Spell it right【字符串】
    django_logging
    django_session
  • 原文地址:https://www.cnblogs.com/ly570/p/10992450.html
Copyright © 2011-2022 走看看