0

Context

I have made a simple web app for uploading content to a blog. The front sends AJAX requests (using FormData) to the backend which is Bottle running on Python 3.7. Text content is saved to a MySQL database and images are saved to a folder on the server. Everything works fine.

Image processing and PIL/Pillow

Now, I want to enable processing of uploaded images to standardise them (I need them all resized and/or cropped to 700x400px).

I was hoping to use Pillow for this. My problem is creating a PIL Image object from the file object in Bottle. I cannot initialise a valid Image object.

Code

# AJAX sends request to this route
@post('/update')
def update():
    # Form data
    title = request.forms.get("title")
    body = request.forms.get("body")
    image = request.forms.get("image")
    author = request.forms.get("author")
    
    # Image upload
    file = request.files.get("file")
    if file:
        extension = file.filename.split(".")[-1]
        if extension not in ('png', 'jpg', 'jpeg'):
            return {"result" : 0, "message": "File Format Error"}
        save_path = "my/save/path"
        file.save(save_path)

The problem

This all works as expected, but I cannot create a valid Image object with pillow for processing. I even tried reloading the saved image using the save path but this did not work either.

Other attempts

The code below did not work. It caused an internal server error, though I am having trouble setting up more detailed Python debugging.

path = save_path + "/" + file.filename
image_data = open(path, "rb")
image = Image.open(image_data)

When logged manually, the path is a valid relative URL ("../domain-folder/images") and I have checked that I am definitely importing PIL (Pillow) correctly using PIL.PILLOW_VERSION.

I tried adapting this answer:

image = Image.frombytes('RGBA', (128,128), image_data, 'raw')

However, I won’t know the size until I have created the Image object. I also tried using io:

image = Image.open(io.BytesIO(image_data))

This did not work either. In each case, it is only the line trying to initialise the Image object that causes problems.

Summary

The Bottle documentation says the uploaded file is a file-like object, but I am not having much success in creating an Image object that I can process.

How should I go about this? I do not have a preference about processing before or after saving. I am comfortable with the processing, it is initialising the Image object that is causing the problem.


Edit - Solution

I got this to work by adapting the answer from eatmeimadanish. I had to use a io.BytesIO object to save the file from Bottle, then load it with Pillow from there. After processing, it could be saved in the usual way.

obj = io.BytesIO()
file.save(obj) # This saves the file retrieved by Bottle to the BytesIO object
path = save_path + "/" + file.filename

# Image processing
im = Image.open(obj) # Reopen the object with PIL
im = im.resize((700,400))
im.save(path, optimize=True)

I found this from the Pillow documentation about a different function that may also be of use.

PIL.Image.frombuffer(mode, size, data, decoder_name='raw', *args)

Note that this function decodes pixel data only, not entire images. If you have an entire image file in a string, wrap it in a BytesIO object, and use open() to load it.

Community
  • 1
  • 1
Chris
  • 4,009
  • 3
  • 21
  • 52

2 Answers2

1

From what I understand, you're trying to resize the image after it has been saved locally (note that you could try to do the resize before it is saved). If this is what you want to achieve here, you can open the image directly using Pillow, it does the job for you (you do not have to open(path, "rb"):

image = Image.open(path)
image.resize((700,400)).save(path)
0asa
  • 224
  • 1
  • 8
  • Thanks for the answer. I’m afraid I still get an error here. My app runs on a sub domain of the blog, but the server folders are all under my name. The path is to a sibling folder in the main domain. I assume this isn’t a problem though, as saving works ok. I have turned on Bottle friendly error messages but only getting internal server error returned. I’ll see if I can find error messages. – Chris Aug 07 '19 at 16:05
1

Use StringIO instead.

From PIL import Image
try:
    import cStringIO as StringIO
except ImportError:
    import StringIO

s = StringIO.StringIO()
#save your in memory file to this instead of a regular file
file = request.files.get("file")
if file:
    extension = file.filename.split(".")[-1]
    if extension not in ('png', 'jpg', 'jpeg'):
        return {"result" : 0, "message": "File Format Error"}
    file.save(s)
im = Image.open(s)
im.resize((700,400))
im.save(s, 'png', optimize=True)
s64 = base64.b64encode(s.getvalue())
eatmeimadanish
  • 3,809
  • 1
  • 14
  • 20
  • Thanks for your answer. It led me on a journey. I’m on Python 3.7 so I had to `import io` instead. `io.StringIO` did not work (it caused an error), but using `io.BytesIO` in this way does not raise errors. The last base-64 line causes an error, but up to that point no errors. Assuming this saves the resized image back to `s`, how do I then save `s` to disk? – Chris Aug 07 '19 at 19:16
  • I got it working based on adapting your answer - thanks. See my edit. I will accept your answer as it got me there. The trick was loading the Bottle file upload into a BytesIO object first. – Chris Aug 07 '19 at 22:26