Kastg commited on
Commit
ae6d72e
·
verified ·
1 Parent(s): 0006475

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -12
app.py CHANGED
@@ -1,7 +1,8 @@
1
  from flask import Flask, request, jsonify, send_from_directory
2
- import shutil
3
  import os
4
  import requests
 
 
5
 
6
  app = Flask(__name__)
7
 
@@ -41,19 +42,16 @@ def upload_image():
41
  if 'image' not in content_type:
42
  return jsonify({"message": "The provided URL does not lead to an image"}), 400
43
 
44
- # Determine the file format
45
- if 'jpeg' in content_type:
46
- file_extension = 'jpg'
47
- elif 'png' in content_type:
48
- file_extension = 'png'
49
- else:
50
- return jsonify({"message": "Unsupported image format"}), 400
51
 
52
  # Save the image
53
- filename = f"uploaded_image.{file_extension}"
54
  image_path = os.path.join(IMAGE_FOLDER, filename)
55
- with open(image_path, 'wb') as f:
56
- f.write(response.content)
57
 
58
  # Update available images dictionary
59
  available_images[filename] = {"url": url, "name": filename}
@@ -62,4 +60,4 @@ def upload_image():
62
 
63
 
64
  if __name__ == "__main__":
65
- app.run(host="0.0.0.0", port=7860, debug=True)
 
1
  from flask import Flask, request, jsonify, send_from_directory
 
2
  import os
3
  import requests
4
+ from PIL import Image
5
+ from io import BytesIO
6
 
7
  app = Flask(__name__)
8
 
 
42
  if 'image' not in content_type:
43
  return jsonify({"message": "The provided URL does not lead to an image"}), 400
44
 
45
+ # Open the image using PIL
46
+ try:
47
+ img = Image.open(BytesIO(response.content))
48
+ except Exception as e:
49
+ return jsonify({"message": f"Error processing image: {str(e)}"}), 400
 
 
50
 
51
  # Save the image
52
+ filename = os.path.basename(url)
53
  image_path = os.path.join(IMAGE_FOLDER, filename)
54
+ img.save(image_path)
 
55
 
56
  # Update available images dictionary
57
  available_images[filename] = {"url": url, "name": filename}
 
60
 
61
 
62
  if __name__ == "__main__":
63
+ app.run(host="0.0.0.0", port=7860, debug=True)