0

I've written a program that generates an image via tkinter.Canvas and saves it. Now I'm updating this project to use FastAPI to let users hit my end-point and download this image.

I'm not trying to display the image on the server, just generate it in memory, save it to storage, then return that image.

Initial proof-of-concept tests work, but I cannot get this to work in Docker because no $DISPLAY is set. Unlike other questions I've seen, I'm not trying to display the image directly, just generate the image, save it, and return a file-path.

This is not a dupe of Tkinter setup on headless server. That question boils down to "how do I not run tkinter while on a server?" I still need tkinter, I just have no intention of displaying it.

I have also seen something about X11, but it seems like I'd need to know the server's environment in order to install X11, and I do not.

Is tkinter really not able to generate an image without $DISPLAY (even if I don't intend to display it)? If so, is there an alternative to tkinter I can use that can generate an image without needing a $DISPLAY? Am I totally misunderstanding a major part of how this works? It seems like I should be able to generate an image and save the bytes without needing all the things required for actual graphical rendering...

  • 1
    You can try using `Pillow.Image` and `Pillow.ImageDraw` instead of `tkinter` to generate image without display. – acw1668 Aug 24 '21 at 04:27
  • 1
    _"I still need tkinter, I just have no intention of displaying it."_ - regardless, tkinter requires a display. Have you tried setting up xvfb on your docker container? – Bryan Oakley Aug 24 '21 at 04:36
  • 1
    _"is there an alternative to tkinter I can use that can generate an image without needing a $DISPLAY?"_ asking for tool recommendations is off-topic here. However, you might want to check out [ImageMagick](https://imagemagick.org/index.php) – Bryan Oakley Aug 24 '21 at 04:40
  • @acw1668 I'm actually already using Pillow.Image to save, I guess I didn't realize I could use it to draw as well. Thanks for the suggestion, I'll look into it! – James Haller Aug 24 '21 at 04:58
  • @BryanOakley I will definitely look into xvfb (and possibly ImageMagick if acw1668's suggestion to use Pillow.Image & .ImageDraw doesn't pan out). Thanks for the suggestions! – James Haller Aug 24 '21 at 04:59

1 Answers1

0

Using Pillow.Image and Pillow.ImageDraw instead of tkinter works for my scenario. (Credit goes to @acw1668)

My project only draws lines, circles, and polygons - all things that ImageDraw can do. The process of switching from one to the other was exceptionally straightforward, and Docker returns the ImageDraw/Image file with no issues!