Akwbw commited on
Commit
ae8baca
·
verified ·
1 Parent(s): 037fdc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -16
app.py CHANGED
@@ -13,41 +13,50 @@ 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,
42
  colormode='color',
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,7 +67,7 @@ def process_image(image_file):
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,7 +85,6 @@ 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
 
 
13
  # 1. Image Read
14
  img = Image.open(image_file)
15
 
16
+ # 2. Shutterstock Size Logic
17
  width, height = img.size
18
  total_pixels = width * height
19
+ target_pixels = 4100000
20
 
 
21
  if total_pixels < target_pixels:
22
  scale_ratio = (target_pixels / total_pixels) ** 0.5
23
  new_width = int(width * scale_ratio)
24
  new_height = int(height * scale_ratio)
 
25
  img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
26
  print(f"Upscaled to: {new_width}x{new_height}")
27
 
28
+ # --- FIX IS HERE (Path Change) ---
29
+ # Hum /tmp use nahi kar rahe, direct root folder use kar rahe hain
30
+ # takay 'File Not Found' ka rona khatam ho.
31
+ input_path = "temp_input.png"
32
+ svg_path = "temp_vector.svg"
33
+ eps_path = "output_shutterstock.eps"
34
 
35
+ # Pehle purani files delete karo taake mix na ho
36
+ if os.path.exists(input_path): os.remove(input_path)
37
+ if os.path.exists(svg_path): os.remove(svg_path)
38
+ if os.path.exists(eps_path): os.remove(eps_path)
39
+
40
+ # Image Save (Force Save)
41
  img.save(input_path)
42
+
43
+ # Verify karo ke file save hui ya nahi
44
+ if not os.path.exists(input_path):
45
+ raise Exception("File save hi nahi hui Malik! Permissions ka masla hai.")
46
 
47
+ # 3. Vector Conversion
 
48
  convert_image_to_svg_py(
49
+ input_path, # Ab ye path 100% exist karta hai
50
  svg_path,
51
  colormode='color',
52
  hierarchical='stacked',
53
  mode='spline',
54
  filter_speckle=10,
55
+ color_precision=6,
56
+ layer_difference=20,
57
  corner_threshold=60,
58
  length_threshold=10,
59
+ max_iterations=8,
60
  splice_threshold=45,
61
  path_precision=3
62
  )
 
67
 
68
  @app.route('/')
69
  def home():
70
+ return "😈 Devil API is Running (Path Fixed)!"
71
 
72
  @app.route('/convert', methods=['POST'])
73
  def convert():
 
85
  download_name='shutterstock_vector.eps'
86
  )
87
  except Exception as e:
 
88
  print(f"ERROR: {str(e)}")
89
  return jsonify({"error": str(e)}), 500
90