Serverless 实现图片压缩与水印是他的第三季
Posted hsoyoi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Serverless 实现图片压缩与水印是他的第三季相关的知识,希望对你有一定的参考价值。
个image对象和宽度传入,通过resize方法进行大小的调整,实现压缩功能。
def compressImage(image, width):
height = image.size[1] / (image.size[0] / width)
return image.resize((int(width), int(height)))
图像水印
图像水印部分采用的是文字水印,除了文字水印还可以考虑使用图片水印等。
此处为了将水印放在图像的右下角,并且恰好不超出图像范围,进行了每个字符大小的获取:
height = []
width = []
for eveStr in watermarkStr:
thisWidth, thisHeight = drawImage.textsize(eveStr, font)
height.append(thisHeight)
width.append(thisWidth)
通过这样处理之后,得到的height列表就是所有即将水印文字的高度,width列表是所有即将水印文字的宽度。此处要将水印放在右下角只需要在图片整体高度上减去height列表最大值,图片整体宽度基础上减去width列表的总和即可:
def watermarImage(image, watermarkStr):
txtImage = Image.new(‘RGBA‘, image.size, (0, 0, 0, 0))
font = ImageFont.truetype("Brimborion.TTF", 40)
drawImage = ImageDraw.Draw(txtImage)
height = []
width = []
for eveStr in watermarkStr:
thisWidth, thisHeight = drawImage.textsize(eveStr, font)
height.append(thisHeight)
width.append(thisWidth)
drawImage.text((txtImage.size[0] - sum(width) - 10, txtImage.size[1] - max(height) - 10),
watermarkStr, font=font,
fill=(255, 255, 255, 255))
return Image.alpha_composite(image, txtImage)
部署到云函数
通过函数的事件描述,可以确定腾讯云函数的对象存储触发器事件结果为:
{
"Records": [
{
"cos": {
"cosSchemaVersion": "1.0",
"cosObject": {,
"meta": {
"x-cos-request-id": "NWMxOWY4MGFfMjViMjU4NjRfMTUyMV8yNzhhZjM=",
"Content-Type": ""
},
"vid": "",
"key": "/1253970026/testpic/testfile",
"size": 1029
},
"cosBucket": {
"region": "cd",
"name": "testpic",
"appid": "1253970026"
},
"cosNotificationId": "unkown"
},
"event": {
"eventName": "cos: ObjectCreated:Post",
"eventVersion": "1.0",
"eventTime": 1545205770,
"eventSource": "qcs::cos",
"requestParameters": {
"requestSourceIP": "192.168.15.101",
"requestHeaders": {
"Authorization": "q-sign-algorithm=sha1&q-ak=AKIDQm6iUh2NJ6jL41tVUis9KpY5Rgv49zyC&q-sign-time=1545205709;1545215769&q-key-time=1545205709;1545215769&q-header-list=host;x-cos-storage-class&q-url-param-list=&q-signature=098ac7dfe9cf21116f946c4b4c29001c2b449b14"
}
},
"eventQueue": "qcs:0:lambda??appid/1253970026:default.printevent.$LATEST",
"reservedInfo": "",
"reqid": 179398952
}
}]
}
根据这个结构,我们可以确定出相关详细信息,例如存储桶/APPID以及图片的Key等。将上面的代码按照函数计算的格式进行改写:
-- coding: utf8 --
import os
from PIL import Image, ImageFont, ImageDraw
from qcloud_cos_v5 import CosConfig
from qcloud_cos_v5 import CosS3Client
secret_id = os.environ.get(‘secret_id‘)
secret_key = os.environ.get(‘secret_key‘)
region = os.environ.get(‘region‘)
cosClient = CosS3Client(CosConfig(Region=region, SecretId=secret_id, SecretKey=secret_key))
def compressImage(image, width):
height = image.size[1] / (image.size[0] / width)
return image.resize((int(width), int(height)))
def watermarImage(image, watermarkStr):
txtImage = Image.new(‘RGBA‘, image.size, (0, 0, 0, 0))
font = ImageFont.truetype("Brimborion.TTF", 40)
drawImage = ImageDraw.Draw(txtImage)
height = []
width = []
for eveStr in watermarkStr:
thisWidth, thisHeight = drawImage.textsize(eveStr, font)
height.append(thisHeight)
width.append(thisWidth)
drawImage.text((txtImage.size[0] - sum(width) - 10, txtImage.size[1] - max(height) - 10),
watermarkStr, font=font,
fill=(255, 255, 255, 255))
return Image.alpha_composite(image, txtImage)
def main_handler(event, context):
for record in event[‘Records‘]:
bucket = record[‘cos‘][‘cosBucket‘][‘name‘] + ‘-‘ + record[‘cos‘][‘cosBucket‘][‘appid‘]
key = "/".join(record[‘cos‘][‘cosObject‘][‘key‘].split("/")[3:])
download_path = ‘/tmp/{}‘.format(key.split(‘/‘)[-1])
download_path = ‘/tmp/{}‘.format(key.split(‘/‘)[-1])
upload_path = ‘/tmp/new_mp4-{}‘.format(key.split(‘/‘)[-1])
以上是关于Serverless 实现图片压缩与水印是他的第三季的主要内容,如果未能解决你的问题,请参考以下文章