-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored merge and added gif generation
- Loading branch information
1 parent
928ea3d
commit 0113329
Showing
7 changed files
with
103 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#from .stitching import * | ||
from .stitching import stitch | ||
from .merge_imgs import merge | ||
from .utils import read_img | ||
|
||
#__all__ = [] | ||
__all__ = [stitch, merge, read_img] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,37 @@ | ||
import os | ||
from PIL import Image | ||
from typing import Tuple | ||
from .utils import * | ||
|
||
def add_merge_args(parser): | ||
parser.add_argument('-s', '--stitching_result', | ||
help='Stitching result csv file', | ||
default='stitching_result.csv') | ||
parser.add_argument('-d', '--result_dir', | ||
help='Directory to save merged files', | ||
default='merged') | ||
parser.add_argument('-r', '--exclude_reverse', | ||
help='Whether to additionally include img2 on top of img1', | ||
action='store_true') | ||
return parser | ||
|
||
def merge_imgs(args, res_dir, img1, img2, dx, dy): | ||
if args.verbose: | ||
print('Merging:', img1, img2) | ||
i1, i2 = [Image.open(get_full_path(args,img)) for img in (img1, img2)] | ||
dx, dy = map(round_int, (dx, dy)) | ||
def merge(i1: Image, i2: Image, dx:int, dy:int) -> Tuple[Image, Image]: | ||
assert i1.size == i2.size, "Images must be same size!" | ||
W, H = i1.size | ||
new_W, new_H = W + abs(dx), H + abs(dy) | ||
|
||
i1_x = -dx if dx < 0 else 0 | ||
i1_y = -dy if dy < 0 else 0 | ||
i2_x = dx if dx > 0 else 0 | ||
i2_y = dy if dy > 0 else 0 | ||
|
||
res = Image.new(mode='RGB', size=(new_W, new_H)) | ||
res.paste(i1, (i1_x, i1_y)) | ||
res.paste(i2, (i2_x, i2_y)) | ||
res_path = os.path.join(res_dir, | ||
f'{os.path.splitext(img1)[0]}__{os.path.splitext(img2)[0]}.jpg') | ||
res.save(res_path) | ||
if not args.exclude_reverse: | ||
res.paste(i1, (i1_x, i1_y)) | ||
res.save(res_path[:-4] + '_r.jpg') | ||
|
||
res_r = res.copy() | ||
res_r.paste(i1, (i1_x, i1_y)) | ||
|
||
return res, res_r | ||
|
||
def merge_and_save(base_dir:str, res_dir:str, img_name1:str, img_name2:str, | ||
dx:int, dy:int, resize_factor:int=1, save_gif:bool=False): | ||
i1, i2 = [Image.open(get_full_path(base_dir, i)) for i in (img_name1, img_name2)] | ||
res = merge(i1, i2, dx, dy) | ||
W, H = res[0].size | ||
res_resized = [r.resize((W // resize_factor, H // resize_factor), Image.LANCZOS) | ||
for r in res] | ||
for i, r in enumerate(res_resized): | ||
base1, base2 = [os.path.splitext(n)[0] for n in (img_name1, img_name2)] | ||
r.save(get_full_path(res_dir, f'{base1}__{base2}_{i}.jpg')) | ||
if save_gif: | ||
res_resized[0].save(get_full_path(res_dir, f'{base1}__{base2}.png'), save_all=True, | ||
append_images=res_resized[1:], duration=500, loop=0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters