From 2f62c53a9f3dbb03bd8beeace2d01ca39f43d90b Mon Sep 17 00:00:00 2001 From: Lynnesbian Date: Sat, 17 Oct 2020 13:34:56 +1000 Subject: [PATCH] support for png covers with transparency, other fixes --- bcao.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/bcao.py b/bcao.py index 7e070bd..20f2337 100755 --- a/bcao.py +++ b/bcao.py @@ -62,7 +62,8 @@ if not os.path.exists(args.zip): die(f"Couldn't find {args.zip}.", 2) log("Extracting...") -tmp = tempfile.TemporaryDirectory() +tmpd = tempfile.TemporaryDirectory() +tmp = tmpd.name cover = None song_names = [] @@ -89,6 +90,10 @@ log("Resizing album art to embed in songs...") with Image.open(os.path.join(tmp, cover)) as image: temp_cover = os.path.join(tmp, "cover-lq.jpg") + if image.mode in ["RGBA", "P"]: + # remove alpha channel + image = image.convert("RGB") + image.save(temp_cover, quality=85, optimize=True) image_smol = image @@ -107,14 +112,14 @@ with open(temp_cover, 'r+b') as cover_file: with Image.open(temp_cover) as image: if song_format == "ogg": # i hate this - cover = Picture() - cover.data = data - cover.type = mutagen.id3.PictureType.COVER_FRONT - cover.mime = "image/jpeg" - cover.width = image.size[0] - cover.height = image.size[1] - cover.depth = image.bits - cover = base64.b64encode(cover.write()).decode("ascii") + embed_cover = Picture() + embed_cover.data = data + embed_cover.type = mutagen.id3.PictureType.COVER_FRONT + embed_cover.mime = "image/jpeg" + embed_cover.width = image.size[0] + embed_cover.height = image.size[1] + embed_cover.depth = image.bits + embed_cover = base64.b64encode(embed_cover.write()).decode("ascii") else: log(f"Format {song_format} is not fully supported - cover images will not be modified", 1) @@ -131,7 +136,7 @@ for song in song_names: album = get_tag(m, "album") # embed cover art if song_format == "ogg": - m["metadata_block_picture"] = [cover] + m["metadata_block_picture"] = [embed_cover] m.save() if len(artists) > 1 and "Various Artists" not in artists: @@ -157,11 +162,11 @@ while artist is None: destination = os.path.join(args.destination, artist, album) log(f"Moving files to {destination}...") -os.makedirs(destination, exist_ok = True) +os.makedirs(destination, exist_ok=True) for source_name, dest_name in songs.items(): shutil.move(os.path.join(tmp, source_name), os.path.join(destination, dest_name)) -shutil.move(os.path.join(tmp, "cover.jpg"), os.path.join(destination, "cover.jpg")) +shutil.move(os.path.join(tmp, cover), os.path.join(destination, cover)) -tmp.cleanup() +tmpd.cleanup() log("Done!")