Question

I am currently trying to update an excel-map with data fetched from Django and display it on a page linked to my admin.

The excel-"map" :

enter image description here

Using the xlrd & cie libs I'm working on the fetching / updating data part but a fellow dev told me he would have used an image instead.

My Question is : How could I do such a thing ? Displaying a copy of the above spreadsheet and using row-based coordinates to write the data I want ? (Text data)

Do you guys know a way to work easily with coordinate-based text display in Django's templates ?

Thanks for any help you can give me !

Was it helpful?

Solution

I ended up using the PIL library. It was finally quite easy and could be summarized by the following code :

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

im = Image.open('path/to/map.png')
font = ImageFont.truetype('/usr/share/fonts/dejavu/DejaVuSans.ttf', 7)
draw = ImageDraw.Draw(im)

draw.text((x, y), sentence, font=font, fill='black')

response = HttpResponse(mimetype='image/png')
im.save(response, 'PNG')

return response

Logic behind x and y is irrelevant here, but bear in mind that they start quite illogically on the upper left corner of the image and go from there to right and bottom with positive numbers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top