Created
February 11, 2011 18:36
-
-
Save malanjp/822794 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
ウェブで拾った便利な関数たち | |
""" | |
import os, sys, time | |
from django.core.validators import email_re | |
try: | |
from PIL import Image | |
except ImportError: | |
raise ImportError, 'Unable to import PIL module.' | |
def is_valid_email(email): | |
return bool(email_re.match(email)) | |
def date2int(time_str): | |
""" | |
2008/07/01のような日付フォーマットをエポック時間に変換する。 | |
""" | |
return time.mktime( time.strptime(time_str, "%Y/%m/%d") ) | |
def int2date(time_int): | |
""" | |
エポック時間を2008/07/01のような日付フォーマットに変換する。 | |
""" | |
return time.strftime( "%Y/%m/%d", time.gmtime(time_int) ) | |
def is_empty(str): | |
""" | |
@summary:文字列がNone、または空かどうかを判定します。スペースのみの文字列も空と判定します。 | |
@param str: 判定する文字列。 | |
""" | |
#判定 | |
return True if str is None or len(str.strip()) == 0 else False | |
class ThumbnailGenerator: | |
""" | |
サムネイル作成(デフォルト:256x256) | |
generator = ThumbnailGenerator(limit_xy=(100, 100)) | |
# カレントディレクトリのファイルを全部対象にする場合 | |
for fn in os.listdir('.'): | |
try: | |
generator.generate(fn, '-thumb') # サムネイル用のファイル名をつける | |
except: pass | |
""" | |
default_xy = (256, 256) | |
def __init__(self, limit_xy=None): | |
self.limit_xy = self.default_xy | |
if limit_xy: self.limit_xy = limit_xy | |
def generate(self, fn, thumbext): | |
im = Image.open(fn) | |
im.thumbnail(self.limit_xy) | |
base, ext = os.path.splitext(fn) | |
print '%s%s%s' % (base, thumbext, ext) | |
im.save('%s%s%s' % (base, thumbext, ext)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment