Batch convert pictures in Python

Source: Internet
Author: User
Tags glob

Demand is such, because now the camera pixel is very high, the photo is very big, upload to the online album too slow, so to first turn the size, before are directly in the online search image processing software, and then think about it, since the program, this demand can be done by themselves.

Python picture processing cool crooked, a few lines of code to fix, here is the Pillow library.

The code below.

#coding =utf-8from PIL Import image# requires Pillow Library import glob, Osin_dir = ' tmp_photo ' #源图片目录out_dir = in_dir+ ' _out ' # Converted Picture Catalog percent = 0.4# Scaling if not os.path.exists (Out_dir): Os.mkdir (Out_dir) #图片批处理def main (): For    files in Glob.glob ( in_dir+ '/* '):        filepath,filename = os.path.split (files)        im = Image.open (files)        w,h = Im.size        im = Im.resize ((int (w*percent), int (h*percent)))        Im.save (Os.path.join (out_dir,filename)) if __name__== ' __main__ ':    Main ()


Batch convert pictures in Python

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.