-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (111 loc) · 4.7 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import argparse
from PIL import Image
from collage_builder import SingleSizeColumnCollageBuilder
from color_factory import SpotifyOrderedColorFactory, SpotifyRandomColorFactory, SingleColorFactory, ColorFactory
from core import ImageSize, create_dir
from image_code_provider import PlaylistProvider
from spotify_utils import get_playlist_name, SpotifyCodeColor, DefaultSpotifyBackgroundColor, SpotifyBarColor
_RESULT_FILE_FOLDER = "./result/"
_RESULT_FILE_EXTENSION = ".png"
_DEFAULT_CODE_IMAGE_WIDTH = 256
_IMAGE_FACTORY_NAME_ORDERED = "ordered_colors"
_IMAGE_FACTORY_NAME_RANDOM = "random_colors"
_IMAGE_FACTORY_NAME_SINGLE = "single_color"
def _get_args():
parser = argparse.ArgumentParser()
parser.add_argument("playlist_uri", help="Spotify URI for source playlist", type=str)
_default_column_count = 1
parser.add_argument(
'--columns',
dest="column_count",
help=f"Number of columns ({_default_column_count} default)",
type=int,
default=_default_column_count
)
parser.add_argument('--show', help="Open final image when finished", action='store_true', default=False)
_default_size = _DEFAULT_CODE_IMAGE_WIDTH
parser.add_argument(
'--size',
help=f"Size(width) of individual track code (128, 256, 512, etc). ({_default_size} default)",
type=int,
default=_DEFAULT_CODE_IMAGE_WIDTH
)
parser.add_argument('--shuffle', help="Shuffle tracks in the playlist", action='store_true', default=False)
_default_color_factory_name = _IMAGE_FACTORY_NAME_RANDOM
parser.add_argument(
'--color_factory',
dest="color_factory_name",
help=f"Color choose strategy for individual code image ('{_default_color_factory_name}' default)",
type=str,
choices=[_IMAGE_FACTORY_NAME_ORDERED, _IMAGE_FACTORY_NAME_RANDOM, _IMAGE_FACTORY_NAME_SINGLE],
default=_default_color_factory_name
)
_default_bar_color = SpotifyBarColor.white.value
parser.add_argument(
'--bar_color',
help=f"Color of code image bars ('{_default_bar_color}' default)",
choices=list(map(lambda it: it.value, list(SpotifyBarColor))),
type=str,
default=_default_bar_color
)
_default_bg_color_hex = DefaultSpotifyBackgroundColor.black.value
parser.add_argument(
'--bg_color_hex',
help=f"Color of code image background ('{_default_bg_color_hex}' default)",
type=str,
default=_default_bg_color_hex
)
return parser.parse_args()
def _get_color_factory_by_name(factory_name: str, code_color: SpotifyCodeColor) -> ColorFactory:
_factory_mapping = {
_IMAGE_FACTORY_NAME_ORDERED: SpotifyOrderedColorFactory(code_color.bar),
_IMAGE_FACTORY_NAME_RANDOM: SpotifyRandomColorFactory(code_color.bar),
_IMAGE_FACTORY_NAME_SINGLE: SingleColorFactory(code_color)
}
try:
return _factory_mapping[factory_name]
except KeyError as e:
raise ValueError(f"Undefined color factory name: '{factory_name}'")
def _create_collage_for_playlist(
playlist_uri: str,
color_factory: ColorFactory,
column_count: int,
code_width: int,
shuffle: bool
) -> Image.Image:
print(f"Fetching tracks from playlist URI:{playlist_uri}")
code_size = ImageSize(
width=code_width,
height=PlaylistProvider.get_code_height(code_width)
)
image_provider = PlaylistProvider(code_size.width, playlist_uri, color_factory, shuffle)
code_images = image_provider.get_code_images()
code_count = len(code_images)
canvas_size = ImageSize(
width=code_size.width * column_count,
height=code_size.height * round(code_count / column_count)
)
collage_builder = SingleSizeColumnCollageBuilder(canvas_size, code_size, code_count, column_count)
return collage_builder.build(code_images)
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
_args = _get_args()
_playlist_name = get_playlist_name(_args.playlist_uri)
print(f"Found playlist '{_playlist_name}'!")
_bar_color = SpotifyBarColor[_args.bar_color]
_color_factory = _get_color_factory_by_name(
_args.color_factory_name,
SpotifyCodeColor(bar=_bar_color, background_hex=_args.bg_color_hex)
)
_collage = _create_collage_for_playlist(
playlist_uri=_args.playlist_uri,
color_factory=_color_factory,
column_count=_args.column_count,
code_width=_args.size,
shuffle=_args.shuffle
)
_result_path = f"{_RESULT_FILE_FOLDER}{_playlist_name}-{_args.column_count}x{_args.size}{_RESULT_FILE_EXTENSION}"
create_dir(_result_path)
_collage.save(_result_path, "PNG")
if _args.show:
_collage.show()