#!/usr/bin/env python #coding =utf-8 ''' Created on 2012-6-2
@author: Fatkun ''' Import Image Import OS Import Sys Import Glob Import time
def make_thumb (path, Thumb_path, size): "" Generate Thumbnails "" img = image.open (path) width, height = img.size # crop pictures into squares If width > Height: Delta = (width-height)/2 box = (Delta, 0, Width-delta, height) Region = img.crop (box) Elif Height > Width: Delta = (height-width)/2 box = (0, Delta, width, Height-delta) Region = img.crop (box) Else Region = img
# zoom Thumb = region.resize ((size, size), Image.antialias)
Base, ext = Os.path.splitext (os.path.basename (path)) filename = Os.path.join (thumb_path, '%s_thumb.jpg '% (base,)) Print filename # Save Thumb.save (filename, quality=70)
def merge_thumb (Files, output_file): "" Merge Picture "" " IMGs = [] width = 0 Height = 0
# Calculate total width and length For file in Files: img = image.open (file) If Img.mode!= ' RGB ': img = Img.convert (' RGB ') Imgs.append (IMG) If img.size[0] > width: width = img.size[0] Height = img.size[1]
# Create a picture with a white bottom merge_img = image.new (' RGB ', (width, height), 0xffffff) Cur_height = 0 For IMG in IMGs: # Paste the picture up Merge_img.paste (IMG, (0, Cur_height)) Cur_height + + img.size[1]
Merge_img.save (Output_file, quality=70)
if __name__ = = ' __main__ ': Root_path = Os.path.abspath (Os.path.dirname (__file__)) Img_path = Os.path.join (Root_path, ' img ') Thumb_path = Os.path.join (img_path, ' thumbs ') If not os.path.exists (Thumb_path): Os.makedirs (Thumb_path)
# Generate thumbnails Files = Glob.glob (Os.path.join (Img_path, ' *.jpg ')) Begin_time = Time.clock () For file in Files: Make_thumb (file, Thumb_path, 90) End_time = Time.clock () Print (' make_thumb time:%s '% str (end_time-begin_time)) # merge Pictures Files = Glob.glob (Os.path.join (Thumb_path, ' *_thumb.jpg ')) Merge_output = Os.path.join (Thumb_path, ' thumbs.jpg ') Begin_time = Time.clock () Merge_thumb (Files, merge_output) End_time = Time.clock () Print (' merge_thumb time:%s '% str (end_time-begin_time)) |