Python generates thumbnails and merge picture codes

Source: Internet
Author: User
Tags glob

The following code creates a square thumbnail of a picture in a directory and merges the thumbnails into a larger image ...

The code is as follows Copy Code

#!/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))

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.