1

I am trying to deploy an app that writes and reads png images with Flask. Locally I can run my script with no errors, except when I run it on my server.

I deployed using this guide on DigitalOcean. It uses apache, wsgi and virtualenv.

This an example of my code:

from flask import Flask
from flask import send_file
from PIL import Image

app = Flask(__name__)

@app.route("/")
def hello():
    img = Image.new('RGB', (200, 100), (255, 255, 255))
    img.save('output.png')
    return send_file('output.png', mimetype='image/png')

if __name__ == "__main__":
    app.run()

I found out that what is causing the problem is img.save('output.png') I found also that if I try to load a font like: font = ImageFont.truetype("Archive.otf", 60) it doesn't work neither. My .png and .otf files are in the same folder as __init__.py

I guess I am messing up with the configuration or the directrory paths of apache. If I run img.save outside the flask enviroment it works, so it is not a problem with libraries or dependencies.

3
  • You haven't said what the actual problem is. Commented Nov 26, 2016 at 15:10
  • What happens if you provide absolute path to img.save like img.save("/srv/my/server/path/output.png") (of course, the correspoding dir should exists and your Apache have to have enough rights to write there)? Commented Nov 26, 2016 at 15:11
  • I found out the main problem is that apache or python does not have the rights to write and read files. I am trying to figure out how to do that but without success yet. Commented Nov 26, 2016 at 18:26

1 Answer 1

2

You need to use an absolute path, like /var/www/somedir/somefile. This is because Flask under Apache does not give Python a usable working directory. I would suggest making the path configurable.

Sign up to request clarification or add additional context in comments.

2 Comments

I only managed to save the files in /tmp, I think I need to give apache/python the rights to read and write files, but I am having trouble for that too.
You need to make sure that the Apache user has read/write access to the directories you're trying to use.

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.