Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for blurhash #1490

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/directory_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ def handle_new_image(user, path, job_id, photo=None):
job_id, path, elapsed
)
)
photo._get_blurhash()
elapsed = (datetime.datetime.now() - start).total_seconds()
util.logger.info(
"job {}: get blurhash: {}, elapsed: {}".format(job_id, path, elapsed)
)
photo._recreate_search_captions()
elapsed = (datetime.datetime.now() - start).total_seconds()
util.logger.info(
Expand Down
14 changes: 14 additions & 0 deletions api/migrations/0076_add_blurhash.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("api", "0075_alter_face_cluster_person"),
]
operations = [
migrations.AddField(
model_name="Photo",
name="blurhash",
field=models.TextField(blank=True, null=True),
),
]
21 changes: 18 additions & 3 deletions api/models/photo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fractions import Fraction
from io import BytesIO

import blurhash
import numpy as np
import PIL
import requests
Expand Down Expand Up @@ -73,6 +74,7 @@ class Photo(models.Model):
captions_json = models.JSONField(blank=True, null=True, db_index=True)

dominant_color = models.TextField(blank=True, null=True)
blurhash = models.TextField(blank=True, null=True)

search_captions = models.TextField(blank=True, null=True, db_index=True)
search_location = models.TextField(blank=True, null=True, db_index=True)
Expand Down Expand Up @@ -836,6 +838,19 @@ def _get_dominant_color(self, palette_size=16):
except Exception:
logger.info("Cannot calculate dominant color {} object".format(self))

def _get_blurhash(self, palette_size=16):
# Skip if it's already calculated
if self.blurhash:
return
try:
hash = blurhash.encode(
self.square_thumbnail_small.path, x_components=4, y_components=4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

square_thumbnail_small becomes redundant once thumbhash or blurhash is fully implemented, so use square_thumbnail instead.
Currently, we create white placeholders on the frontend, then update their color to match the dominant color upon receiving a paged response. Next, we use square_thumbnail_small, apply a blur effect, and display it as a temporary preview. Finally, the actual thumbnail is loaded for the timeline. Once thumbhash or blurhash is implemented, dominant color and square_thumbnail_small are redundant and will probably be removed in the future :)

)
self.blurhash = hash
self.save()
except Exception:
logger.info("Cannot calculate blurhash {} object".format(self))

def manual_delete(self):
for file in self.files.all():
if os.path.isfile(file.path):
Expand Down Expand Up @@ -873,7 +888,7 @@ def _set_embedded_media(self, obj):
return obj.main_file.embedded_media

def __str__(self):
main_file_path = self.main_file.path if self.main_file is not None else "No main file"
return (
"{} - {} - {}".format(self.image_hash, self.owner, main_file_path)
main_file_path = (
self.main_file.path if self.main_file is not None else "No main file"
)
return "{} - {} - {}".format(self.image_hash, self.owner, main_file_path)
8 changes: 8 additions & 0 deletions api/serializers/photos.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class PhotoSummarySerializer(serializers.ModelSerializer):
id = serializers.SerializerMethodField()
dominantColor = serializers.SerializerMethodField()
blurhash = serializers.SerializerMethodField()
aspectRatio = serializers.SerializerMethodField()
url = serializers.SerializerMethodField()
location = serializers.SerializerMethodField()
Expand All @@ -25,6 +26,7 @@ class Meta:
fields = (
"id",
"dominantColor",
"blurhash",
"url",
"location",
"date",
Expand Down Expand Up @@ -84,6 +86,12 @@ def get_dominantColor(self, obj) -> str:
else:
return ""

def get_blurhash(self, obj) -> str:
if obj.blurhash:
return obj.blurhash
else:
return ""

def get_type(self, obj) -> str:
if obj.video:
return "video"
Expand Down
1 change: 1 addition & 0 deletions api/views/albums.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ def get_queryset(self):
"main_file",
"search_location",
"dominant_color",
"blurhash",
"public",
"rating",
"hidden",
Expand Down
2 changes: 2 additions & 0 deletions api/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def list(self, request):
"main_file",
"search_location",
"dominant_color",
"blurhash",
"public",
"rating",
"hidden",
Expand Down Expand Up @@ -82,6 +83,7 @@ def list(self, request):
"main_file",
"search_location",
"dominant_color",
"blurhash",
"public",
"rating",
"hidden",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ timm==1.0.11
transformers==4.46.2
# Dependencies for mistral quantized
llama-cpp-python==0.3.1
blurhash-python==1.2.2