add image thumb handle
This commit is contained in:
parent
487087c4c3
commit
d9f1864900
35
ahserver/imgThumb.py
Normal file
35
ahserver/imgThumb.py
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
from PIL import Image
|
||||||
|
from io import BytesIO
|
||||||
|
|
||||||
|
def imageThumb(imgfilepath,width=None,height=None):
|
||||||
|
im = Image.open(imgfilepath)
|
||||||
|
mode = im.mode
|
||||||
|
if mode not in ('L', 'RGB'):
|
||||||
|
if mode == 'RGBA':
|
||||||
|
alpha = im.split()[3]
|
||||||
|
bgmask = alpha.point(lambda x: 255-x)
|
||||||
|
im = im.convert('RGB')
|
||||||
|
# paste(color, box, mask)
|
||||||
|
im.paste((255,255,255), None, bgmask)
|
||||||
|
else:
|
||||||
|
im = im.convert('RGB')
|
||||||
|
|
||||||
|
w, h = im.size
|
||||||
|
if not width and not height:
|
||||||
|
width = 256
|
||||||
|
if width:
|
||||||
|
height = int(float(width) * float(h) / float(w))
|
||||||
|
else:
|
||||||
|
width = int(float(height) * float(w) / float(h))
|
||||||
|
thumb = im.resize((width,height),Image.ANTIALIAS)
|
||||||
|
f = BytesIO()
|
||||||
|
thumb.save(f,format='jpeg',quality=60)
|
||||||
|
im.close()
|
||||||
|
v = f.getvalue()
|
||||||
|
with open('thumb.jpg','wb') as x:
|
||||||
|
x.write(v)
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
imageThumb("/home/ymq/media/pictures/2019-08/IMG_20190804_113014.jpg", width=256)
|
Loading…
Reference in New Issue
Block a user