diff --git a/src/cautiousrobot/__main__.py b/src/cautiousrobot/__main__.py index 74a4cb3..098ab52 100644 --- a/src/cautiousrobot/__main__.py +++ b/src/cautiousrobot/__main__.py @@ -173,6 +173,10 @@ def download_images(data, img_dir, log_filepath, error_log_filepath, filename = if os.path.exists(downsample_dir_path + "/" + image_name): # Don't overwrite resized images either continue + + if not os.path.exists(downsample_dir_path): + os.makedirs(downsample_dir_path, exist_ok=False) + downsample_and_save_image( image_dir_path=image_dir_path, image_name=image_name, @@ -180,7 +184,7 @@ def download_images(data, img_dir, log_filepath, error_log_filepath, filename = downsample_size=downsample, log_errors=log_errors, image_index=i, - url=url, + file_path=url, error_log_filepath=error_log_filepath ) diff --git a/src/cautiousrobot/utils.py b/src/cautiousrobot/utils.py index 2fbf02e..5933fe7 100644 --- a/src/cautiousrobot/utils.py +++ b/src/cautiousrobot/utils.py @@ -47,7 +47,7 @@ def process_csv(csv_path, expected_cols): return df -def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, downsample_size, log_errors, image_index, url, error_log_filepath): +def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, downsample_size, log_errors, image_index, file_path, error_log_filepath): """ Downsample an image and save it to the specified directory. @@ -58,14 +58,13 @@ def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, d - downsample_size (int): The new size (both width and height) for the downsampled image. - log_errors (dict): A dictionary to store errors encountered during the downsampling process. - image_index (int): The index of the current image being processed, used for logging. - - url (str): The URL associated with the image, used for logging errors. + - file_path (str): The file path or URL associated with the image, used for logging errors. - error_log_filepath (str): The file path where error logs are stored. Returns: None """ - if not os.path.exists(downsample_dir_path): - os.makedirs(downsample_dir_path, exist_ok=False) + try: img = Image.open(f"{image_dir_path}/{image_name}") @@ -76,7 +75,8 @@ def downsample_and_save_image(image_dir_path, image_name, downsample_dir_path, d log_errors, index=image_index, image="downsized_" + image_name, - url=url, + file_path=file_path, response_code=str(e) ) - update_log(log=log_errors, index=image_index, filepath=error_log_filepath) \ No newline at end of file + update_log(log=log_errors, index=image_index, filepath=error_log_filepath) + \ No newline at end of file diff --git a/tests/test_downsample.py b/tests/test_downsample.py index b7499d8..5496303 100644 --- a/tests/test_downsample.py +++ b/tests/test_downsample.py @@ -14,7 +14,7 @@ def setUp(self): self.downsample_size = 100 self.log_errors = {} # Dictionary to store error logs self.error_log_filepath = "error_log.json" - self.url = "http://example.com/image.jpg" + self.file_path = "file://example.com/image.jpg" def tearDown(self): if os.path.exists(self.image_dir_path): @@ -24,15 +24,13 @@ def tearDown(self): if os.path.exists(self.error_log_filepath): os.remove(self.error_log_filepath) - def mock_log_response_side_effect(self, log_errors, index, image, url, response_code): + def mock_log_response_side_effect(self, log_errors, index, image, file_path, response_code): """Helper function to mimic the behavior of log_response.""" - log_errors[index] = {'image': image, 'url': url, 'response_code': response_code} + log_errors[index] = {'image': image, 'file_path': file_path, 'response_code': response_code} return log_errors - @patch("os.makedirs") - @patch("os.path.exists", return_value=False) @patch("PIL.Image.open") - def test_downsample_and_save_image_success(self, mock_open, mock_exists, mock_makedirs): + def test_downsample_and_save_image_success(self, mock_open): """ Test the successful downsampling and saving of an image. """ mock_image = MagicMock(spec=Image.Image) @@ -49,21 +47,19 @@ def test_downsample_and_save_image_success(self, mock_open, mock_exists, mock_ma self.downsample_size, self.log_errors, 0, # image_index - self.url, + self.file_path, self.error_log_filepath ) - mock_makedirs.assert_called_once_with(self.downsample_dir_path, exist_ok=False) mock_open.assert_called_once_with(f"{self.image_dir_path}/test_image.jpg") mock_image.resize.assert_called_once_with((self.downsample_size, self.downsample_size)) mock_resized_image.save.assert_called_once_with(f"{self.downsample_dir_path}/test_image.jpg") - @patch("os.makedirs") @patch("os.path.exists", return_value=True) @patch("PIL.Image.open", side_effect=FileNotFoundError("File not found")) @patch("cautiousrobot.utils.log_response") @patch("cautiousrobot.utils.update_log") - def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_log_response, mock_open, mock_exists, mock_makedirs): + def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_log_response, mock_open, mock_exists): """ Test the behavior when the image file is not found. """ mock_log_response.side_effect = self.mock_log_response_side_effect @@ -75,17 +71,16 @@ def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_lo self.downsample_size, self.log_errors, 0, # image_index - self.url, + self.file_path, self.error_log_filepath ) - mock_makedirs.assert_not_called() mock_open.assert_called_once_with(f"{self.image_dir_path}/missing_image.jpg") mock_log_response.assert_called_once_with( self.log_errors, index=0, image="downsized_missing_image.jpg", - url=self.url, + file_path=self.file_path, response_code="File not found" ) mock_update_log.assert_called_once_with( @@ -98,12 +93,11 @@ def test_downsample_and_save_image_file_not_found(self, mock_update_log, mock_lo self.assertIn(0, self.log_errors) self.assertEqual(self.log_errors[0]['response_code'], "File not found") - @patch("os.makedirs") @patch("os.path.exists", return_value=False) @patch("PIL.Image.open", side_effect=Exception("Unexpected error")) @patch("cautiousrobot.utils.log_response") @patch("cautiousrobot.utils.update_log") - def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_log_response, mock_open, mock_exists, mock_makedirs): + def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_log_response, mock_open, mock_exists): """ Test the behavior when an unexpected error occurs. """ mock_log_response.side_effect = self.mock_log_response_side_effect @@ -115,17 +109,16 @@ def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_ self.downsample_size, self.log_errors, 1, - self.url, + self.file_path, self.error_log_filepath ) - mock_makedirs.assert_called_once_with(self.downsample_dir_path, exist_ok=False) mock_open.assert_called_once_with(f"{self.image_dir_path}/test_image.jpg") mock_log_response.assert_called_once_with( self.log_errors, index=1, image="downsized_test_image.jpg", - url=self.url, + file_path=self.file_path, response_code="Unexpected error" ) mock_update_log.assert_called_once_with( @@ -138,4 +131,4 @@ def test_downsample_and_save_image_unexpected_error(self, mock_update_log, mock_ self.assertEqual(self.log_errors[1]['response_code'], "Unexpected error") if __name__ == "__main__": - unittest.main() \ No newline at end of file + unittest.main()