-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete-Small-Images.py
More file actions
63 lines (52 loc) · 2.66 KB
/
Delete-Small-Images.py
File metadata and controls
63 lines (52 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import sys
from PIL import Image
from logger_config import setup_custom_logger
from shared_methods import log_error, log_info
logger = setup_custom_logger('Delete-Small-Images')
def delete_small_jpeg_files(directory):
"""
Recursively deletes JPEG files in the specified directory and its subdirectories that are smaller than 100 KB
or have a width or height smaller than 500 pixels. Excludes any directories starting with '@'.
Args:
directory (str): The path to the root directory containing JPEG files.
"""
# Define the size limit below which files will be deleted (in bytes)
size_limit = 100 * 1024 # 100 KB
# Define the pixel limit below which files will be deleted
pixel_limit = 500
# Walk through the directory tree
for root, dirs, files in os.walk(directory):
# Modify dirs in-place to skip any directories starting with '@'
dirs[:] = [d for d in dirs if not d.startswith('@')]
# Iterate over all files in the current directory
for filename in files:
# Check if the file is a JPEG
if filename.lower().endswith(('.jpeg', '.jpg')):
# Construct the full path to the file
file_path = os.path.join(root, filename)
try:
# Get the size of the file
file_size = os.path.getsize(file_path)
# Open the image file to check its dimensions
with Image.open(file_path) as img:
width, height = img.size
# If the file size is less than the limit and dimensions are smaller than the pixel limit, delete the file
if file_size < size_limit and (width < pixel_limit or height < pixel_limit):
# os.remove(file_path)
log_info(logger, f"Deleted \"{filename}\" because it was smaller than 100 KB or dimensions were smaller than 500px.")
else:
log_info(logger, f"\"{filename}\" checked at {file_size}KB, width {width}px, height {height}px.")
except Exception as e:
log_error(logger, f"Error processing file \"{filename}\": {e}")
# Command line interaction
if __name__ == '__main__':
log_info(logger, f"Current working directory: {os.getcwd()}")
if len(sys.argv) < 2:
log_error(logger, "No directory path provided.")
sys.exit(1)
start_directory = sys.argv[1]
if not os.path.isdir(start_directory):
log_error(logger, f"\"{start_directory}\" is not a valid directory.")
sys.exit(1)
delete_small_jpeg_files(start_directory)