Skip to content

Commit

Permalink
add blending cli options
Browse files Browse the repository at this point in the history
  • Loading branch information
hanzhi713 committed Nov 29, 2021
1 parent f7fc1d4 commit 29222b4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 24 deletions.
41 changes: 27 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ You can either use our pre-built binaries from [release](https://github.com/hanz

Binaries can be downloaded from [release](https://github.com/hanzhi713/image-collage-maker/releases).

On Windows, my program may be blocked by Windows Defender because it is not signed. Don't worry as there is no security risk. On MacOS or Linux, after downloading the binary, you may need to add executing permission. Open your terminal, go to the file's directory and type
On Windows, my program may be blocked by Windows Defender because it is not signed (signing costs money!). Don't worry as there is no security risk. On MacOS or Linux, after downloading the binary, you may need to add executing permission. Open your terminal, go to the file's directory and type

```bash
chmod +x ./photomosaic-maker-3.0-macos-x64
Expand Down Expand Up @@ -96,7 +96,7 @@ This fitting option ensures that each tile is used for the same amount of times.
python3 make_img.py --path img --dest_img img/1.png --size 25 --dup 6 --out collage.png
```

```--dup 6``` specifies that each tile needs to be used 6 times (i.e. duplicates the set of tiles 6 times). Increase that number if you don't have enough source tiles or you want a better fitting result. Note that a large number of tiles may result in long computational time. To make sure the computation completes within a reasonable amount of time, please make sure that you are using less than 6000 tiles after duplication.
```--dup 6``` specifies that each tile needs to be used 6 times (i.e. duplicates the set of tiles 6 times). Increase that number if you don't have enough source tiles or you want a better fitting result. Note that a large number of tiles may result in long computational time. To make sure the computation completes within a reasonable amount of time, it is recommended that you use less than 6000 tiles after duplication. Tile number larger than 6000 will probably takes longer than a minute to compute. Note that this recommended limit does **not** apply for the best fit option (see section below).

| Original | Fitting Result |
| ------------------------------------------- | ---------------------------------------------- |
Expand Down Expand Up @@ -153,7 +153,7 @@ python make_img.py --recursive --path img/zhou --dest_img examples/messi.jpg --s
```python3 make_img.py -h``` will give you all the available command line options.

```
$ python make_img.py -h
$ python make_img.py --help
usage: make_img.py [-h] [--path PATH] [--recursive]
[--num_process NUM_PROCESS] [--out OUT] [--size SIZE]
[--verbose] [--resize_opt {center,stretch}]
Expand All @@ -166,24 +166,27 @@ usage: make_img.py [-h] [--path PATH] [--recursive]
[--max_width MAX_WIDTH] [--freq_mul FREQ_MUL]
[--deterministic] [--dup DUP] [--salient]
[--lower_thresh LOWER_THRESH]
[--background BACKGROUND BACKGROUND BACKGROUND] [--exp]
[--background BACKGROUND BACKGROUND BACKGROUND]
[--blending {alpha,brightness}]
[--blending_level BLENDING_LEVEL] [--exp]
optional arguments:
-h, --help show this help message and exit
--path PATH Path to the tiles (default: img)
--recursive Whether to read the sub-folders for the specified path
(default: False)
--num_process NUM_PROCESS
Number of processes to use when loading tiles
(default: 8)
--out OUT The filename of the output image (default: )
Number of processes to use when loading tile (default:
8)
--out OUT The filename of the output collage/photomosaic
(default: )
--size SIZE Size (side length) of each tile in pixels in the
resulting collage/photomosaic (default: 50)
--verbose Print progress message to console (default: False)
--resize_opt {center,stretch}
How to resize each tile so they become square
images. Center: crop a square in the center. Stretch:
stretch the tile (default: center)
How to resize each tile so they become square images.
Center: crop a square in the center. Stretch: stretch
the tile (default: center)
--ratio RATIO RATIO Aspect ratio of the output image (default: (16, 9))
--sort {none,bgr_sum,av_hue,av_sat,av_lum,rand,pca_bgr,pca_hsv,pca_lab,pca_gray,pca_lum,pca_sat,pca_hue,tsne_bgr,tsne_hsv,tsne_lab,tsne_gray,tsne_lum,tsne_sat,tsne_hue}
Sort method to use (default: bgr_sum)
Expand All @@ -210,18 +213,28 @@ optional arguments:
--freq_mul FREQ_MUL Frequency multiplier to balance tile fairless and
mosaic quality. Minimum: 0. More weight will be put on
tile fairness when this number increases. (default: 1)
--deterministic Do not randomize the tiles for unfair tile usage
(default: False)
--deterministic Do not randomize the tiles. This option is only valid
if unfair option is enabled (default: False)
--dup DUP Duplicate the set of tiles by how many times (default:
1)
--salient Make photomosaic for salient objects only (default:
False)
--lower_thresh LOWER_THRESH
The threshold for saliency detection, between 0.0 and
1.0 (default: 0.5)
The threshold for saliency detection, between 0.0 (no
object area = blank) and 1.0 (maximum object area =
original image) (default: 0.5)
--background BACKGROUND BACKGROUND BACKGROUND
Background color in RGB for non salient part of the
image (default: (255, 255, 255))
--blending {alpha,brightness}
The types of blending used. alpha: alpha
(transparency) blending. Brightness: blending of
brightness (lightness) channel in the HSL colorspace
(default: alpha)
--blending_level BLENDING_LEVEL
Level of blending, between 0.0 (no blending) and 1.0
(maximum blending). Default is no blending (default:
0.0)
--exp Do experiments (for testing only) (default: False)
```

Expand Down
11 changes: 5 additions & 6 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ def show_img(img: np.ndarray, printDone: bool = True) -> None:
# right panel ROW 0
file_path = StringVar()
file_path.set("N/A")
Label(right_panel, text="Path of source images:").grid(
row=0, columnspan=2, sticky="W", pady=(10, 2))
Label(right_panel, text="Path to tiles:").grid(row=0, columnspan=2, sticky="W", pady=(10, 2))

# right panel ROW 1
Label(right_panel, textvariable=file_path, wraplength=150).grid(
Expand Down Expand Up @@ -368,7 +367,7 @@ def action():
dest_img_path = StringVar()
dest_img_path.set("N/A")
dest_img = None
Label(right_col_opt_panel, text="Path of destination image: ").grid(
Label(right_col_opt_panel, text="Path to the target image: ").grid(
row=0, columnspan=2, sticky="W", pady=2)

# right collage option panel ROW 1:
Expand Down Expand Up @@ -408,7 +407,8 @@ def change_alpha(_=None):
show_img(mkg.alpha_blend(result_collage, dest_img, 1 - alpha_scale.get() / 100), False)

# right collage option panel ROW 3:
Label(right_col_opt_panel, text="Color Blend:").grid(row=3, column=0, sticky="W", padx=(0, 5))
LabelWithTooltip(right_col_opt_panel, text="Color Blend:", tooltip=mkg.PARAMS.blending.help).grid(
row=3, column=0, sticky="W", padx=(0, 5))

# right collage option panel ROW 4:
colorization_opt = StringVar()
Expand Down Expand Up @@ -464,8 +464,7 @@ def attach_uneven():
collage_even_panel.grid(row=11, columnspan=2, sticky="W")

# collage even panel ROW 0
Label(collage_even_panel, text="C Types: ").grid(
row=0, column=0, sticky="W")
Label(collage_even_panel, text="C Types: ").grid(row=0, column=0, sticky="W")
ctype = StringVar()
ctype.set("float32")
OptionMenu(collage_even_panel, ctype, "", *mkg.PARAMS.ctype.choices).grid(row=0, column=1, sticky="W")
Expand Down
22 changes: 18 additions & 4 deletions make_img.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,29 @@ class PARAMS:
# ---- unfair tile assginment options -----
unfair = _PARAMETER(type=bool, default=False,
help="Whether to allow each tile to be used different amount of times (unfair tile usage). ")
max_width = _PARAMETER(type=int, default=80, help="Maximum width of the collage. This option is only valid if unfair option is enabled")
max_width = _PARAMETER(type=int, default=80,
help="Maximum width of the collage. This option is only valid if unfair option is enabled")
freq_mul = _PARAMETER(type=int, default=1,
help="Frequency multiplier to balance tile fairless and mosaic quality. Minimum: 0. "
"More weight will be put on tile fairness when this number increases.")
deterministic = _PARAMETER(type=bool, default=False, help="Do not randomize the tiles for unfair tile usage")
deterministic = _PARAMETER(type=bool, default=False,
help="Do not randomize the tiles. This option is only valid if unfair option is enabled")

# --- fair tile assignment options ---
dup = _PARAMETER(type=int, default=1, help="Duplicate the set of tiles by how many times")

# ---- saliency detection options ---
salient = _PARAMETER(type=bool, default=False, help="Make photomosaic for salient objects only")
lower_thresh = _PARAMETER(type=float, default=0.5, help="The threshold for saliency detection, between 0.0 and 1.0")
lower_thresh = _PARAMETER(type=float, default=0.5,
help="The threshold for saliency detection, between 0.0 (no object area = blank) and 1.0 (maximum object area = original image)")
background = _PARAMETER(nargs=3, type=int, default=(255, 255, 255),
help="Background color in RGB for non salient part of the image")

# ---- blending options ---
blending = _PARAMETER(type=str, default="alpha", choices=["alpha", "brightness"],
help="The types of blending used. alpha: alpha (transparency) blending. Brightness: blending of brightness (lightness) channel in the HSL colorspace")
blending_level = _PARAMETER(type=float, default=0.0,
help="Level of blending, between 0.0 (no blending) and 1.0 (maximum blending). Default is no blending")

def bgr_sum(img: np.ndarray) -> float:
"""
Expand Down Expand Up @@ -766,7 +774,13 @@ def main(args):
else:
grid, sorted_imgs = calc_col_even(
dest_img, imgs, args.dup, args.colorspace, args.ctype, args.metric)
save_img(make_collage(grid, sorted_imgs, args.rev_row), args.out, "")

collage = make_collage(grid, sorted_imgs, args.rev_row)
if args.blending == "alpha":
collage = alpha_blend(collage, dest_img, 1.0 - args.blending_level)
else:
collage = lightness_blend(collage, dest_img, 1.0 - args.blending_level)
save_img(collage, args.out, "")


if __name__ == "__main__":
Expand Down

0 comments on commit 29222b4

Please sign in to comment.