File size: 2,721 Bytes
6d461c8 f8745b1 6d461c8 ae8baca 6d461c8 ae8baca 6d461c8 ae8baca 6d461c8 ae8baca 6d461c8 ae8baca 6d461c8 ae8baca 6d461c8 ae8baca 6d461c8 f8745b1 ae8baca 6d461c8 ae8baca 6d461c8 f8745b1 ae8baca f8745b1 6d461c8 3f9fd9e 6d461c8 f8745b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
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):
# 1. Image Read
img = Image.open(image_file)
# 2. Shutterstock Size Logic
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}")
# --- FIX IS HERE (Path Change) ---
# Hum /tmp use nahi kar rahe, direct root folder use kar rahe hain
# takay 'File Not Found' ka rona khatam ho.
input_path = "temp_input.png"
svg_path = "temp_vector.svg"
eps_path = "output_shutterstock.eps"
# Pehle purani files delete karo taake mix na ho
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)
# Image Save (Force Save)
img.save(input_path)
# Verify karo ke file save hui ya nahi
if not os.path.exists(input_path):
raise Exception("File save hi nahi hui Malik! Permissions ka masla hai.")
# 3. Vector Conversion
convert_image_to_svg_py(
input_path, # Ab ye path 100% exist karta hai
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
)
# 4. EPS Conversion
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) |