Tonic commited on
Commit
d12d5cb
·
1 Parent(s): 0e370ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -4
app.py CHANGED
@@ -28,12 +28,19 @@ import os
28
  from PIL import Image, UnidentifiedImageError
29
  import secrets
30
 
 
31
  def save_image(image_file) -> str:
32
- upload_dir = "uploaded_images"
33
- print("Creating upload directory if it doesn't exist.")
34
  os.makedirs(upload_dir, exist_ok=True)
35
  try:
36
- print(f"Attempting to open the image file: {image_file.name}")
 
 
 
 
 
 
37
  image = Image.open(image_file).convert("RGB")
38
  file_name = secrets.token_hex(10) + ".png"
39
  file_path = os.path.join(upload_dir, file_name)
@@ -41,7 +48,6 @@ def save_image(image_file) -> str:
41
  print("Saving the image.")
42
  image.save(file_path, format="PNG")
43
  print("Image saved successfully.")
44
-
45
  return file_path
46
  except UnidentifiedImageError:
47
  print("Error: The file is not a recognizable image.")
 
28
  from PIL import Image, UnidentifiedImageError
29
  import secrets
30
 
31
+
32
  def save_image(image_file) -> str:
33
+ upload_dir = os.path.abspath("uploaded_images")
34
+ print(f"Creating upload directory at {upload_dir} if it doesn't exist.")
35
  os.makedirs(upload_dir, exist_ok=True)
36
  try:
37
+ temp_file_path = image_file.name
38
+ print(f"Attempting to open the image file: {temp_file_path}")
39
+ if os.path.exists(temp_file_path) and os.path.getsize(temp_file_path) > 0:
40
+ print(f"File exists and size is {os.path.getsize(temp_file_path)} bytes")
41
+ else:
42
+ print("File does not exist or is empty.")
43
+ return None
44
  image = Image.open(image_file).convert("RGB")
45
  file_name = secrets.token_hex(10) + ".png"
46
  file_path = os.path.join(upload_dir, file_name)
 
48
  print("Saving the image.")
49
  image.save(file_path, format="PNG")
50
  print("Image saved successfully.")
 
51
  return file_path
52
  except UnidentifiedImageError:
53
  print("Error: The file is not a recognizable image.")