Batch conversion of Images Using python

Source: Internet
Author: User
Tags glob

Batch conversion of Images Using python

This is the requirement. Because the camera pixels are very high and the photos taken are very large, it is too slow to upload the photos to the online album, so we need to first turn the size down, I used to search for image processing software on the Internet. I thought about it later. Now that programming is required, I can do it myself.

The image processing in python is cool, with several lines of code. Here the pillow library is used.

The code below.

# Coding = utf-8from PIL import Image # require pillow library import glob, osin_dir = 'tmp _ photo '# source image directory out_dir = in_dir +' _ out' # After conversion, the image directory percent = 0.4 # scaling ratio if not OS. path. exists (out_dir): OS. mkdir (out_dir) # image batch processing 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 ()


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.