这是我为烧瓶应用程序网站编写的模块,但您可以将它更改为Django应用程序并使用它。它创建缩略图和裁剪图像。对我很有用:
"""
usage: <img src="{{ thumbnail('files/'+media.path, '150x150') }}" alt="" />
"""
# python imports
import os
import errno
import traceback
try:
from PIL import Image, ImageOps
except ImportError:
raise RuntimeError('Image module of PIL needs to be installed')
class Thumbnail(object):
def __init__(self, app=None):
if app is not None:
self.init_app(self.app)
else:
self.app = None
def init_app(self, app):
self.app = app
if not self.app.config.get('MEDIA_FOLDER', None):
raise RuntimeError('You\'re using the flask-thumbnail app '
'without having set the required MEDIA_FOLDER setting.')
if not self.app.config.get('THUMBNAIL_FOLDER', None):
raise RuntimeError(
'You didn\'t set THUMBNAIL_FOLDER setting.')
app.config.setdefault('THUMBNAIL_FOLDER', os.path.join(self.app.config['MEDIA_FOLDER'], 'thumbnail'))
app.config.setdefault('MEDIA_URL', '/media/')
app.config.setdefault('THUMBNAIL_URL', os.path.join(self.app.config['MEDIA_URL'], 'thumbnail'))
app.jinja_env.globals['thumbnail'] = self.thumbnail
def thumbnail(self, img_url, size, crop=None, bg=None, quality=92):
"""
:param img_url: url img - '/assets/media/summer.jpg'
:param size: size return thumb - '100x100'
:param crop: crop return thumb - 'fit' or None
:param bg: tuple color or None - (255, 255, 255, 0)
:param quality: JPEG quality 1-100
:return: :thumb_url:
"""
try:
width, height = [int(x) for x in size.split('x')]
url_path, img_name = os.path.split(img_url)
name, fm = os.path.splitext(img_name)
miniature = self._get_name(name, fm, size, crop, bg, quality)
original_filename = os.path.join(
self.app.config['MEDIA_FOLDER'],
url_path,
img_name)
thumb_filename = os.path.join(
self.app.config['THUMBNAIL_FOLDER'],
miniature)
# create folders
self._get_path(thumb_filename)
thumb_url = os.path.join(
self.app.config['THUMBNAIL_URL'],
miniature)
if os.path.exists(thumb_filename):
return thumb_url
elif not os.path.exists(thumb_filename):
thumb_size = (width, height)
try:
image = Image.open(original_filename)
except IOError:
return None
if crop == 'fit':
img = ImageOps.fit(image, thumb_size, Image.ANTIALIAS)
else:
img = image.copy()
img.thumbnail((width, height), Image.ANTIALIAS)
if bg:
img = self._bg_square(img, bg)
img.save(thumb_filename, image.format, quality=quality)
return thumb_url
except Exception, e:
traceback.print_exc()
print(e.message)
@staticmethod
def _bg_square(img, color=0xff):
size = (max(img.size),) * 2
layer = Image.new('L', size, color)
layer.paste(
img, tuple(map(lambda x: (x[0] - x[1])/2, zip(size, img.size))))
return layer
@staticmethod
def _get_path(full_path):
directory = os.path.dirname(full_path)
try:
if not os.path.exists(full_path):
os.makedirs(directory)
except OSError as e:
if e.errno != errno.EEXIST:
raise
@staticmethod
def _get_name(name, fm, *args):
for v in args:
if v:
name += '_%s' % v
name += fm
return name
你为什么不使用和imagefield? –
您可以轻松调整大小。 –
@appa_lover,因为我需要上传其他文件。 –