def gen_pic(MediaId):
pic = Image.open('media/' + MediaId).convert('LA')
# pic = ImageEnhance.Brightness(pic).enhance(0.35)
portion = pic.size[0]/pic.size[1]
if portion < 1:
#portrait
if portion <= 0.75:
pic_w = 960
pic_h = round(960/portion)
box = (0,round((pic_h-1280)/2),960,round(pic_h/2+640))
if portion > 0.75:
pic_h = 1280
pic_w = round(1280*portion)
box = (round((pic_w-960)/2),0,round(pic_w/2+480),1280)
layer = Image.open('./lomolive/layer_p.png')
elif portion > 1:
#landscape
if portion >= 1.3333:
pic_h = 960
pic_w = round(960*portion)
box = (round((pic_w-1280)/2),0,round(pic_w/2+640),960)
if portion < 1.3333:
pic_w = 1280
pic_h = round(1280/portion)
box = (0,round((pic_h-960)/2),1280,round(pic_h/2+480))
layer = Image.open('./lomolive/layer_l.png')
elif portion == 1:
#square
(pic_w,pic_h) = (960,960)
box = (0,0,960,960)
layer = Image.open('./lomolive/layer_s.png')
pic = pic.resize((pic_w, pic_h))
pic = pic.crop(box)
pic = lomoize(pic, 0.4, 1)
pic = pic.convert('RGB')
pic.paste(layer,(0,0),layer)
pic.save('media/' + MediaId + '.jpg', quality=95)
评论列表
文章目录