2023-06-29 19:15:13 +00:00
|
|
|
import fastapi
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
from io import BytesIO
|
2023-06-29 19:20:12 +00:00
|
|
|
|
|
|
|
from fastapi.responses import PlainTextResponse
|
2023-06-29 19:15:13 +00:00
|
|
|
app = fastapi.FastAPI()
|
|
|
|
|
|
|
|
img = Image.open("./smoking-caterpillar.jpg")
|
|
|
|
font = ImageFont.truetype("./unicode.impact.ttf", 72)
|
|
|
|
|
|
|
|
class JPEGResponse(fastapi.Response):
|
|
|
|
media_type="image/jpeg"
|
|
|
|
|
2023-06-29 19:20:12 +00:00
|
|
|
@app.get("/", response_class=PlainTextResponse)
|
|
|
|
async def root():
|
2023-06-30 12:45:32 +00:00
|
|
|
return "go to /img for the funny thingy"
|
2023-06-29 19:20:12 +00:00
|
|
|
|
2023-06-29 19:15:13 +00:00
|
|
|
@app.get("/img", response_class=JPEGResponse)
|
|
|
|
async def getImage(request: fastapi.Request):
|
|
|
|
img2 = img.copy()
|
|
|
|
draw = ImageDraw.Draw(img2)
|
2023-06-30 13:00:42 +00:00
|
|
|
draw.text((img2.width/2,img2.height/2), request.headers["x-forwarded-for"], (255,255,255), font=font, stroke_width=5, stroke_fill=(0,0,0), anchor="mm")
|
2023-06-29 19:15:13 +00:00
|
|
|
img_bin = BytesIO()
|
|
|
|
img2.save(img_bin, format="jpeg")
|
|
|
|
return fastapi.Response(content=img_bin.getvalue(), media_type="image/jpeg")
|