aboutsummaryrefslogtreecommitdiffstats
path: root/97suifangqa/apps/profile/image.py
blob: b5dd55012b49287b9b8c7d63cb3f9d6ccda285af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- coding: utf-8 -*-
"""
	图片处理函数
"""
import os
import time

from hashlib import md5
from django.conf import settings
from os import mkdir, path as _path

try:
	from PIL import Image, ImageOps, ImageDraw
except:
	import Image, ImageOps, ImageDraw


def rm_avatar_thumb(object_id, avatar_link):
	name, ext = _path.splitext(avatar_link)
	try:
		fn = generat_location(object_id, ext, 200)
		os.remove(fn)
	except:
		pass
	

def _mkdir(dirname):
	"""
		遍历创建文件夹
	"""
	index = []
	initial = 0
	for i in range(dirname.count('/')):
		initial = dirname.find('/', initial, len(dirname))
		index.insert(len(index), initial)
		initial += 1
	for j in index[1:]:
		_dir = dirname[:j]
		if _dir and not _path.exists(_dir):
			mkdir(_dir)


def generat_dir_and_fn(uid, flag):
	root = settings.MEDIA_ROOT
	fn = md5('%s' % uid).hexdigest()
	d1 = fn[-2:]
	d2 = fn[-4:-2]
	dirname = "%s%s/%s/%s/" % (root, flag, d1, d2)
	return dirname, fn


def generat_location(uid, ext, size, flag):
	"""
		获取文件路径
	"""
	fmt = "%s%s_%s%s"
	dirname, fn = generat_dir_and_fn(uid, flag)
	_mkdir(dirname)
	if not ext or \
		ext == '.':
		ext = '.jpg'
	return fmt % (dirname, fn, size, ext)


def crop(image, object_id, size=100, mask_img=None, flag="cache", **kwargs):
	"""
		截取图片
	"""
	_size = (size, size)
	path = image.path
	root, filename = _path.split(path)
	name, ext = _path.splitext(filename)

	original = Image.open(open(path, 'rb'))

	# 保存为圆形缩略图
	"""
	mask = Image.new('L', _size, 0)
	draw = ImageDraw.Draw(mask) 
	draw.ellipse((0, 0) + _size, fill=255)
	thumb = ImageOps.fit(original, mask.size, centering=(0.5, 0.5))
	thumb.putalpha(mask)
	"""
	try:
		if mask_img:
			mask = Image.open(mask_img).convert('L')
			thumb = ImageOps.fit(original, _size, centering=(0.5, 0.5))
			thumb.putalpha(mask)
		else: thumb = ImageOps.fit(original, _size, Image.ANTIALIAS)
		
		thumb_path = generat_location(object_id, ext, size, flag)
		if not _path.exists(thumb_path):
			thumb.save(thumb_path)
	except:
		thumb_path = path
		
	return thumb_path.replace(settings.MEDIA_ROOT, settings.MEDIA_URL)