1

I have an input for images upload in my page. When you click on "send image" the post request is received in my back-end.

I don't, and I don't want to, save the image.

What I do, from the back, is: I send the image to an API which will return me the image's tags and then I will display the tags and the image itself, that has been uploaded, in my html page.

if request.method == "POST":
            form = ImageForm(request.POST, request.FILES)

            if form.is_valid():
                imageUploaded = request.FILES['image_file']
                try:
                    c = Client(cId, sId)
                    c.get_token()
                    tags = c.image_lookup(imageUploaded)

                    urlImage = base64.b64encode(imageUploaded.read())
                    context.update({
                        'image_path': urlImage,
                        'tags': tags.json,
                        'btn_visible': True,
                    })
                except ValueError as e:
                    logging.info(e)
                    context.update({
                        'btn_visible': False,
                        'error_message': 'A problem occured, we will fix it as soon as possible. We apologise for the inconvenience.'
                    })

in my HTML:

<img id="cv-image" src="data:image/png;base64,{{ image_path }}">

But, my problem is that my image_path is desperately empty.

What's the problem?

EDIT: It's super weird, if I comment the code calling the Client class, which do a GET and a POST on an API, it will works. I still don't get it nor how to make it works.

4
  • Did you pass the context to a render method on a template? Commented Aug 26, 2016 at 14:29
  • Yes, everything works fine except that encoding. Commented Aug 26, 2016 at 14:37
  • If you throw in a print imageUploaded.name right before the line where you do the b64 encoding, does the output match what you'd expect or is it blank too? Commented Aug 26, 2016 at 15:06
  • Yes, I have the name displayed: 12788292_10207620162414638_1498066895_n.jpg ! Commented Aug 26, 2016 at 15:10

2 Answers 2

1

I'm probably too late to the party here, but I just stumbled across this and it sounds to me like an issue with the file's seek position. If image_lookup() reads the file (or causes the file to be read), it may leave the seek position at the end of the file. Subsequent reads of the file (from that position) therefore return nothing.

This makes sense given that commenting out the image_lookup() call, and making a copy of the file instance prior to making the call, both worked. If it is indeed the case, seeking the file back to the start is all you need to do, which is as simple as: file.seek(0).

This is untested, but should be all you need:

c = Client(cId, sId)
c.get_token()
tags = c.image_lookup(imageUploaded)

# Seek file back to the beginning
imageUploaded.seek(0)

urlImage = base64.b64encode(imageUploaded.read())
Sign up to request clarification or add additional context in comments.

Comments

0

Ok, as I'm not saving the image, once I send it to the API the object in itself isn't available anymore.

Not sure to understand why I still can print it, I had to make a copy of it:

if form.is_valid():
            imageUploaded = request.FILES['image_file']
            imageCopy = copy.deepcopy(imageUploaded)
            try:
                c = Client(cId, sId)
                c.get_token()
                tags = c.image_lookup(imageUploaded)

                urlImage = base64.b64encode(imageCopy.read())

And now it works perfectly fine!

As asked, here is the code of Image_lookup:

def image_lookup(self, imageUploaded, image_type=None):
        '''POST /v1/imageLookup'''
        param_files = [
            ('input_image', ('image.jpg', imageUploaded, image_type or 'image/png'))
        ]

        return self.post('someAPIurl/imageLookup', files=param_files)

3 Comments

Sounds really strange. Would you mind to check the docs for Client.image_lookup() method?
I also want to see the docs for that method. I'm really curious as to what's going on here.
Sorry for the late answer. Done :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.