1

I'm trying to intercept an image from an HTML form's input control to convert it into a byte string before processing it on the server side.

How do I intercept the file?

upload_files = self.get_uploads('file')
# Intercept here to do something different than just upload 
blob_info = upload_files[0]

How do I convert it into a byte string that can be converted back to an image later?

I'm using Python and App Engine.

sjakobi
  • 3,546
  • 1
  • 25
  • 43
Matt Norris
  • 8,596
  • 14
  • 59
  • 90
  • 2
    base64? http://docs.python.org/library/base64.html – tMC Jun 03 '11 at 05:00
  • 3
    It's not clear what you're asking - the code you quote is server side, so when you run it the image has already been uploaded. And, an image _is_ a byte string. – Nick Johnson Jun 03 '11 at 05:20
  • you already have an bytestring... you just have to store it in a blob property.. – Abdul Kader Jun 03 '11 at 07:09
  • 1
    http://stackoverflow.com/questions/5211780/store-jpg-gif-png-etc-it-gae-datastore/5217869#5217869 – Abdul Kader Jun 03 '11 at 07:10
  • Thank you all for the comments. I think I have to understand a bit more about images - this is my first foray. To help me get going, if I were to do str(blob_info), would it return the string rep of a base64 string like using str(urlfetch(url).result.content)? I can post this as a separate question if you feel I should. – Matt Norris Jun 03 '11 at 21:32

1 Answers1

0

Assume your upload control is in a form named "image" and you are using Werkzeug's FileStorage:

img_stream = self.form.image.data
mimetype = img_stream.content_type
img_str = img_stream.read().encode('base64').replace('\n', '')

data_uri = 'data:%s;%s,%s' % (mimetype, 'base64', img_str)

Your data_uri now contains the string information you need.

Thank you all for your helpful comments!

Matt Norris
  • 8,596
  • 14
  • 59
  • 90