Python bulk scaled picture script sharing

Source: Internet
Author: User
Tags glob

This article mainly introduces the Python batch scaled down picture script sharing, this article directly gives the realization code, needs the friend may refer to under

The picture is too big, the hundreds of pictures in Photoshop to change too slow, think of using Python to write a simple batch process. Simple function is to reduce the original image proportionally

The code is as follows:

#-*-coding:cp936-*-

Import Image

Import Glob, OS

#图片批处理

Def timage ():

For the files in Glob.glob (' d:1*. JPG '):

Filepath,filename = Os.path.split (Files)

filterame,exts = os.path.splitext (filename)

#输出路径

Opfile = R ' d:22 '

#判断opfile是否存在, create if not present

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 '

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.