-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_image.py
189 lines (166 loc) · 5.62 KB
/
file_image.py
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from os import path,mkdir,fdopen
from shutil import copyfile
from random import randint
from tempfile import mkdtemp, mkstemp
import Image
import ImageDraw
def mkTempFile(content = None):
(fd, source_file) = mkstemp()
if not content is None:
fh = fdopen(fd, 'w')
fh.write(content)
fh.close()
return source_file
def mkImageWithFrame(x, y, width=1, frame_color=None, bg_color=None):
if frame_color is None:
frame_color = (0, 0, 0)
if bg_color is None:
bg_color = (0xff, 0xff, 0xff)
im = Image.new("RGB", (x, y), bg_color)
d = ImageDraw.Draw(im)
d.rectangle([(0, 0), (x, y)], fill=frame_color)
out_file = mkTempFile()
im.save(out_file, 'JPEG')
return ImageInfo(out_file)
class FileImageException(Exception):
pass
class FileProcess:
base_dir = path.realpath(path.curdir)
base_url = ''
img_subdir = None
@staticmethod
def fullPath(file_path):
return path.join(FileProcess.base_dir, str(file_path))
@staticmethod
def fullUrl(file_path):
return FileProcess.base_url + '/' + file_path
@staticmethod
def imageInfo(image_path):
ii = ImageInfo(FileProcess.fullPath(image_path))
if not ii.is_image():
return ii
ii.short_path = image_path
return ii
def __init__(self):
self.dir_max = 99
self.file_max = 999999999
def copyFile(self, source_file, copy_func = None, short_dir = None, ext = None):
if not path.isfile(source_file):
raise FileImageException('Cant find source file')
if ext is None:
(_, ext) = path.splitext(source_file)
while True:
p = []
if short_dir:
p.extend(short_dir.split(path.sep))
p.append(str(randint(1, self.dir_max)))
for pp in [p[0:v] for v in range(1,len(p)+1)]:
rand_dir = self.fullPath(path.join(*pp))
if not path.isdir(rand_dir):
mkdir(rand_dir)
p.append(str(randint(1, self.file_max)) + str(ext))
target_file = path.join(*p)
full_target_path = self.fullPath(target_file)
if not path.isfile(full_target_path):
break;
if copy_func is None:
copy_func = copyfile
copy_func(source_file, full_target_path)
return target_file
def copyImage(self, source_file, transform = None, short_dir = None):
source_info = ImageInfo(source_file)
if not source_info.is_image():
return None
if transform is None:
copy_func = None
else:
copy_func = lambda source, target: transform.process(source, target)
p = [v for v in [self.img_subdir, short_dir] if v]
if p:
short_dir = path.join(*p)
#print source_info.content_type
target_file = self.copyFile(source_file, copy_func = copy_func, short_dir = short_dir,\
ext = source_info.file_ext)
return FileProcess.imageInfo(target_file)
class ImageTransform:
STD = 1
@staticmethod
def create(transform_type, width = None, height = None):
types = {
ImageTransform.STD: ImageTransformStd
}
if transform_type in types:
it = types[transform_type]
return it(width, height)
else:
raise FileImageException('Cant find this type')
def __init__(self, width = None, height = None):
self.width = width
self.height = height
def process(self, source_file, target_file):
raise FileImageException('Cant process empty for this transfer type')
class ImageTransformStd(ImageTransform):
def process(self, source_file, target_file):
try:
i = Image.open(source_file)
(i_width, i_height) = i.size
i_ratio = float(i_width) / float(i_height)
#print i_ratio, i_width, i_height
ratio = float(self.width) / float(self.height)
if i_ratio < ratio:
h = self.height
w = int(self.height * i_ratio)
else:
w = self.width
h = int(float(self.width) / float(i_ratio))
i.resize((w, h)).save(target_file)
except IOError:
return False
return True
class ImageInfo:
JPEG = 1
GIF = 2
PNG = 3
file_exts = {
JPEG: '.jpg',
GIF: '.gif',
PNG: '.png'
}
pil_formats = {
'JPEG': JPEG,
'GIF': GIF,
'PNG': PNG
}
def __init__(self, file_path):
self.file_path = file_path
self.width = 0
self.height = 0
self.content_type = None
self.initInfo()
if not path.isfile(self.file_path):
return
def initInfo(self):
try:
im = Image.open(self.file_path)
self.content_type = self.pil_formats[im.format]
except Exception as e:
#print str(e) + "\nfor file " + self.file_path
self.content_type = None
return
(self.width, self.height) = im.size
@property
def file_ext(self):
try:
return self.file_exts[self.content_type]
except NameError:
raise FileImageException('Cant find file extension for this content type')
except KeyError as e:
<<<<<<< HEAD
raise e
=======
raise FileImageException("Cant find content_type \'%s\'" % self.content_type)
>>>>>>> ee07473ed0fd62f60355c74e6d5014b7e5665c5d
def __bool__(self):
return self.is_image()
def is_image(self):
return (not self.content_type is None)