pillow 读写保存图片

1.安装pillow

pip install pillow

 

2.读写以及等比例缩放保存图片

img = Image.open("/tmp/123/000000165.jpg")
img_w, img_h = img.size

# 宽度、高度最大值 > 2500等比例缩放
if max(img_h, img_w) > 2500:
    if img_h > img_w:
        img_w = int(2000 / img_h * img_w)
        img_h = 2000
    else:
        img_h = int(2000 / img_w * img_h)
        img_w = 2000

    img = img.resize((img_w, img_h), Image.ANTIALIAS)
    img.save("/tmp/12322222.jpg",'JPEG')

    stream = BytesIO()  # 创建1个IO对象
    img.save(stream, 'JPEG')  # 把图片保存到内存
    data = stream.getvalue()

    photo_base64 = base64.b64encode(data)
    print(photo_base64)

 

posted on 2021-11-10 14:59  星河赵  阅读(536)  评论(0编辑  收藏  举报

导航