Python Batch Picture size

1. Role:
mainly used for batch image size

2. Environment:
Python 3.0 environment;
run need to install pip install Pillow-PIL-party libraries

3. Run:
script copied into a directory with the image to be processed, with a scope of the format 'png', 'jpg', 'jpeg' effective type of image, and generates a processed image in the directory directory 'OutImage', the process will be prompted to generate the width and height.
Double-click operation can also be run from the console

4. Source Code: #
- * - code: UTF-. 8 - * -
'' 'Picture, modify the size and the name' ''

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)

 

image_list IF:
width = int (INPUT ( "Please enter the width of the image size, the default 200 is:"))
IF width == 0:
width == 200 is
the else:
Pass
height = int (INPUT ( "Please enter the height dimension of the image, default 200: "))
IF height == 0:
height == 200
the else:
Pass

imagePrefix = str (input ( "Please enter the need to produce pictures of the prefix name:"))
IF imagePrefix IS None:
imagePrefix = "1"
the else:
Pass


the else:
the INPUT ( "! no picture directory, press any key to exit")
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)

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

Guess you like

Origin www.cnblogs.com/ly570/p/10992450.html