add colors to album page header

- add colors attribute to the album class
- render color gradient in the album page
This commit is contained in:
geoffrey45
2022-06-30 14:01:48 +03:00
committed by Mungai Geoffrey
parent f919ce35df
commit a23b6200eb
7 changed files with 70 additions and 42 deletions
+10
View File
@@ -2,6 +2,7 @@
This file contains the Album class for interacting with This file contains the Album class for interacting with
album documents in MongoDB. album documents in MongoDB.
""" """
from typing import List
from app.db.mongodb import convert_many from app.db.mongodb import convert_many
from app.db.mongodb import convert_one from app.db.mongodb import convert_one
from app.db.mongodb import MongoAlbums from app.db.mongodb import MongoAlbums
@@ -59,3 +60,12 @@ class Albums(MongoAlbums):
""" """
album = self.collection.find_one({"hash": hash}) album = self.collection.find_one({"hash": hash})
return convert_one(album) return convert_one(album)
def set_album_colors(self, colors: List[str], album_id: str) -> None:
"""
Sets the colors for an album.
"""
self.collection.update_one(
{"_id": ObjectId(album_id)},
{"$set": {"colors": colors}},
)
+5 -1
View File
@@ -15,6 +15,7 @@ from app.lib.albumslib import ValidateAlbumThumbs
from app.lib import trackslib from app.lib import trackslib
from app.lib.populate import CreateAlbums, Populate from app.lib.populate import CreateAlbums, Populate
from app.lib.playlistlib import ValidatePlaylistThumbs from app.lib.playlistlib import ValidatePlaylistThumbs
from app.lib.colorlib import ProcessAlbumColors
@helpers.background @helpers.background
@@ -23,7 +24,7 @@ def run_checks():
Checks for new songs every 5 minutes. Checks for new songs every 5 minutes.
""" """
# while True: while True:
trackslib.validate_tracks() trackslib.validate_tracks()
Populate() Populate()
@@ -34,6 +35,9 @@ def run_checks():
ValidateAlbumThumbs() ValidateAlbumThumbs()
ValidatePlaylistThumbs() ValidatePlaylistThumbs()
ProcessAlbumColors()
time.sleep(300)
@helpers.background @helpers.background
+25 -28
View File
@@ -1,17 +1,19 @@
from io import BytesIO
import colorgram import colorgram
from app import api
from app import instances from app import instances
from app.lib.taglib import return_album_art
from PIL import Image
from progress.bar import Bar
def get_image_colors(image) -> list: from app.helpers import Get
from app import settings
from app.models import Album
from app.logger import get_logger
log = get_logger()
def get_image_colors(image: str) -> list:
"""Extracts 2 of the most dominant colors from an image.""" """Extracts 2 of the most dominant colors from an image."""
try: try:
colors = sorted(colorgram.extract(image, 2), key=lambda c: c.hsl.h) colors = sorted(colorgram.extract(image, 4), key=lambda c: c.hsl.h)
except OSError: except OSError:
return [] return []
@@ -24,30 +26,25 @@ def get_image_colors(image) -> list:
return formatted_colors return formatted_colors
def save_track_colors(img, filepath) -> None: class ProcessAlbumColors:
"""Saves the track colors to the database""" def __init__(self) -> None:
log.info("Processing album colors")
all_albums = Get.get_all_albums()
track_colors = get_image_colors(img) all_albums = [a for a in all_albums if len(a.colors) == 0]
tc_dict = { for a in all_albums:
"filepath": filepath, self.process_color(a)
"colors": track_colors,
}
instances.track_color_instance.insert_track_color(tc_dict) log.info("Processing album colors ... ✅")
@staticmethod
def process_color(album: Album):
img = settings.THUMBS_PATH + "/" + album.image
def save_t_colors(): colors = get_image_colors(img)
_bar = Bar("Processing image colors", max=len(api.DB_TRACKS))
for track in api.DB_TRACKS: if len(colors) > 0:
filepath = track["filepath"] instances.album_instance.set_album_colors(colors, album.albumid)
album_art = return_album_art(filepath)
if album_art is not None: return colors
img = Image.open(BytesIO(album_art))
save_track_colors(img, filepath)
_bar.next()
_bar.finish()
+8
View File
@@ -82,6 +82,7 @@ class Album:
Creates an album object Creates an album object
""" """
albumid: str
title: str title: str
artist: str artist: str
hash: str hash: str
@@ -92,14 +93,21 @@ class Album:
is_soundtrack: bool = False is_soundtrack: bool = False
is_compilation: bool = False is_compilation: bool = False
is_single: bool = False is_single: bool = False
colors: List[str] = field(default_factory=list)
def __init__(self, tags): def __init__(self, tags):
self.albumid = tags["_id"]["$oid"]
self.title = tags["title"] self.title = tags["title"]
self.artist = tags["artist"] self.artist = tags["artist"]
self.date = tags["date"] self.date = tags["date"]
self.image = tags["image"] self.image = tags["image"]
self.hash = tags["hash"] self.hash = tags["hash"]
try:
self.colors = tags["colors"]
except KeyError:
self.colors = []
@property @property
def is_soundtrack(self) -> bool: def is_soundtrack(self) -> bool:
keywords = ["motion picture", "soundtrack"] keywords = ["motion picture", "soundtrack"]
+11 -3
View File
@@ -1,6 +1,13 @@
<template> <template>
<div class="album-h" ref="albumheaderthing"> <div class="album-h" ref="albumheaderthing">
<div class="a-header rounded"> <div
class="a-header rounded"
:style="{
backgroundImage: `linear-gradient(
37deg, ${album.colors[0]}, ${album.colors[3]}
)`,
}"
>
<div class="art"> <div class="art">
<div <div
class="image shadow-lg rounded" class="image shadow-lg rounded"
@@ -18,7 +25,9 @@
<span v-else-if="album.is_single">Single</span> <span v-else-if="album.is_single">Single</span>
<span v-else>Album</span> <span v-else>Album</span>
</div> </div>
<div class="title ellip">{{ album.title }}</div> <div class="title ellip">
{{ album.title }}
</div>
</div> </div>
<div class="bottom"> <div class="bottom">
<div class="stats"> <div class="stats">
@@ -67,7 +76,6 @@ useVisibility(albumheaderthing, nav.toggleShowPlay);
padding: 1rem; padding: 1rem;
height: 100%; height: 100%;
background-color: $black; background-color: $black;
background-color: #000000;
background-image: linear-gradient(37deg, $black 20%, $gray, $black 90%); background-image: linear-gradient(37deg, $black 20%, $gray, $black 90%);
.art { .art {
+4 -2
View File
@@ -15,7 +15,7 @@ export interface Track {
image: string; image: string;
tracknumber?: number; tracknumber?: number;
disknumber?: number; disknumber?: number;
index?: number index?: number;
} }
export interface Folder { export interface Folder {
@@ -27,6 +27,7 @@ export interface Folder {
} }
export interface AlbumInfo { export interface AlbumInfo {
albumid: string;
title: string; title: string;
artist: string; artist: string;
count: number; count: number;
@@ -36,7 +37,8 @@ export interface AlbumInfo {
is_compilation: boolean; is_compilation: boolean;
is_soundtrack: boolean; is_soundtrack: boolean;
is_single: boolean; is_single: boolean;
hash: string hash: string;
colors: string[];
} }
export interface Artist { export interface Artist {
-1
View File
@@ -20,7 +20,6 @@
<AlbumBio :bio="album.bio" /> <AlbumBio :bio="album.bio" />
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</template> </template>