Created
June 14, 2025 01:54
-
-
Save Akkiesoft/aa89b9bdeafa7f5597d4aeb286bfd4a1 to your computer and use it in GitHub Desktop.
Copilotに作らせた、任意の画像をリサイズしてBMPとして返すAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# API to resize and convert to bmp image | |
# 99% Copilot made | |
from PIL import Image | |
import sys | |
import os | |
import requests | |
from flask import Flask, request, send_file, jsonify | |
from io import BytesIO | |
app = Flask(__name__) | |
@app.route('/convert', methods=['POST']) | |
def convert(): | |
data = request.get_json() | |
url = data.get('url') | |
print(url) | |
if not url: | |
return jsonify({'error': 'No URL provided'}), 400 | |
try: | |
response = requests.get(url) | |
response.raise_for_status() | |
img_bytes = BytesIO(response.content) | |
with Image.open(img_bytes) as img: | |
output_bytes = BytesIO() | |
w, h = img.size | |
# Resize while maintaining aspect ratio: max height 64px, max width 320px | |
scale = min(64 / h, 320 / w, 1) | |
new_width = int(w * scale) | |
new_height = int(h * scale) | |
resized = img.resize((new_width, new_height)) | |
resized.save(output_bytes, format='BMP') | |
output_bytes.seek(0) | |
return send_file(output_bytes, mimetype='image/bmp', as_attachment=False, download_name='converted.bmp') | |
except Exception as e: | |
return jsonify({'error': str(e)}), 500 | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=3039, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment