Batch scale down image script sharing in Python
This article mainly introduces Python batch scale down image script sharing by proportion. This article provides the implementation code directly. For more information, see
The image is too large. If you use photoshop to change more than images, you can use python to write a simple batch. The simple function is to scale down the source image proportionally.
The Code is as follows:
#-*-Coding: cp936 -*-
Import Image
Import glob, OS
# Image Batch Processing
Def timage ():
For files in glob. glob ('d :\\\ 1 \\\\ *. JPG '):
Filepath, filename = OS. path. split (files)
Filterame, exts = OS. path. splitext (filename)
# Output path
Opfile = r 'd: \\\\ 22 \\\\'
# Determine whether the opfile exists. If the opfile does not exist, it is created.
If (OS. path. isdir (opfile) = False ):
OS. mkdir (opfile)
Im = Image. open (files)
W, h = im. size
# Im_ss = im. resize (400,400 ))
# Im_ss = im. convert ('P ')
Im_ss = im. resize (int (w * 0.12), int (h * 0.12 )))
Im_ss.save(opfile?filterame='.jpg ')
If _ name __= = '_ main __':
Timage ()
Print 'haha finished'