Skip to content

Commit

Permalink
Merge pull request #8 from nitfe/fix-pillow
Browse files Browse the repository at this point in the history
Fix entry point and import errors and one layer rgb
  • Loading branch information
iamjazzar committed Feb 15, 2024
2 parents 3152ca1 + 67fce92 commit 2f400ec
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 34 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ repos:
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
args: ['--maxkb=950']
- repo: https://github.com/psf/black
rev: 24.2.0
hooks:
Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ Takes an image file and generates a cross stitch pattern using a user specified

## Results

Photo by [Kiki Siepel](https://unsplash.com/@studiokiek?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/images/nature/flower?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/flower.jpg" alt="Alt text" style="display: inline-block; margin: 0 auto; max-width: 600px"/>
<img src="https://github.com/nitfe/tarraz/raw/v1.1.3/images/palestine.png" alt="Palestinian Flag" style="display: inline-block; margin: 0 auto; max-width: 600px"/>

<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/colored_symbols.jpg" alt="Colored with Symbols" style="display: inline-block; margin: 0 auto; max-width: 300px"/>
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/black_white_symbols.jpg" alt="Black White with Symbols" style="display: inline-block; margin: 0 auto; max-width: 300px"/>
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/colored.jpg" alt="Colored" style="display: inline-block; margin: 0 auto; max-width: 300px"/>
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/key.jpg" alt="Keys" style="display: inline-block; margin: 0 auto; max-width: 300px; height: 256px"/>
<img src="https://github.com/nitfe/tarraz/raw/v1.1.3/images/results/colored_symbols.jpg" alt="Colored with Symbols" style="display: inline-block; margin: 0 auto; max-width: 300px"/>
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/results/black_white_symbols.jpg" alt="Black White with Symbols" style="display: inline-block; margin: 0 auto; max-width: 300px"/>
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/results/colored.jpg" alt="Colored" style="display: inline-block; margin: 0 auto; max-width: 300px"/>
<img src="https://github.com/nitfe/tarraz/raw/v0.0.1/images/results/key.jpg" alt="Keys" style="display: inline-block; margin: 0 auto; max-width: 300px; height: 256px"/>


## Usage
Expand All @@ -31,7 +30,7 @@ pip install tarraz

### CLI Example
```shell
tarraz .tmp/palestinian_flag.jpg --colors 6 --stitches-count 100
tarraz images/palestine.png --colors 4 --stitches-count 200
```

### Python Example
Expand All @@ -45,7 +44,7 @@ from tarraz.models import RGB


# Choose a color provider
image_path = ".tmp/flower.jpg"
image_path = "images/palestine.png"
provider = DMCProvider()

tarraz = Tarraz(
Expand Down Expand Up @@ -76,7 +75,7 @@ SVGStitcher.stitch(
### Options
```shell
$ tarraz --help
usage: tarraz [-h] [-v] [-c COLORS] [-n STITCHES_COUNT] [-w WIDTH] [-m DMC] [-t TRANSPARENT [TRANSPARENT ...]] [-s DIST] [-z CELL_SIZE] [--no-cleanup] [--svg] image
usage: tarraz [-h] [--version] [-c COLORS] [-n STITCHES_COUNT] [-w WIDTH] [-m DMC] [-t TRANSPARENT [TRANSPARENT ...]] [-o DIST] [-z CELL_SIZE] [--no-cleanup] [--svg] image

Generate a DMC-colored cross-stitch pattern from a given image.

Expand All @@ -95,7 +94,7 @@ optional arguments:
-m DMC, --dmc DMC DMC json color path.
-t TRANSPARENT [TRANSPARENT ...], --transparent TRANSPARENT [TRANSPARENT ...]
A Color to ignore from the end result.
-s DIST, --dist DIST DMC json color path.
-o DIST, --dist DIST DMC json color path.
-z CELL_SIZE, --cell-size CELL_SIZE
The size of the generated Aida fabric cell.
--no-cleanup Don't run cleanup job on generated image.
Expand Down
Binary file removed images/black_white_symbols.jpg
Binary file not shown.
Binary file removed images/colored.jpg
Binary file not shown.
Binary file removed images/colored_symbols.jpg
Binary file not shown.
Binary file removed images/flower.jpg
Binary file not shown.
Binary file removed images/key.jpg
Binary file not shown.
Binary file added images/palestine.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/results/black_white_symbols.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/results/colored.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/results/colored_symbols.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/results/key.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ known_first_party = "tarraz"

[tool.poetry]
name = "tarraz"
version = "1.1.2"
version = "1.2.0"
description = "A cross-stitch image generator. Generates a cross stitch pattern given by a user and generates a DMC colored pattern."
authors = ["Ahmed Jazzar <ahmed@nitfe.com>"]
maintainers = []

license = "GNU Affero General Public License v3"
readme = "README.md"
packages = [{ include = "tarraz"}]
packages = [{ include = "tarraz" }]
homepage = "https://www.nitfe.com/"
repository = "https://github.com/nitfe/tarraz.git"
documentation = "https://github.com/nitfe/tarraz/blob/main/README.md"
Expand All @@ -47,11 +47,11 @@ Changelog = "https://github.com/nitfe/tarraz/releases"
# Requirements
[tool.poetry.dependencies]
python = ">=3.8"
pillow = "*"
pillow = ">=9.5.0"

[tool.poetry.dev-dependencies]
pre-commit = { version = "*" }

# Entry point
[tool.poetry.scripts]
tarraz = "tarraz.main:main"
[tool.poetry.plugins.console_scripts]
"tarraz" = "tarraz.main:main"
13 changes: 11 additions & 2 deletions tarraz/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
import importlib.metadata
import logging
import os

from tarraz import constants
Expand All @@ -14,7 +15,6 @@

def init_argparse() -> argparse.ArgumentParser:
parser.add_argument(
"-v",
"--version",
action="version",
version=f"{parser.prog} version {VERSION}",
Expand Down Expand Up @@ -59,7 +59,7 @@ def init_argparse() -> argparse.ArgumentParser:
help="A Color to ignore from the end result.",
)
parser.add_argument(
"-s",
"-o",
"--dist",
type=str,
default=constants.BASE_DIR / ".tmp/",
Expand All @@ -82,13 +82,22 @@ def init_argparse() -> argparse.ArgumentParser:
action="store_true",
help="Export result to svg files.",
)
parser.add_argument(
"-v",
"--verbose",
action="store_true",
help="Show debug messages.",
)
return parser


def main() -> None:
p = init_argparse()
args = p.parse_args()

if args.verbose:
logger.setLevel(logging.DEBUG)

base_file_name = os.path.basename(args.image).split(".")[0]

logger.info("Generating pattern for %s...", base_file_name)
Expand Down
7 changes: 4 additions & 3 deletions tarraz/processor.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from typing import TYPE_CHECKING, List, Union

from PIL import Image
from typing import List, TYPE_CHECKING, Union

from tarraz.logger import logger
from tarraz.models import Color, Coordinate, ImageSize, RGB
from tarraz.models import RGB, Color, Coordinate, ImageSize
from tarraz.providers import DMCProvider
from tarraz.utils import generate_image, get_neighbours, index_of

Expand Down Expand Up @@ -31,7 +32,7 @@ def __init__(

self._cleanup = cleanup
self._colors_num = colors_num
self._image = Image.open(image_path)
self._image = Image.open(image_path).convert("RGB")
self._provider = provider
self._x_count = x_count

Expand Down
7 changes: 4 additions & 3 deletions tarraz/stitcher/display.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import TYPE_CHECKING, Optional

from PIL import Image

from tarraz.stitcher import Stitcher

if TYPE_CHECKING:
Expand All @@ -12,16 +13,16 @@ def init(self, width: int, height: int) -> None:
self.result = Image.new("RGB", (width, height))

def draw_cell(
self, color: "Optional[Color]", coordinate: "Coordinate", size: int
self, coordinate: "Coordinate", size: int, color: "Optional[Color]" = None
) -> None:
pixels = self.result.load()

for i in range(coordinate.x, coordinate.x + size):
for j in range(coordinate.y, coordinate.y + size):
pixels[i - size, j - size] = color.rgb
pixels[i - size, j - size] = color.rgb if color else (255, 255, 255, 0)

def finish(self) -> None:
self.result.show()

def save(self, dist_dir: str, ext: str) -> None:
def save(self, ext: str) -> None:
return
14 changes: 10 additions & 4 deletions tarraz/stitcher/stitcher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import os
from abc import ABC
from typing import TYPE_CHECKING, Optional, List
from typing import TYPE_CHECKING, List, Optional

from tarraz import constants
from tarraz.logger import logger
from tarraz.models import Coordinate

if TYPE_CHECKING:
from tarraz.models import Color, ImageSize, Palette, PaletteImage, RGB
from tarraz.models import RGB, Color, ImageSize, Palette, PaletteImage


class Stitcher(ABC):
Expand Down Expand Up @@ -56,7 +56,7 @@ def generate_key(
return NotImplemented

def draw_cell(
self, color: "Optional[Color]", coordinate: "Coordinate", size: int
self, coordinate: "Coordinate", size: int, color: "Optional[Color]" = None
) -> NotImplemented:
return NotImplemented

Expand All @@ -69,14 +69,17 @@ def draw_cells(
config: "Optional[dict]" = None,
transparent: "Optional[List[RGB]]" = None,
):
if not transparent:
transparent = []

x = y = cell_size
for row in pattern:
for color_i in row:
coordinate = Coordinate(x, y)
color = (
colors[color_i] if colors[color_i].rgb not in transparent else None
)
self.draw_cell(color, coordinate, cell_size)
self.draw_cell(coordinate, cell_size, color=color)
x += cell_size
y += cell_size
x = cell_size
Expand Down Expand Up @@ -104,6 +107,9 @@ def stitch(
width = size.width * cell_size
height = size.height * cell_size

if not transparent:
transparent = []

if not configs:
configs = [{"name": "NO_CONFIG"}]

Expand Down
12 changes: 6 additions & 6 deletions tarraz/stitcher/svg.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import os
from typing import TYPE_CHECKING, Optional, List
from typing import TYPE_CHECKING, List, Optional

from tarraz.logger import logger
from tarraz.models import Color, Coordinate
from tarraz.stitcher import Stitcher

if TYPE_CHECKING:
from tarraz.models import (
RGB,
ImageSize,
Palette,
PaletteImage,
RGB,
StrokeType,
SVGAttributes,
)
Expand Down Expand Up @@ -77,9 +77,9 @@ def generate_key(
keyed.add(color.code)

def draw_cell(
self, color: "Optional[Color]", coordinate: "Coordinate", size: int
self, coordinate: "Coordinate", size: int, color: "Optional[Color]" = None
) -> None:
fill, symbols, stroke = self._get_svg_attributes(color, coordinate, size)
fill, symbols, stroke = self._get_svg_attributes(coordinate, size, color=color)

self._append_to_file(
f"""
Expand Down Expand Up @@ -244,7 +244,7 @@ def _gen_glyph(color: "Color", coordinate: "Coordinate", scale: float = 1.0) ->
def _add_key_to_svg(
self, coordinate: "Coordinate", size: int, color: "Color"
) -> None:
fill, symbols, stroke = self._get_svg_attributes(color, coordinate, size)
fill, symbols, stroke = self._get_svg_attributes(coordinate, size, color)

self._append_to_file(
f"""
Expand Down Expand Up @@ -308,7 +308,7 @@ def _add_key_to_svg(
)

def _get_svg_attributes(
self, color: "Optional[Color]", coordinate: "Coordinate", size: int
self, coordinate: "Coordinate", size: int, color: "Optional[Color]" = None
) -> "SVGAttributes":
fill = "fill:rgb(255,255,255);"
stroke: "StrokeType" = "stroke:none;"
Expand Down

0 comments on commit 2f400ec

Please sign in to comment.