gpt4 book ai didi

python - 如何将图像(ndarray对象)转换为图像对象以便可以JSON序列化?

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:55 25 4
gpt4 key购买 nike

我是Python和编程世界的新手。我有一个将图像转换为 numpy 数组的代码。我想学习如何反转它,即将 numpy 数组转换为图像。

我有一个rest api代码,它从post方法获取图像,将其转换为numpy数组,进行一些处理并返回一些结果。但是,我正在尝试修改代码,以便我可以将两个图像作为 post 方法的输入,将其转换为 numpy 数组,将这些图像合并为一个并将最终图像作为 json 响应发送。

我已经成功修改了代码,因此它接受两个图像作为输入。稍后我将添加将两个图像合并为一个的代码。目前,我正在尝试将图像作为 json 响应发送。为此,我只是想按原样发送从 post 方法获得的图像。但我收到一个错误“‘ndarray’类型的对象不可 JSON 序列化”。所以,我想,我应该将 ndarray 对象(之前创建的)转换回图像,以便它可以被 json 序列化。如何做到这一点?

# import the necessary packages
from django.views.decorators.csrf import csrf_exempt
from django.http import JsonResponse
import numpy as np
import urllib.request
import json
import cv2
import os


@csrf_exempt
def detect(request):
# initialize the data dictionary to be returned by the request
data = {"success":False}

# check to see if this is a post request
if request.method == "POST":
# check to see if an image was uploaded
if request.FILES.get("image1", None) and request.FILES.get("image2", None) is not None:
# grab the uploaded image
image1 = _grab_image1(stream=request.FILES["image1"])
image2 = _grab_image2(stream=request.FILES["image2"])

# otherwise, assume that a URL was passed in
else:
# grab the URL from the request
url = request.POST.get("url", None)

# if the URL is None, then return an error
if url is None:
data["error"] = "No URL provided."
return JsonResponse(data)

# load the image and convert
image1 = _grab_image1(url=url)
image2 = _grab_image2(url=url)

# Code for combining two image

data.update({"final1": image1,"final2": image2, "success": True})

# return a JSON response
return JsonResponse(data)

def _grab_image1(path=None, stream=None, url=None):
# if the path is not None, then load the image from disk
if path is not None:
image1 = cv2.imread(path) #loads the image


# otherwise, the image does not reside on disk
else:
# if the URL is not None, then download the image
if url is not None:
resp = urllib.request.urlopen(url)
data = resp.read()

# if the stream is not None, then the image has been uploaded
elif stream is not None:
data = stream.read()

# convert the image to a NumPy array and then read it into
# OpenCV format
image1 = np.asarray(bytearray(data), dtype="uint8")
image1 = cv2.imdecode(image1, cv2.IMREAD_COLOR)

# return the image
return image1

def _grab_image2(path=None, stream=None, url=None):
# if the path is not None, then load the image from disk
if path is not None:
image2 = cv2.imread(path) #loads the image


# otherwise, the image does not reside on disk
else:
# if the URL is not None, then download the image
if url is not None:
resp = urllib.request.urlopen(url)
data = resp.read()

# if the stream is not None, then the image has been uploaded
elif stream is not None:
data = stream.read()

# convert the image to a NumPy array and then read it into
# OpenCV format
image2 = np.asarray(bytearray(data), dtype="uint8")
image2 = cv2.imdecode(image2, cv2.IMREAD_COLOR)

# return the image
return image2

转换图像(ndarray对象),以便它可以被json序列化。

最佳答案

我认为不可能以这种方式做你想做的事情......也许你可以尝试两件事:

  1. 将其存储在服务器中的某个位置并序列化其 URL。
  2. 对图像进行编码,将编码后的图像放入 JSON 中并稍后进行解码。你可以尝试使用base64 python 库。

如果选择第二个选项,只需像这样对 ndarray 进行编码:

coded_image = base64.b64encode(image)

解码:

decoded_image = base64.decodestring(coded_image)

关于python - 如何将图像(ndarray对象)转换为图像对象以便可以JSON序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53923601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com