|
|
import os |
|
|
from flask import Flask, request, send_file, jsonify |
|
|
from flask_cors import CORS |
|
|
from vtracer import convert_image_to_svg_py |
|
|
import cairosvg |
|
|
from PIL import Image |
|
|
import io |
|
|
|
|
|
app = Flask(__name__) |
|
|
CORS(app) |
|
|
|
|
|
def process_image(image_file): |
|
|
|
|
|
img = Image.open(image_file) |
|
|
|
|
|
|
|
|
width, height = img.size |
|
|
total_pixels = width * height |
|
|
target_pixels = 4100000 |
|
|
|
|
|
if total_pixels < target_pixels: |
|
|
scale_ratio = (target_pixels / total_pixels) ** 0.5 |
|
|
new_width = int(width * scale_ratio) |
|
|
new_height = int(height * scale_ratio) |
|
|
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS) |
|
|
print(f"Upscaled to: {new_width}x{new_height}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
input_path = "temp_input.png" |
|
|
svg_path = "temp_vector.svg" |
|
|
eps_path = "output_shutterstock.eps" |
|
|
|
|
|
|
|
|
if os.path.exists(input_path): os.remove(input_path) |
|
|
if os.path.exists(svg_path): os.remove(svg_path) |
|
|
if os.path.exists(eps_path): os.remove(eps_path) |
|
|
|
|
|
|
|
|
img.save(input_path) |
|
|
|
|
|
|
|
|
if not os.path.exists(input_path): |
|
|
raise Exception("File save hi nahi hui Malik! Permissions ka masla hai.") |
|
|
|
|
|
|
|
|
convert_image_to_svg_py( |
|
|
input_path, |
|
|
svg_path, |
|
|
colormode='color', |
|
|
hierarchical='stacked', |
|
|
mode='spline', |
|
|
filter_speckle=10, |
|
|
color_precision=6, |
|
|
layer_difference=20, |
|
|
corner_threshold=60, |
|
|
length_threshold=10, |
|
|
max_iterations=8, |
|
|
splice_threshold=45, |
|
|
path_precision=3 |
|
|
) |
|
|
|
|
|
|
|
|
cairosvg.svg2eps(url=svg_path, write_to=eps_path) |
|
|
return eps_path |
|
|
|
|
|
@app.route('/') |
|
|
def home(): |
|
|
return "😈 Devil API is Running (Path Fixed)!" |
|
|
|
|
|
@app.route('/convert', methods=['POST']) |
|
|
def convert(): |
|
|
if 'image' not in request.files: |
|
|
return jsonify({"error": "No image uploaded"}), 400 |
|
|
|
|
|
try: |
|
|
file = request.files['image'] |
|
|
eps_file_path = process_image(file) |
|
|
|
|
|
return send_file( |
|
|
eps_file_path, |
|
|
mimetype='application/postscript', |
|
|
as_attachment=True, |
|
|
download_name='shutterstock_vector.eps' |
|
|
) |
|
|
except Exception as e: |
|
|
print(f"ERROR: {str(e)}") |
|
|
return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(host='0.0.0.0', port=7860) |