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.

Domingo
  • 119
  • 2
  • 7
  • You haven't said what the actual problem is. – Daniel Roseman Nov 26 '16 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)? – Ilya V. Schurov Nov 26 '16 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. – Domingo Nov 26 '16 at 18:26

1 Answers1

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.

Leonora Tindall
  • 1,391
  • 2
  • 12
  • 30
  • 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. – Domingo Nov 26 '16 at 18:25
  • 1
    You need to make sure that the Apache user has read/write access to the directories you're trying to use. – Leonora Tindall Nov 26 '16 at 18:26