tsbpp commited on
Commit
f812b4c
1 Parent(s): 6ce386d

Upload load.py

Browse files
Files changed (1) hide show
  1. load.py +13 -11
load.py CHANGED
@@ -15,14 +15,16 @@ def download_image_with_retries(image_url, output_file):
15
  print(f"Download failed: {e}")
16
  return False
17
 
18
- def convert_gif_to_jpg(input_path, output_path):
19
  try:
20
  with Image.open(input_path) as img:
21
- img.convert('RGB').save(output_path, 'JPEG')
22
- os.remove(input_path) # Remove the original GIF after conversion
 
 
23
  return True
24
  except Exception as e:
25
- print(f"Error converting GIF to JPG: {e}")
26
  return False
27
 
28
  def verify_and_download_images(data):
@@ -32,19 +34,19 @@ def verify_and_download_images(data):
32
  for key, value in data.items():
33
  image_url = value['imageURL']
34
  ext = os.path.splitext(image_url)[1].lower() # Ensure extension is in lowercase for comparison
35
- is_gif = ext == '.gif'
36
- output_ext = '.jpg' if is_gif else ext
37
  output_file = f'{images_directory}/{key}{output_ext}'
38
 
39
  if not os.path.exists(output_file):
40
  print(f"Image {key}{output_ext} not found, attempting to download...")
41
- temp_output_file = output_file if not is_gif else f'{images_directory}/{key}{ext}'
42
  if not download_image_with_retries(image_url, temp_output_file):
43
  print(f"Warning: Could not download image {image_url}")
44
- elif is_gif:
45
- # Convert GIF to JPG
46
- if not convert_gif_to_jpg(temp_output_file, output_file):
47
- print(f"Warning: Could not convert GIF to JPG for image {image_url}")
48
 
49
  # Load the dataset JSON file (assuming you have a dataset.json file in the same directory)
50
  with open('dataset.json', 'r') as fp:
 
15
  print(f"Download failed: {e}")
16
  return False
17
 
18
+ def convert_to_jpg(input_path, output_path):
19
  try:
20
  with Image.open(input_path) as img:
21
+ # Convert image to RGB to ensure compatibility if original is in a different mode (e.g., RGBA, P)
22
+ rgb_img = img.convert('RGB')
23
+ rgb_img.save(output_path, 'JPEG')
24
+ os.remove(input_path) # Remove the original image file after conversion
25
  return True
26
  except Exception as e:
27
+ print(f"Error converting image to JPG: {e}")
28
  return False
29
 
30
  def verify_and_download_images(data):
 
34
  for key, value in data.items():
35
  image_url = value['imageURL']
36
  ext = os.path.splitext(image_url)[1].lower() # Ensure extension is in lowercase for comparison
37
+ needs_conversion = ext in ['.gif', '.png']
38
+ output_ext = '.jpg' if needs_conversion else ext
39
  output_file = f'{images_directory}/{key}{output_ext}'
40
 
41
  if not os.path.exists(output_file):
42
  print(f"Image {key}{output_ext} not found, attempting to download...")
43
+ temp_output_file = output_file if not needs_conversion else f'{images_directory}/{key}{ext}'
44
  if not download_image_with_retries(image_url, temp_output_file):
45
  print(f"Warning: Could not download image {image_url}")
46
+ elif needs_conversion:
47
+ # Convert image to JPG
48
+ if not convert_to_jpg(temp_output_file, output_file):
49
+ print(f"Warning: Could not convert image to JPG for image {image_url}")
50
 
51
  # Load the dataset JSON file (assuming you have a dataset.json file in the same directory)
52
  with open('dataset.json', 'r') as fp: