Skip to content

Commit

Permalink
Merge a9c6be3 into master
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Sep 23, 2021
2 parents bbdcb5d + a9c6be3 commit ac0743f
Show file tree
Hide file tree
Showing 13 changed files with 101 additions and 15 deletions.
49 changes: 47 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[![Generic badge](https://img.shields.io/badge/status-draft-red.svg)](#)
[![PyPI version shields.io](https://img.shields.io/pypi/v/img2texture.svg)](https://pypi.python.org/pypi/img2texture/)
[![Generic badge](https://img.shields.io/badge/Python-3.7+-blue.svg)](#)
[![Generic badge](https://img.shields.io/badge/OS-Windows%20|%20macOS%20|%20Linux-blue.svg)](#)
Expand Down Expand Up @@ -45,7 +44,53 @@ $ pip3 install img2texture

# Run

Create new `seamless.jpg` from `source.jpg`.
```
$ img2texture /path/to/source.jpg /path/to/seamless_result.jpg
$ img2texture /path/to/source.jpg /path/to/seamless.jpg
```

## --overlap

The `--overlap` option determines how much of the image will be used to hide the seams.

For example, the following command uses 25% of the width and 25% of the height
of the original image:

```
$ img2texture source.jpg seamless.jpg --overlap 0.25
```

Increasing the value makes the seam less visible. However, the image becomes smaller.

<details>
<summary>Sample images</summary>

:warning: If images don't load, check out the [original of this document on GitHub](https://github.com/rtmigo/img2texture_py#readme).

### --overlap 0.05

The 5% seam.

![--overlap 0.05](docs/3_orion_05_2x2.jpg)



### --overlap 0.4

The 40% seam.

![--overlap 40](docs/3_orion_40_2x2.jpg)

</details>

## --tile

The `--tile` option will create a 2x2 tiled version in addition to the converted image.

The following command will create `seamless.jpg` and `seamless_2x2.jpg`.

```
$ img2texture source.jpg seamless.jpg --tile
```

All the samples on this page were created with `--tile`.
Binary file added docs/3_orion_05.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 docs/3_orion_05_2x2.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 docs/3_orion_40.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 docs/3_orion_40_2x2.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 docs/3_orion_51.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 docs/3_orion_51_2x2.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 img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions img2texture/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from . import cli

if __name__ == "__main__":
cli()
52 changes: 41 additions & 11 deletions img2texture/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,69 @@
from enum import Enum
from pathlib import Path

import img2texture._constants as constants
from img2texture import img2tex
from ._constants import __version__ as version, __copyright__ as copyright
# import __version__ as version, __copyright__ as copyright
from ._tiling import tile


def print_version():
print(f'img2texture {version} {copyright}')
print(f'img2texture {constants.__version__} {constants.__copyright__}')


class Mode(Enum):
both = "both"
none = "none"


def confirm(message: str) -> bool:
while True:
answer = input(f"{message} (y/n) ").upper()
if answer.startswith("Y"):
return True
if answer.startswith("N"):
return False


class ParsedArgs:
def __init__(self):
if "--version" in sys.argv:
print_version()
exit(0)

parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(description="Converts images to seamless tiles")

parser.add_argument("-m", "--mix",
type=float,
default=0.2)
parser.add_argument("source",
help="Path of source image file.")
parser.add_argument("target",
help="Path of the converted file.")
parser.add_argument("--tile",
parser.add_argument("-o", "--overlap",
type=float,
default=0.2,
help="Fraction of the original width and height to "
"use for overlapping seam. Default: 0.2 "
"(i.e. 20%%)")
parser.add_argument("-t", "--tile",
action='store_true',
default=False,
help="Create an additional file with four copies "
"of the converted image merged side by side")

# hidden option
parser.add_argument("--mode",
choices=[str(m.value) for m in Mode],
default=Mode.both)
default=Mode.both,
help=argparse.SUPPRESS)
parser.add_argument('--version',
action='store_true',
default=False,
help="Show version info and exit")

self._parsed = parser.parse_args()

if not 0 <= self.overlap_pct <= 1.0:
parser.error("--overlap must be in range from 0 to 1")

@property
def source(self) -> Path:
return Path(self._parsed.source)
Expand All @@ -57,8 +76,8 @@ def target(self) -> Path:
return Path(self._parsed.target)

@property
def mix(self) -> float:
return self._parsed.mix
def overlap_pct(self) -> float:
return self._parsed.overlap

@property
def mode(self) -> Mode:
Expand All @@ -78,10 +97,21 @@ def tile_filename(texture: Path) -> Path:

def cli():
args = ParsedArgs()
img2tex(args.source, args.target, pct=args.mix)

if args.target.exists():
if not confirm(f"File '{args.target.name}' exists. Overwrite?"):
exit(3)
os.remove(args.target)

img2tex(args.source, args.target, pct=args.overlap_pct)

if args.tile:
tile_src = args.target if args.mode != Mode.none else args.source
tile_fn = tile_filename(tile_src)
if tile_fn.exists() and not confirm(
f"File '{tile_fn}' exists. Overwrite?"):
exit(3)

if tile_fn.exists():
os.remove(tile_fn)
tile(tile_src, tile_fn, horizontal=2, vertical=2)
2 changes: 1 addition & 1 deletion img2texture/_constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0.1"
__version__ = "0.1.0"

__copyright__ = "(c) 2021 Artyom Galkin <github.com/rtmigo>"
__license__ = "MIT"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def load_module_dict(filename: str) -> dict:
constants = load_module_dict(f'{name}/_constants.py')

readme = (Path(__file__).parent / 'README.md').read_text(encoding="utf-8")
readme = "#"+readme.partition('\n#')[-1]

setup(
name=name,
Expand Down Expand Up @@ -59,4 +60,3 @@ def load_module_dict(filename: str) -> dict:

test_suite="test_unit.suite"
)

7 changes: 7 additions & 0 deletions test_pkg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

if __name__ == "__main__":
with Package() as pkg:
# running console_scripts defined in setup.py
pkg.run_shell_code('img2texture --help')
pkg.run_shell_code('img2texture', expected_return_code=2)

# running img2texture/__main__.py
pkg.run_shell_code('python -m img2texture',
expected_return_code=2)
pkg.run_shell_code('python -m img2texture --help',
expected_return_code=0)

print("\nPackage is OK!")

0 comments on commit ac0743f

Please sign in to comment.