Python 通过curl向Flask提交图像失败,返回“0”;图像数据不足';
我正在运行一个Flask webapp,它接受Python 通过curl向Flask提交图像失败,返回“0”;图像数据不足';,python,curl,flask,png,python-imaging-library,Python,Curl,Flask,Png,Python Imaging Library,我正在运行一个Flask webapp,它接受/predict路径上的POST请求。调用应用程序时,执行以下代码将消息字节转换回PNG图像: from flask import request from PIL import Image img = Image.frombytes('RGBA', (512,512), request.data, 'raw') 我正在通过以下curl命令向该API提交一个有效的512 x 512 PNG图像: curl -i -X POST http://127
/predict
路径上的POST
请求。调用应用程序时,执行以下代码将消息字节转换回PNG图像:
from flask import request
from PIL import Image
img = Image.frombytes('RGBA', (512,512), request.data, 'raw')
我正在通过以下curl
命令向该API提交一个有效的512 x 512 PNG图像:
curl -i -X POST http://127.0.0.1:5000/predict\
-H "Content-Type: application/octet-stream"\
--data-binary "@/Users/alex/Desktop/test_image_512.png"
请求始终失败,数据如下:
[...]
File "/Users/alex/Desktop/bob-ross-neural-painter/app/app.py", line 24, in predict
Image.frombytes('RGBA', (512,512), request.data, 'raw')
File "/Users/alex/miniconda3/envs/spell-dev/lib/python3.7/site-packages/PIL/Image.py", line 2542, in frombytes
im.frombytes(data, decoder_name, args)
File "/Users/alex/miniconda3/envs/spell-dev/lib/python3.7/site-packages/PIL/Image.py", line 829, in frombytes
raise ValueError("not enough image data")
ValueError: not enough image data
我已经验证了PNG图像()在本地是有效的512x512 PNG图像:np.array(image.open('/Users/alex/Desktop/test\u image\u 512.PNG'))。shape
返回(512,512,4)
我似乎无法确定问题的根本原因。图像是否在传输过程中被损坏
以下是完整的路线定义供参考:
@app.route('/predict', methods=['POST'])
def predict():
# bytes -> PIL image -> array -> tensor
img = torch.tensor(
np.array(
Image.frombytes('RGBA', (512,512), request.data, 'raw')
)
)
pillow
Image.fromarray
方法需要一个raw bytemap作为输入,并且在给定编码像素字节(例如本例中的PNG编码像素)时将不起作用。处理此情况的正确方法是使用BytesIO
和Image。请改为打开
:
以open('test_image_512.png','rb')作为f的:
img_b=f.read()
图像。打开(BytesIO(img_b))
枕头图像。fromarray
方法需要一个原始字节映射作为输入,并且在给定编码像素字节(例如本例中的PNG编码像素)时将不起作用。处理此情况的正确方法是使用BytesIO
和Image。请改为打开
:
以open('test_image_512.png','rb')作为f的:
img_b=f.read()
图像。打开(BytesIO(img_b))
请参阅。请参阅。