16
16
17
17
import os
18
18
import json
19
+ from os .path import dirname
19
20
import re
20
21
import logging
21
22
import filecmp
@@ -127,6 +128,7 @@ def fix_borders(assets_dict, script_config, border_colors, destination_dir, dry_
127
128
128
129
# Extracting necessary parameters from the script config
129
130
border_width = script_config ['border_width' ]
131
+ exclusion_list = script_config ['exclusion_list' ]
130
132
rgb_border_colors = []
131
133
132
134
# Convert border colors to RGB format if available
@@ -198,7 +200,7 @@ def fix_borders(assets_dict, script_config, border_colors, destination_dir, dry_
198
200
if rgb_border_color :
199
201
results = replace_border (input_file , output_path , rgb_border_color , border_width , logger )
200
202
else :
201
- results = remove_border (input_file , output_path , border_width , logger )
203
+ results = remove_border (input_file , output_path , border_width , exclusion_list , logger )
202
204
if results :
203
205
if path :
204
206
messages .append (f"{ action } { data ['title' ]} { year } - { file_name } " )
@@ -277,34 +279,75 @@ def replace_border(input_file, output_path, border_colors, border_width, logger)
277
279
logger .error (f"Error processing { input_file } " )
278
280
return False
279
281
280
- def remove_border (input_file , output_path , border_width , logger ):
282
+ def remove_border (input_file , output_path , border_width , exclusion_list , logger ):
281
283
"""
282
284
Crops the center of an image, reducing its dimensions by 50 pixels on each side.
283
285
284
286
Args:
285
287
input_file (str): The input file.
286
288
output_path (str): The output path.
287
289
border_width (int): The border width.
290
+ exclusion_list (list): Light posters to exclude from having a black border on the bottom
288
291
Returns:
289
292
bool: True if the file was saved, False otherwise.
290
293
"""
291
294
295
+ def format_season (string ):
296
+ return re .sub (r'Season0*(\d+)' , r'Season \1' , string )
297
+
298
+ if exclusion_list is None :
299
+ exclusion_list = []
300
+
292
301
# Open the image
293
302
try :
294
303
with Image .open (input_file ) as image : # Open the image
295
304
# Set the border width
296
305
width , height = image .size # Get the width and height of the image
297
-
298
306
# Remove top, left, and right borders, and replace bottom border with black
299
- final_image = image .crop ((border_width , border_width , width - border_width , height )) # Crop the image to remove the borders
300
- bottom_border = Image .new ("RGB" , (width - 2 * border_width , border_width ), color = 'black' ) # Create a black image for the bottom border
301
- bottom_border_position = (0 , height - border_width - border_width ) # Position the bottom border 25 pixels from the bottom
302
- final_image .paste (bottom_border , bottom_border_position ) # Paste the black bottom border at the specified position
307
+ file_name = os .path .basename (input_file )
308
+ name_without_extension , extension = os .path .splitext (file_name )
309
+ # Format for no asset folders
310
+ parts = name_without_extension .split ('_' )
311
+ # Check if list is greater > 2 indicating season file, if not return None
312
+ if len (parts )>= 2 :
313
+ name_without_season , season = parts
314
+ else :
315
+ name_without_season = parts [0 ]
316
+ season = None
317
+ if season is not None :
318
+ formatted_season = format_season (season )
319
+ # Create variable to match exclusion_list format
320
+ formatted_name_without_season = f"{ name_without_season } - { formatted_season } "
321
+ else :
322
+ formatted_name_without_season = None
323
+
324
+ # Format for asset folders
325
+ directory_path = os .path .dirname (input_file )
326
+ parent_dir_name = os .path .basename (directory_path )
327
+ formatted_name_without_extension = format_season (name_without_extension )
328
+ # Create variable to match exclusion_list format
329
+ folder_season_file_name = f"{ parent_dir_name } - { formatted_name_without_extension } "
330
+
331
+ # Check asset folders for exclusion list match
332
+ if folder_season_file_name in exclusion_list and "Season" in name_without_extension : # season file name
333
+ final_image = image .crop ((border_width , border_width , width - border_width , height - border_width ))
334
+ elif parent_dir_name in exclusion_list and "Season" not in name_without_extension : # poster/series file name
335
+ final_image = image .crop ((border_width , border_width , width - border_width , height - border_width ))
336
+
337
+ # Check formatted file name against exclusion list if asset folders is false
338
+ elif name_without_extension in exclusion_list : # poster/series file name
339
+ final_image = image .crop ((border_width , border_width , width - border_width , height - border_width ))
340
+ elif formatted_name_without_season in exclusion_list : # season file name
341
+ final_image = image .crop ((border_width , border_width , width - border_width , height - border_width ))
342
+ # Not an exclusion
343
+ else :
344
+ final_image = image .crop ((border_width , border_width , width - border_width , height )) # Crop the image to remove the borders
345
+ bottom_border = Image .new ("RGB" , (width - 2 * border_width , border_width ), color = 'black' ) # Create a black image for the bottom border
346
+ bottom_border_position = (0 , height - border_width - border_width ) # Position the bottom border 25 pixels from the bottom
347
+ final_image .paste (bottom_border , bottom_border_position ) # Paste the black bottom border at the specified position
303
348
304
349
# Resize the image to 1500x1000
305
350
final_image = final_image .resize ((1000 , 1500 )).convert ("RGB" )
306
-
307
- file_name = os .path .basename (input_file )
308
351
final_path = f"{ output_path } /{ file_name } " # Set the output path to the parent directory
309
352
310
353
if os .path .isfile (final_path ):
@@ -546,4 +589,4 @@ def main(config):
546
589
logger .error (f"\n \n An error occurred:\n " , exc_info = True )
547
590
logger .error (f"\n \n " )
548
591
finally :
549
- logger .info (create_bar (f"END { name } " ))
592
+ logger .info (create_bar (f"END { name } " ))
0 commit comments