#20 Bugfix: Resize the cover image (max allow: 640x640, same as 163), thus can reduce the file size.

This commit is contained in:
codezjx 2017-10-21 16:28:55 +08:00
parent 1adf7db7c6
commit 67840eae8c
4 changed files with 19 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import requests
from ncm import config from ncm import config
from ncm.api import CloudApi from ncm.api import CloudApi
from ncm.file_util import add_metadata_to_song from ncm.file_util import add_metadata_to_song
from ncm.file_util import resize_img
def download_song_by_id(song_id, download_folder, sub_folder=True): def download_song_by_id(song_id, download_folder, sub_folder=True):
@ -59,6 +60,9 @@ def download_song_by_song(song, download_folder, sub_folder=True):
cover_file_name = 'cover_{}.jpg'.format(song_id) cover_file_name = 'cover_{}.jpg'.format(song_id)
download_file(cover_url, cover_file_name, song_download_folder) download_file(cover_url, cover_file_name, song_download_folder)
# resize cover
resize_img(os.path.join(song_download_folder, cover_file_name))
# add metadata for song # add metadata for song
song_file_path = os.path.join(song_download_folder, song_file_name) song_file_path = os.path.join(song_download_folder, song_file_name)
cover_file_path = os.path.join(song_download_folder, cover_file_name) cover_file_path = os.path.join(song_download_folder, cover_file_name)

View File

@ -2,6 +2,19 @@
from mutagen.mp3 import MP3, HeaderNotFoundError from mutagen.mp3 import MP3, HeaderNotFoundError
from mutagen.id3 import ID3, APIC, TPE1, TIT2, TALB, error from mutagen.id3 import ID3, APIC, TPE1, TIT2, TALB, error
from PIL import Image
def resize_img(file_path, max_size=(640, 640), quality=90):
try:
img = Image.open(file_path)
except IOError:
print('Can\'t open image:', file_path)
return
if img.size[0] > max_size[0] or img.size[1] > max_size[1]:
img.thumbnail(max_size, Image.ANTIALIAS)
img.save(file_path, quality=quality)
def add_metadata_to_song(file_path, cover_path, song): def add_metadata_to_song(file_path, cover_path, song):

View File

@ -1,3 +1,4 @@
requests>=2.17.3 requests>=2.17.3
pycrypto>=2.6.1 pycrypto>=2.6.1
mutagen>=1.38.0 mutagen>=1.38.0
Pillow>=4.3.0

View File

@ -9,6 +9,7 @@ setup(
'requests>=2.17.3', 'requests>=2.17.3',
'pycrypto>=2.6.1', 'pycrypto>=2.6.1',
'mutagen>=1.38.0', 'mutagen>=1.38.0',
'Pillow>=4.3.0',
], ],
entry_points={ entry_points={