Render image without saving

后端 未结 3 692
梦谈多话
梦谈多话 2021-01-22 20:44

I want to get an image from user, work with it at beckend and render result back to user. Is it possible to do without saving image on disk?

my view:

cla         


        
相关标签:
3条回答
  • 2021-01-22 21:11

    This answer is inspired by @Daniel Roseman answer

    write the image to BytesIO()

    from PIL import Image  
    img = Image.open("image.jpg") 
    data = io.BytesIO()
    img.save(data, "JPEG") # just an example, load the image into BytesIO any way you like.
    

    encode img into base64

    encoded_img = base64.b64encode(data.getvalue())
    

    decode img in context, as well as setting content_type

    decoded_img = encoded_img.decode('utf-8')
    img_data = f"data:image/jpeg;base64,{decoded_img}"
    

    render your template

    return render(request, "index.html", img_data=img_data)
    

    in your HTML template

    <img src="{{ img_data }}" >

    you can check also https://buraksenol.medium.com/pass-images-to-html-without-saving-them-as-files-using-python-flask-b055f29908a

    0 讨论(0)
  • 2021-01-22 21:16

    The problem is that returning a page to the user would normally involve two requests: one for the HTML page, and one for the image itself as referenced by an img tag in that HTML. But as you say, without storing it anywhere in the meantime, the image would be lost.

    There is an alternative: you can use a data uri to include the actual data for the image inline in the html. That means you won't have to persist it across requests, as everything will be returned at one. Data uris are supported in most browsers, including ie8+.

    You can format the data like this, for example:

    from base64 import b64encode
    
    encoded = b64encode(img_data)
    mime = "image/jpeg"
    uri = "data:%s;base64,%s" % (mime, encoded)
    

    And use it directly in the template:

    <img src="{{ uri }}">
    
    0 讨论(0)
  • 2021-01-22 21:37

    If your form is Model Form .then jus try the below code.

    from django.core.files import File
    from django.core.files.temp import NamedTemporaryFile
    
    class InputImageForm(forms.Form):
        image = forms.ImageField()
        class Meta:
           model = YourModel
    
    
    
    def get_image(request):
        if request.method == 'POST':
            form = InputImageForm(request.POST, request.FILES)
            if form.is_valid():
                image = request.FILES['image']
                save_image_from_url(form,image)
    
            else:
               form = InputImageForm()
        else:
            raise Http404
    
    
    
    def save_image_from_url(model, img):
        r = request.FILES['image']
    
        img_temp = NamedTemporaryFile(delete=True)
        img_temp.write(r.content)
        img_temp.flush()
    
        i = model.image.save("image.jpg", File(img_temp), save=True)
        return HttpResponse(i.read(), mimetype="YOUR MIME TYPE")
    
    0 讨论(0)
提交回复
热议问题