animepfp / server.py
Reaperxxxx's picture
Update server.py
7685678 verified
Raw
History Blame Contribute Delete
5.06 kB
import os
import json
import random
import string
from flask import Flask, request, send_file
from PIL import Image, ImageDraw, ImageFont
import time
app = Flask(__name__)
# Ensure output directory exists
OUTPUT_DIR = "output"
os.makedirs(OUTPUT_DIR, exist_ok=True)
# Load assets
BG_IMAGE_PATH = "background.png" # Background for the image
FONT_PATH = "arial.ttf" # Adjust the font path if needed
# Load character ID mapping
with open("map.json", "r") as f:
character_map = json.load(f)
# Helper function to generate a random string for the file name
def generate_random_filename():
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=10)) + ".png"
# Clean up old files (older than 1 hour) in the output directory
def clear_old_files():
current_time = time.time()
for filename in os.listdir(OUTPUT_DIR):
file_path = os.path.join(OUTPUT_DIR, filename)
if os.path.getmtime(file_path) < current_time - 3600: # 1 hour
os.remove(file_path)
@app.route("/api", methods=["GET"])
def generate_image():
# Extract parameters from request
chat_id = request.args.get("chat_id", "default")
name = request.args.get("name", "Reiker")
balance = request.args.get("balance", "5000")
equipped_character_id = int(request.args.get("equipped_character", 1)) # Default to ID 1 if not provided
# Get the character name using the ID from map.json
equipped_character = character_map.get(str(equipped_character_id), "Unknown Character")
# Load background image
bg_image = Image.open(BG_IMAGE_PATH).convert("RGBA")
# Create a drawing object
draw = ImageDraw.Draw(bg_image)
# Load fonts
font_large = ImageFont.truetype(FONT_PATH, bg_image.width // 15) # Larger font for name
font_medium = ImageFont.truetype(FONT_PATH, bg_image.width // 18) # Slightly smaller for balance and character
# Text properties
text_color = (255, 255, 255) # White text color
margin_top = bg_image.height // 15 # Margin for top text
# Draw Name ("Reiker") - Aligned to the left
name_size = draw.textbbox((0, 0), name, font=font_large)
name_x = 20 # Set to 20 to align left
name_y = margin_top
# Draw Balance ("Balance: 5000") - Aligned to the left
balance_text = f"Balance: {balance}"
balance_size = draw.textbbox((0, 0), balance_text, font=font_medium)
balance_x = 20 # Set to 20 to align left
balance_y = name_y + (name_size[3] - name_size[1]) + 20 # Small gap below name
# Draw Equipped Character ("Gojo") - Aligned to the left
equipped_text = f"Equipped Character: {equipped_character}"
equipped_size = draw.textbbox((0, 0), equipped_text, font=font_medium)
equipped_x = 20 # Set to 20 to align left
equipped_y = balance_y + (balance_size[3] - balance_size[1]) + 20 # Small gap below balance
# Draw text
draw.text((name_x, name_y), name, font=font_large, fill=text_color)
draw.text((balance_x, balance_y), balance_text, font=font_medium, fill=text_color)
draw.text((equipped_x, equipped_y), equipped_text, font=font_medium, fill=text_color)
# Overlay equipped character image (based on ID, use "1.png" for ID 1, etc.)
char_image_path = f"{equipped_character_id}.png"
if os.path.exists(char_image_path):
char_image = Image.open(char_image_path).convert("RGBA")
else:
# Use a placeholder image if the character image doesn't exist
char_image = Image.open("placeholder.png").convert("RGBA")
# Character image size adjustment
char_width = bg_image.width // 2 # Character image will take up half the width of the background
char_image.thumbnail((char_width, char_width), Image.LANCZOS)
# Character image position (centered horizontally and placed below the text)
char_x = (bg_image.width - char_image.width) // 2
char_y = equipped_y + (equipped_size[3] - equipped_size[1]) + 40 # Small gap below equipped text, further down
# Paste the character image with transparency (alpha channel)
bg_image.paste(char_image, (char_x, char_y), char_image)
# Save the image with a random filename
random_filename = generate_random_filename()
output_path = os.path.join(OUTPUT_DIR, random_filename)
bg_image.save(output_path)
# Optionally clear old files (older than 1 hour)
clear_old_files()
return send_file(output_path, mimetype="image/png")
@app.route("/img", methods=["GET"])
def get_image():
# Extract the image ID from the query parameter 'q'
image_id = request.args.get("q")
if image_id:
# Build the image path using the provided image ID, directly in the current directory
image_path = os.path.join(os.getcwd(), f"{image_id}.png")
if os.path.exists(image_path):
return send_file(image_path, mimetype="image/png")
else:
return "Image not found", 404
else:
return "Image ID not provided", 400
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=True)