Update app.py
Browse files
app.py
CHANGED
|
@@ -7,33 +7,35 @@ from PIL import Image
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
-
# CORS allow kar rahe hain taake kisi bhi HTML site se request aa sake
|
| 11 |
CORS(app)
|
| 12 |
|
| 13 |
def process_image(image_file):
|
| 14 |
# 1. Image Read
|
| 15 |
img = Image.open(image_file)
|
| 16 |
|
| 17 |
-
# 2.
|
| 18 |
width, height = img.size
|
| 19 |
total_pixels = width * height
|
| 20 |
-
target_pixels =
|
| 21 |
|
|
|
|
| 22 |
if total_pixels < target_pixels:
|
| 23 |
scale_ratio = (target_pixels / total_pixels) ** 0.5
|
| 24 |
new_width = int(width * scale_ratio)
|
| 25 |
new_height = int(height * scale_ratio)
|
|
|
|
| 26 |
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 27 |
print(f"Upscaled to: {new_width}x{new_height}")
|
| 28 |
|
| 29 |
-
# Temp
|
| 30 |
input_path = "/tmp/temp_input.png"
|
| 31 |
svg_path = "/tmp/temp_vector.svg"
|
| 32 |
eps_path = "/tmp/output_shutterstock.eps"
|
| 33 |
|
| 34 |
img.save(input_path)
|
| 35 |
|
| 36 |
-
# 3. Vector Conversion (
|
|
|
|
| 37 |
convert_image_to_svg_py(
|
| 38 |
input_path,
|
| 39 |
svg_path,
|
|
@@ -41,11 +43,11 @@ def process_image(image_file):
|
|
| 41 |
hierarchical='stacked',
|
| 42 |
mode='spline',
|
| 43 |
filter_speckle=10,
|
| 44 |
-
color_precision=
|
| 45 |
-
layer_difference=
|
| 46 |
corner_threshold=60,
|
| 47 |
length_threshold=10,
|
| 48 |
-
max_iterations=
|
| 49 |
splice_threshold=45,
|
| 50 |
path_precision=3
|
| 51 |
)
|
|
@@ -56,7 +58,7 @@ def process_image(image_file):
|
|
| 56 |
|
| 57 |
@app.route('/')
|
| 58 |
def home():
|
| 59 |
-
return "😈 Devil API is Running
|
| 60 |
|
| 61 |
@app.route('/convert', methods=['POST'])
|
| 62 |
def convert():
|
|
@@ -74,8 +76,9 @@ def convert():
|
|
| 74 |
download_name='shutterstock_vector.eps'
|
| 75 |
)
|
| 76 |
except Exception as e:
|
|
|
|
|
|
|
| 77 |
return jsonify({"error": str(e)}), 500
|
| 78 |
|
| 79 |
if __name__ == '__main__':
|
| 80 |
-
# Local testing ke liye, lekin Docker mein Gunicorn handle karega
|
| 81 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 7 |
import io
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
|
|
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
def process_image(image_file):
|
| 13 |
# 1. Image Read
|
| 14 |
img = Image.open(image_file)
|
| 15 |
|
| 16 |
+
# 2. Shutterstock Size Logic (4MP Target)
|
| 17 |
width, height = img.size
|
| 18 |
total_pixels = width * height
|
| 19 |
+
target_pixels = 4100000 # 4.1 MP (Safe limit)
|
| 20 |
|
| 21 |
+
# Sirf tab resize karega agar image choti hai
|
| 22 |
if total_pixels < target_pixels:
|
| 23 |
scale_ratio = (target_pixels / total_pixels) ** 0.5
|
| 24 |
new_width = int(width * scale_ratio)
|
| 25 |
new_height = int(height * scale_ratio)
|
| 26 |
+
# LANCZOS for sharpness
|
| 27 |
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
| 28 |
print(f"Upscaled to: {new_width}x{new_height}")
|
| 29 |
|
| 30 |
+
# Temp Paths
|
| 31 |
input_path = "/tmp/temp_input.png"
|
| 32 |
svg_path = "/tmp/temp_vector.svg"
|
| 33 |
eps_path = "/tmp/output_shutterstock.eps"
|
| 34 |
|
| 35 |
img.save(input_path)
|
| 36 |
|
| 37 |
+
# 3. Vector Conversion (OPTIMIZED SETTINGS FOR FREE SERVER)
|
| 38 |
+
# RAM bachanay ke liye precision thora adjust kiya hai
|
| 39 |
convert_image_to_svg_py(
|
| 40 |
input_path,
|
| 41 |
svg_path,
|
|
|
|
| 43 |
hierarchical='stacked',
|
| 44 |
mode='spline',
|
| 45 |
filter_speckle=10,
|
| 46 |
+
color_precision=6, # Reduced from 8 to 6 (Saves RAM)
|
| 47 |
+
layer_difference=20, # Increased from 16 to 20 (Faster processing)
|
| 48 |
corner_threshold=60,
|
| 49 |
length_threshold=10,
|
| 50 |
+
max_iterations=8, # Reduced from 10 to 8 (Prevents Timeout)
|
| 51 |
splice_threshold=45,
|
| 52 |
path_precision=3
|
| 53 |
)
|
|
|
|
| 58 |
|
| 59 |
@app.route('/')
|
| 60 |
def home():
|
| 61 |
+
return "😈 Devil API is Running (Optimized Mode)!"
|
| 62 |
|
| 63 |
@app.route('/convert', methods=['POST'])
|
| 64 |
def convert():
|
|
|
|
| 76 |
download_name='shutterstock_vector.eps'
|
| 77 |
)
|
| 78 |
except Exception as e:
|
| 79 |
+
# Error print karega taake logs mein dikhe
|
| 80 |
+
print(f"ERROR: {str(e)}")
|
| 81 |
return jsonify({"error": str(e)}), 500
|
| 82 |
|
| 83 |
if __name__ == '__main__':
|
|
|
|
| 84 |
app.run(host='0.0.0.0', port=7860)
|