-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
80 lines (60 loc) · 2.53 KB
/
main.py
File metadata and controls
80 lines (60 loc) · 2.53 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import image
import spotify
import config
from PIL import Image
from math import sqrt
import os
import numpy as np
# TODO: clean up code
# TODO: add a session.py file
# TODO: change the img = image.open to with Image.open as img:
spotify.preProcessCSV()
songIds = spotify.getAllSongIDs()
albumURLs = spotify.getAllAlbumURLs(songIds)
spotify.downloadAlbumCovers(albumURLs)
# DO FINAL PROCESSING HERE
albumData = spotify.returnAlbumData()
albumColors = [a[1].astype(np.float32) for a in albumData]
albumPaths = [a[2] for a in albumData]
# Determine N such that N*N = len(albumData) or the largest N where N*N < len(albumData)
N = int(sqrt(len(albumData) - config.K_DELETION))
# Return image data, with image resized to NxN
imgData = image.returnImageData(N, config.INPUT_IMAGE_PATH)
finalImg = Image.new(
"RGB",
(
N * config.ALBUM_TILE_SIZE,
N * config.ALBUM_TILE_SIZE
)
)
def findClosestAlbum(imgTileColor, albumColors):
diffs = np.array(albumColors) - imgTileColor
dists = np.linalg.norm(diffs, axis=1)
closestIdx = np.argmin(dists)
return closestIdx
for imgID, imgColor in imgData:
idx = findClosestAlbum(imgColor, albumColors)
albumPath = albumPaths[idx]
row = imgID // N # like y coords
col = imgID % N # like x coords
albumCover = Image.open(albumPath).convert("RGB")
# To resize with high quality, use Lanczos
# https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.Resampling.LANCZOS
# https://en.wikipedia.org/wiki/Lanczos_resampling
albumCover = albumCover.resize((config.ALBUM_TILE_SIZE, config.ALBUM_TILE_SIZE), Image.Resampling.LANCZOS)
# Notice the swap, since x is cols and y is rows
finalImg.paste(albumCover, (col * config.ALBUM_TILE_SIZE, row * config.ALBUM_TILE_SIZE))
del albumColors[idx]
del albumPaths[idx]
if config.BLEND_FINAL:
# overlay a bit of the initial image to get better color representation
inputImg = Image.open(config.INPUT_IMAGE_PATH).convert("RGB")
inputImg = inputImg.resize((N * config.ALBUM_TILE_SIZE, N * config.ALBUM_TILE_SIZE), Image.Resampling.LANCZOS)
finalImg = Image.blend(finalImg, inputImg, alpha=config.BLEND_ALPHA)
if config.DEBUG_FLAG:
finalImg.show()
print("\nFinal image of size", N, "x", N, "created!")
print("Albums unused: ", len(albumData) - len(imgData))
# Save to folder called output, create it if it doesn't exist, else append a number to the folder name
os.makedirs("output", exist_ok=True) # create output folder if it doesn't exist
finalImg.save(config.OUTPUT_IMAGE_PATH)