Akwbw commited on
Commit
6d461c8
·
verified ·
1 Parent(s): 3ee71b5

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from flask import Flask, request, send_file, jsonify
3
+ from flask_cors import CORS
4
+ from vtracer import convert_image_to_svg_py
5
+ import cairosvg
6
+ from PIL import Image
7
+ import io
8
+
9
+ app = Flask(__name__)
10
+ CORS(app) # HTML App se connect karne ke liye zaroori
11
+
12
+ def process_image(image_file):
13
+ # 1. Image Read
14
+ img = Image.open(image_file)
15
+
16
+ # 2. Shutterstock Logic (Smart Resize)
17
+ # Hamein Total Pixels > 4 Million chahiye (e.g. 2000x2000 = 4MP)
18
+ width, height = img.size
19
+ total_pixels = width * height
20
+ target_pixels = 4500000 # 4.5 MP (Safety margin)
21
+
22
+ if total_pixels < target_pixels:
23
+ # Calculate scaling factor
24
+ scale_ratio = (target_pixels / total_pixels) ** 0.5
25
+ new_width = int(width * scale_ratio)
26
+ new_height = int(height * scale_ratio)
27
+
28
+ # High Quality Resize (LANCZOS)
29
+ img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
30
+ print(f"Upscaled to: {new_width}x{new_height}")
31
+
32
+ # Save temp png
33
+ input_path = "/tmp/temp_input.png"
34
+ svg_path = "/tmp/temp_vector.svg"
35
+ eps_path = "/tmp/output_shutterstock.eps"
36
+
37
+ img.save(input_path)
38
+
39
+ # 3. Vector Conversion (Cleanest Settings)
40
+ convert_image_to_svg_py(
41
+ input_path,
42
+ svg_path,
43
+ colormode='color',
44
+ hierarchical='stacked',
45
+ mode='spline', # Curves not pixels
46
+ filter_speckle=10, # Choti dots remove
47
+ color_precision=8,
48
+ layer_difference=12,
49
+ corner_threshold=60,
50
+ length_threshold=10,
51
+ max_iterations=10,
52
+ splice_threshold=45,
53
+ path_precision=3
54
+ )
55
+
56
+ # 4. EPS Conversion
57
+ cairosvg.svg2eps(url=svg_path, write_to=eps_path)
58
+
59
+ return eps_path
60
+
61
+ @app.route('/convert', methods=['POST'])
62
+ def convert():
63
+ if 'image' not in request.files:
64
+ return jsonify({"error": "No image uploaded"}), 400
65
+
66
+ try:
67
+ file = request.files['image']
68
+ eps_file_path = process_image(file)
69
+
70
+ # File wapis bhejo
71
+ return send_file(
72
+ eps_file_path,
73
+ mimetype='application/postscript',
74
+ as_attachment=True,
75
+ download_name='shutterstock_vector.eps'
76
+ )
77
+ except Exception as e:
78
+ return jsonify({"error": str(e)}), 500
79
+
80
+ @app.route('/')
81
+ def home():
82
+ return "😈 Devil Vector API is Running inside Docker!"
83
+
84
+ if __name__ == '__main__':
85
+ app.run(host='0.0.0.0', port=5000)