I have flask api response picture:
FORMAT = {'image/jpeg':'JPEG', 'image/bmp':'BMP', 'image/png':'PNG', 'image/gif': 'GIF'}
@app.route('/api/image/<id>/<str_size>', methods=['get'])
def show_thumbnail(id, str_size):
size = int(str_size)
with get_db().cursor() as cur:
cur.callproc('getimage', (id,))
result = cur.fetchone()
buf = BytesIO(result[1])
if(size>0):
im = Image.open(buf)
im.thumbnail((size, size))
buf = BytesIO(b'')
im.save(buf, format=FORMAT[result[0].lower()])
fw = open('w03.jpg', 'wb')
fw.write(buf.getbuffer())
fw.close()
resp = Response(buf)
resp.headers.set('Content-Type', result[0].lower())
return resp
ps:
result[0] = 'image/jpeg'
result[1] is the bytes array of jpeg picture.
If I set the size(str_size) = 0, I mean I do not run PIL Image thumbnail code part. I can get the correct picture in response.
If I set the size(str_size) = 256 for instance, I find the 'w03.jpg' is correct and I can get the correct resize image, but the response is black for the reason is the image contains error.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…