在Django中将numpy数组显示为图像

在Django中将numpy数组显示为图像

本文介绍了在Django中将numpy数组显示为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django框架的新手.我正在建立一个网站,从用户那里获取图像,然后处理该图像并返回到一个numpy数组(处理后的图像).我想将numpy数组显示为图像.我怎样才能做到这一点?谢谢您的阅读和帮助?

I am new to Django framework. I am building a website that take an image from user, then process the image and return to a numpy array (processed image). I want to show the numpy array as image. How can I do that? Thank you for reading and helps?

index.html

<form name="image" method = "post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Upload</button>
</form>

索引视图

def index(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            model = MyDeepLearningModel.get_instance()
            file_name = request.FILES['file']

            processed_image = model.run_png(file_name) #processed_image is an numpy array

            #how to show the processed_image in index.html?
            return render(request, 'lowlighten/index.html')
    else:
        form = UploadFileForm()
    return render(request, 'lowlighten/index.html', {'form': form})

推荐答案

好吧,我们首先达成共识:要在前端显示图片,您需要为该图片提供一个url,该图片应该存在于已知的位置,因此前端可以从中加载它.

Ok, let's first agree on something: to show an image in the frontend, you need to have a url to that image, this image should exist somewhere known so the frontend could load it from.

因此,我假设您不愿意将此图像保存在任何地方(例如imgur或类似内容),因此最好的办法是从该图像中创建数据uri.

So I'm assuming you are not willing to save this image anywhere -like imgur or something-, so the best thing to do is to make a data uri out of this image.

首先,我们需要将您的numpy数组转换为图像:

First we need to convert your numpy array into an image:

from PIL import Image

def to_image(numpy_img):
    img = Image.fromarray(data, 'RGB')
    return img

然后要从此图像中获取uri,我们需要对其进行进一步处理:

Then to get a uri out of this image we need to further process it:

import base64
from io import BytesIO
def to_data_uri(pil_img):
    data = BytesIO()
    img.save(data, "JPEG") # pick your format
    data64 = base64.b64encode(data.getvalue())
    return u'data:img/jpeg;base64,'+data64.decode('utf-8')

现在您已将图像编码为数据uri,您可以将该数据uri传递到前端并在img标签中使用

Now you have your image encoded as a data uri, you can pass that data uri to the frontend and use it in an img tag

<img src={{ image_uri }} />

基于此,我们可以如下更改您的功能:

Based on that we could change your function as follows:

def index(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            model = MyDeepLearningModel.get_instance()
            file_name = request.FILES['file']

            processed_image = model.run_png(file_name) #processed_image is an numpy array
            pil_image = to_image(processed_image)
            image_uri = to_data_uri(pil_image)

            #how to show the processed_image in index.html?
            return render(request, 'lowlighten/index.html', {'image_uri': image_uri})
    else:
        form = UploadFileForm()
    return render(request, 'lowlighten/index.html', {'form': form})

这篇关于在Django中将numpy数组显示为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:44