-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
72 lines (58 loc) · 2.35 KB
/
main.py
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
64
65
66
67
68
69
70
71
import os
import argparse
from mutagen.mp3 import MP3
from mutagen.id3 import ID3, APIC, error, ID3NoHeaderError
from PIL import Image
from io import BytesIO
def crop_center_square(image):
"""Crop the center square of the image."""
width, height = image.size
new_edge_length = min(width, height)
left = (width - new_edge_length) // 2
top = (height - new_edge_length) // 2
right = (width + new_edge_length) // 2
bottom = (height + new_edge_length) // 2
return image.crop((left, top, right, bottom))
def process_mp3_file(file_path):
"""Process a single MP3 file to crop its album art to 1:1."""
try:
audio = MP3(file_path, ID3=ID3)
except ID3NoHeaderError:
print(f"Skipping {file_path}: No ID3 tag.")
return
if not audio.tags:
print(f"Skipping {file_path}: No tags found.")
return
apic_tags = [tag for tag in audio.tags.values() if isinstance(tag, APIC)]
if not apic_tags:
print(f"Skipping {file_path}: No APIC (album art) tag found.")
return
for apic in apic_tags:
try:
image_data = apic.data
image = Image.open(BytesIO(image_data))
if image.width == image.height:
print(f"Skipping {file_path}: Image is already 1:1 aspect ratio.")
return
cropped_image = crop_center_square(image)
output = BytesIO()
cropped_image.save(output, format='JPEG')
apic.data = output.getvalue()
audio.save()
print(f"Processed {file_path}: Album art cropped to 1:1.")
return # Stop after processing the first APIC tag
except Exception as e:
print(f"Error processing album art in {file_path}: {e}")
def process_folder(folder_path):
"""Process all MP3 files in the given folder."""
for filename in os.listdir(folder_path):
if filename.lower().endswith('.mp3'):
file_path = os.path.join(folder_path, filename)
process_mp3_file(file_path)
def main():
parser = argparse.ArgumentParser(description='Crop center 1:1 the image art of MP3 files in a folder.')
parser.add_argument('folder', help='Path to the folder containing MP3 files')
args = parser.parse_args()
process_folder(args.folder)
if __name__ == "__main__":
main()