2018-06-23

需求:用 Python 实现将网络图片转成 jpg 格式保存到七牛

代码如下:

    res = requests.get(p_img_url)
    im = Image.open(BytesIO(res.content)).convert('RGB')
    imgByteArr = BytesIO()
    im.save(imgByteArr, format='JPEG')
    output = imgByteArr.getvalue()
    # 构建鉴权对象
    q = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
    token = q.upload_token(QINIU_BUCKET, path_name, 3600)
    # 上传到七牛
    ret, info = qiniu.put_data(token, path_name, output)

支持转换的图片格式如下(不区分大小写):
'PCX,ICNS,HDF5,EPS,MSP,XBM,WEBP,BUFR,PPM,SGI,BMP,TGA,ICO,TIFF,JPEG,SPIDER,GIF,GRIB,IM,FITS,PDF,WMF,MPO,PALM,JPEG2000,PNG'

参考链接:

https://blog.csdn.net/wgPython/article/details/80740067
https://github.com/python-pillow/Pillow/issues/3192
https://stackoverflow.com/questions/33101935/convert-pil-image-to-byte-array

你可能感兴趣的:(2018-06-23)