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 ()