Skip to content

Commit

Permalink
Add:Apply Overray
Browse files Browse the repository at this point in the history
  • Loading branch information
new-sankaku committed Mar 22, 2024
1 parent 9f4728d commit 45cf91b
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 27 deletions.
113 changes: 106 additions & 7 deletions scripts/ImageManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ def revert_image():
resultInfomation = ImageProcessing.createInfomation( now_numbers_panel_json_path, now_working_number, now_working_max_number )
return previous_image, resultInfomation


def flipH_image(image_apply_component):
print("flipH_image image_apply_component:", type(image_apply_component))

Expand All @@ -184,13 +183,113 @@ def flipH_image(image_apply_component):
print("flipH_image error, not found image.")
return image_apply_component # 変更なしで返す

def apply_overray(image_apply_component, slider) :
print("apply_overray image_apply_component:", type(image_apply_component))
print("apply_overray slider :", type(slider))
# apply_overray image_apply_component: <class 'numpy.ndarray'>
# apply_overray slider : <class 'float'>
def resize_and_maintain_aspect_ratio(image, base_height, base_width, zoom_percentage):
"""
画像のサイズを変更し、アスペクト比を保ちます。
"""
height, width = image.shape[:2]
if height > width:
scale = base_height / height
else:
scale = base_width / width

# Zoom factorを考慮してscaleを調整
scale *= zoom_percentage / 100

new_width = int(width * scale)
new_height = int(height * scale)

resized_image = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
return resized_image





def apply_overray(work_img_component, xSlider, ySlider, zoomSlider):
print("apply_overray work_img_component:", type(work_img_component))

print("apply_overray xSlider:", xSlider)
print("apply_overray ySlider:", ySlider)
print("apply_overray zoomSlider:", zoomSlider)

global select_apply_image
global image_history
image_history.append(work_img_component)

# 画像をPIL.Imageオブジェクトに変換
work_image = Image.fromarray(work_img_component).convert("RGBA")
apply_image = Image.fromarray(select_apply_image).convert("RGBA")

# apply_imageのリサイズ(アスペクト比を維持)
ratio = max(work_image.width / apply_image.width, work_image.height / apply_image.height)
new_size = (int(apply_image.width * ratio), int(apply_image.height * ratio))
apply_image_resized = apply_image.resize(new_size, Image.ANTIALIAS)

# zoomSliderを適用してさらにリサイズ
zoom_factor = zoomSlider / 100
zoomed_size = (int(new_size[0] * zoom_factor), int(new_size[1] * zoom_factor))
apply_image_zoomed = apply_image_resized.resize(zoomed_size, Image.ANTIALIAS)

# apply_imageをwork_imageの指定位置に合成するための準備
# まずはwork_imageと同じサイズの透明画像を作成
overlay_image = Image.new("RGBA", work_image.size, (255, 255, 255, 0))

# overlay_image とapply_image_zoomedの縦横サイズを取得
overlay_height, overlay_width = overlay_image.size
apply_zoomed_height, apply_zoomed_width = apply_image_zoomed.size

# overlay_image とapply_image_zoomedの縦横サイズをprintで出力
print(f"overlay_image height_width {overlay_height}_{overlay_width}")
print(f"apply_image_zoomed height_width {apply_zoomed_height}_{apply_zoomed_width}")

print(f"xSlider_ySlider {xSlider}_{ySlider}")

x = int(overlay_height * (xSlider / 100))
y = int(overlay_width * (ySlider / 100))

# apply_image_zoomedの幅と高さを取得
apply_width, apply_height = apply_image_zoomed.size

# apply_image_zoomedの中心が(x, y)に来るように調整
adjusted_x = x - (apply_width // 2)
adjusted_y = y - (apply_height // 2)

# 調整した位置でapply_image_zoomedをoverlay_imageに貼り付ける
overlay_image.paste(apply_image_zoomed, (adjusted_x, adjusted_y), apply_image_zoomed)


# 最終的な画像を合成
final_image = Image.alpha_composite(work_image, overlay_image)

# 結果をnumpy配列に変換して返す
final_img_array = np.array(final_image)
return final_img_array

image_history.append(work_img_component)

print("apply_overray image_apply_component select_apply_image:", type(select_apply_image))
work_image = ImageProcessing.convertRGBA(work_img_component)
apply_image = ImageProcessing.convertRGBA(select_apply_image)

workHeight, workWidth = ImageProcessing.get_height_and_width(work_image)
applyHeight, applyWidth = ImageProcessing.get_height_and_width(apply_image)

#★apply_imageの縮尺を変更します。
#★apply_imageのサイズをwork_imageの縦横いずれか大きい方に合わせてアクセプト比を保ったままサイズを変更します。
#★変更したapply_imageのサイズをwork_imageのサイズのzoomSliderの数値%にします。
#★zoomSliderは80等の数値が入ります。
#★縮尺の変更では縦長、横長の画像に対応してください。

#★work_imageにapply_imageを上書きします。
#★上書き位置はX軸がxSlider、Y軸がySliderです。
#★上書き時にwork_imageの領域をはみ出ないように対応してください。
#★また、アルファブレンディングを行ってください。

#★出来上がった画像をfinal_img_arrayに入れて返します。
final_img_array = None
return final_img_array

return image_apply_component

def skip_apply_number() :
global now_numbers_panel_json_path
Expand Down
18 changes: 16 additions & 2 deletions scripts/ImageProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,28 @@ def warp_insert_image_improved(comic_img, insert_img, panel_coords, position_dro

return final_image

def convertRGBA( image ) :
if len(image.shape) == 2: # グレースケールの場合
def convertRGBA(image):
if len(image.shape) == 2: # Gray
image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGRA)
elif image.shape[2] == 3: # RGB
image = cv2.cvtColor(image, cv2.COLOR_RGB2RGBA)
elif image.shape[2] == 4: # RGBA
pass
else:
raise ValueError("Unsupported image format. Image must be grayscale, RGB, or RGBA.")

return image

def get_height_and_width(image):
if len(image.shape) == 2: # Gray
return image.shape
elif image.shape[2] == 3: # RGB
return image.shape[:2]
elif image.shape[2] == 4: # RGBA
return image.shape[:2]
else:
raise ValueError("Unsupported image format. Image must be grayscale, RGB, or RGBA.")

def extract_black_lines(comic_img):
"""
コミック画像から黒線の枠を抽出します。
Expand Down
72 changes: 54 additions & 18 deletions scripts/MangaMakerUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def on_ui_tabs():
skip_symbol = '\U000023e9' # ⏩
new_symbol = '\U0001F195' # 🆕
apply_symbol = '\U00002714' # ✔️
apply_over_lay_symbol = '\U0001F5D7' # 🗗
flip_H_symbol = '\U00002194' # ↔️
reverse_symbol = '\U0001F501' # 🔁
save_symbol = '\U0001F4BE' # 💾
Expand All @@ -92,7 +93,6 @@ def on_ui_tabs():
infomation = infomation + "4.Click Apply Image. "
gr.Markdown(f"<small>{infomation}</small>", show_label=False)
with gr.Row():

manga_panel_gallary = gr.Gallery(label='select manga panel image.',
object_fit="contain",
show_label=True,
Expand All @@ -108,18 +108,53 @@ def on_ui_tabs():
with gr.Row():
with gr.Row():
with gr.Row():
new_image_button = gr.Button(value=new_symbol+"New Image", size="sm", min_width=120)
skip_button = gr.Button(value=skip_symbol+"Skip Number", size="sm", min_width=120)
revert_button = gr.Button(value=reverse_symbol+"Revert Change", size="sm", min_width=120)
gr.Markdown("")
with gr.Group():
new_image_button = gr.Button(value=new_symbol+"New Image", size="sm")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Group():
skip_button = gr.Button(value=skip_symbol+"Skip Number", size="sm")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Group():
revert_button = gr.Button(value=reverse_symbol+"Revert Change", size="sm")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Group():
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Row():
# apply_overray_button = gr.Button(value="Apply Overray(Transparency)", size="sm", min_width=150)
apply_button = gr.Button(value=apply_symbol+"Apply Image", size="sm", min_width=120)
position_dropdown = gr.Dropdown(["Center", "Top-Left", "Top-Center", "Top-Right"
, "Bottom-Left", "Bottom-Center", "Bottom-Right"],
value="Center", show_label=False, info="Clip Position", interactive=True)
flipH_button = gr.Button(value=flip_H_symbol+"Flip Horizontal", size="sm", min_width=120)
gr.Markdown("")
with gr.Group():
apply_button = gr.Button(value=apply_symbol+"Apply Image", size="sm")
with gr.Accordion(label="Settings", open=True):
position_dropdown = gr.Dropdown(["Center", "Top-Left", "Top-Center", "Top-Right"
, "Bottom-Left", "Bottom-Center", "Bottom-Right"],
value="Center", show_label=False, info="Clip Position", interactive=True)
with gr.Group():
flipH_button = gr.Button(value=flip_H_symbol+"Flip Horizontal", size="sm")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Group():
apply_overray_button = gr.Button(value=apply_over_lay_symbol+"Apply Overray", size="sm")
with gr.Accordion(label="Settings", open=False):
xSlider = gr.Slider(info="x axis", value=75, show_label=False, interactive=True)
ySlider = gr.Slider(info="Y axis", value=75, show_label=False, interactive=True)
zoomSlider = gr.Slider(info="Zoom", value=80, show_label=False, interactive=True)
infomation = infomation + "x and y are the coordinates to which the center point of Apply Image is applied."
infomation = infomation + "It is assumed that transparent images will be used."
infomation = infomation + "The method for specifying x and y is difficult to use, so we may need to recreate it."
gr.Markdown(f"<small>{infomation}</small>", show_label=False)
with gr.Group():
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
gr.Markdown("")
with gr.Row():
with gr.Row():
work_img_component = gr.Image(interactive=False,
Expand All @@ -135,16 +170,17 @@ def on_ui_tabs():
image_mode="RGBA")
with gr.Row():
with gr.Row():
with gr.Row():
with gr.Column():
with gr.Group():
with gr.Row():
save_button = gr.Button(value=save_symbol+"Save Image", size="sm", min_width=70)
with gr.Row():
open_folder_button = ToolButton(folder_symbol, elem_id='MangaMaker_open_folder',
visible=not shared.cmd_opts.hide_ui_dir_config, tooltip="Open images output directory.")
with gr.Row():
gr.Markdown("")
with gr.Group():
gr.Markdown("")
with gr.Row():
with gr.Group():
gr.Markdown("")
with gr.Group():
gr.Markdown("")
with gr.Row():
str = ""
Expand All @@ -170,7 +206,7 @@ def on_ui_tabs():
new_image_button.click(fn=ImageManager.new_image, inputs=[], outputs=[work_img_component, infomationTextBox])
skip_button.click(fn=ImageManager.skip_apply_number, inputs=[], outputs=[infomationTextBox])
apply_button.click(fn=ImageManager.apply_image, inputs=[work_img_component, position_dropdown], outputs=[work_img_component, infomationTextBox])
# apply_overray_button.click(fn=ImageManager.apply_overray, inputs=[image_apply_component, slider], outputs=[image_apply_component] )
apply_overray_button.click(fn=ImageManager.apply_overray, inputs=[work_img_component, xSlider, ySlider, zoomSlider], outputs=[work_img_component] )
save_button.click(fn=ImageManager.save_image, inputs=[work_img_component], outputs=[infomationTextBox])

manga_panel_gallary.select(fn=ImageManager.on_manga_panel_gallary_selected, inputs=[], outputs=[infomationTextBox])
Expand Down

0 comments on commit 45cf91b

Please sign in to comment.