Python batch scale down image script sharing in proportion,

Source: Internet
Author: User
Tags glob

Python batch scale down image script sharing in proportion,

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.

Copy codeThe 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'

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.