Skip to content

Commit

Permalink
mean color utils functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemiyGranat committed Mar 8, 2024
1 parent 9f2230c commit dd3d3f7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
separate-compose := "command -v docker-compose 2>/dev/null"

# TODO: add compose variable to avoid branching in every recipe

init:
if [ ! -f requirements.lock ]; then rye sync; fi
rye run pre-commit install
Expand Down
13 changes: 12 additions & 1 deletion scraper/src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,18 @@ async def process_image(url: str, info: ScraperInfo) -> None:
img.save(output_path)

await ctx.image_repo.add(
entities.Image(id=id, url=url, hash=hash_value, processed=0)
entities.Image(
id=id,
url=url,
hash=hash_value,
mean_h=0,
mean_s=0,
mean_v=0,
mean_l=0,
mean_a=0,
mean_b=0,
processed=0,
)
)
info.images_scraped += 1

Expand Down
26 changes: 26 additions & 0 deletions shared/color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from enum import Enum

import numpy as np
from PIL import Image
from skimage import color


class ColorModel(int, Enum):
LAB = 0
HSV = 1


def mean_color(image_path, color_model):
image = Image.open(image_path).convert("RGBA")
np_image = np.array(image)

mask = np_image[..., 3] > 0
np_image = np_image[..., :3][mask].reshape(-1, 3) / 255.0

if color_model == ColorModel.LAB:
converted_image = color.rgb2lab(np_image)
else:
converted_image = color.rgb2hsv(np_image)

mean_color = np.mean(converted_image, axis=0)
return mean_color
4 changes: 3 additions & 1 deletion shared/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class SqliteRepository(AbstractRepository):
async def create_table(self) -> None:
await self._db.execute(
query=f"""CREATE TABLE IF NOT EXISTS {self._table_name}
(id TEXT, url TEXT, hash TEXT, processed INTEGER);"""
(id TEXT, url TEXT, hash TEXT, mean_h REAL, mean_s REAL,
mean_v REAL, mean_l REAL, mean_a REAL, mean_b REAL,
processed INTEGER);"""
)

async def get_many_from_list(self, field, values) -> List[Entity]:
Expand Down
6 changes: 6 additions & 0 deletions shared/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class Image(Entity):
id: str
url: str
hash: str
mean_h: float
mean_s: float
mean_v: float
mean_l: float
mean_a: float
mean_b: float
processed: int # bool but SQLite moment

_pk: ClassVar[str] = "id"
Expand Down

0 comments on commit dd3d3f7

Please sign in to comment.