Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nagadomi committed Oct 17, 2024
2 parents 7e8e187 + 82b409e commit 31c0137
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 21 deletions.
7 changes: 7 additions & 0 deletions iw3/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def initialize_component(self):
name="chk_recursive")
self.chk_recursive.SetValue(False)

self.chk_exif_transpose = wx.CheckBox(self.pnl_file, label=T("EXIF Transpose"),
name="chk_exif_transpose")
self.chk_exif_transpose.SetValue(True)
self.chk_exif_transpose.SetToolTip(T("Transpose images according to EXIF Orientaion Tag"))

self.chk_metadata = wx.CheckBox(self.pnl_file, label=T("Add metadata to filename"),
name="chk_metadata")
self.chk_metadata.SetValue(False)
Expand All @@ -146,6 +151,7 @@ def initialize_component(self):
sublayout = wx.BoxSizer(wx.HORIZONTAL)
sublayout.Add(self.chk_resume, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.chk_recursive, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.chk_exif_transpose, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.chk_metadata, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.sep_image_format, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.lbl_image_format, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
Expand Down Expand Up @@ -1144,6 +1150,7 @@ def parse_args(self):
pad=pad,
rotate_right=rotate_right,
rotate_left=rotate_left,
disable_exif_transpose=not self.chk_exif_transpose.GetValue(),
vf=vf,
max_output_width=max_output_width,
max_output_height=max_output_height,
Expand Down
2 changes: 2 additions & 0 deletions iw3/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"Skip processing when the output file already exists": "出力ファイルが存在する場合に処理をスキップ"
"Process all subfolders": "すべてのサブフォルダを処理"
"Add metadata to filename": "ファイル名にメタデータを追加"
"EXIF Transpose": "EXIF回転補正"
"Transpose images according to EXIF Orientaion Tag": "EXIF Orientation Tagに従って画像を転置"
"Image Format": "画像フォーマット"
"Play": "再生"

Expand Down
8 changes: 5 additions & 3 deletions iw3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ def process_images(files, output_dir, args, depth_model, side_model, title=None)
loader = ImageLoader(
files=files,
load_func=load_image_simple,
load_func_kwargs={"color": "rgb"})
load_func_kwargs={"color": "rgb", "exif_transpose": not args.disable_exif_transpose})
futures = []
tqdm_fn = args.state["tqdm_fn"] or tqdm
pbar = tqdm_fn(ncols=80, total=len(files), desc=title)
Expand Down Expand Up @@ -864,7 +864,7 @@ def export_images(args):
loader = ImageLoader(
files=files,
load_func=load_image_simple,
load_func_kwargs={"color": "rgb"})
load_func_kwargs={"color": "rgb", "exif_transpose": not args.disable_exif_transpose})
futures = []
tqdm_fn = args.state["tqdm_fn"] or tqdm
pbar = tqdm_fn(ncols=80, total=len(files), desc="Images")
Expand Down Expand Up @@ -1408,6 +1408,8 @@ def __repr__(self):
help="rembg model type")
parser.add_argument("--rotate-left", action="store_true",
help="Rotate 90 degrees to the left(counterclockwise)")
parser.add_argument("--disable-exif-transpose", action="store_true",
help="Disable EXIF orientation transpose")
parser.add_argument("--rotate-right", action="store_true",
help="Rotate 90 degrees to the right(clockwise)")
parser.add_argument("--low-vram", action="store_true",
Expand Down Expand Up @@ -1718,7 +1720,7 @@ def iw3_main(args):
make_output_filename(args.input, args, video=False))
else:
output_filename = args.output
im, _ = load_image_simple(args.input, color="rgb")
im, _ = load_image_simple(args.input, color="rgb", exif_transpose=not args.disable_exif_transpose)
im = TF.to_tensor(im).to(args.state["device"])
output = process_image(im, args, depth_model, side_model)
make_parent_dir(output_filename)
Expand Down
26 changes: 17 additions & 9 deletions nunif/utils/pil_io.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from PIL import Image, ImageCms, PngImagePlugin, UnidentifiedImageError
from PIL import Image, ImageCms, PngImagePlugin, UnidentifiedImageError, ImageOps
import io
import struct
import base64
Expand Down Expand Up @@ -35,9 +35,12 @@ def convert_i2l(im):
return im.point(lambda i: i / 255).convert('L')


def _load_image(im, filename, color=None, keep_alpha=False, bg_color=255):
def _load_image(im, filename, color=None, keep_alpha=False, bg_color=255, exif_transpose=False):
meta = {"engine": "pil", "filename": filename}
im.load()
if exif_transpose:
ImageOps.exif_transpose(im, in_place=True)

meta["mode"] = im.mode

if im.mode in {"L", "I", "RGB", "P"}:
Expand Down Expand Up @@ -120,9 +123,11 @@ def _load_image(im, filename, color=None, keep_alpha=False, bg_color=255):
return im, meta


def _load_image_simple(filename, color="rgb", bg_color=255):
def _load_image_simple(filename, color="rgb", bg_color=255, exif_transpose=False):
im = Image.open(filename)
im.load()
if exif_transpose:
ImageOps.exif_transpose(im, in_place=True)

transparency = im.info.get('transparency')
if isinstance(transparency, bytes) or isinstance(transparency, int):
Expand All @@ -145,9 +150,10 @@ def _load_image_simple(filename, color="rgb", bg_color=255):
return im, {"filename": filename}


def load_image_simple(filename, color="rgb", bg_color=255):
def load_image_simple(filename, color="rgb", bg_color=255, exif_transpose=False, **kwargs):
try:
im, meta = _load_image_simple(filename, color, bg_color)
im, meta = _load_image_simple(filename, color=color, bg_color=bg_color,
exif_transpose=exif_transpose)
return im, meta
except UnidentifiedImageError:
return None, None
Expand All @@ -163,12 +169,13 @@ def load_image_simple(filename, color="rgb", bg_color=255):
return None, None


def load_image(filename, color=None, keep_alpha=False, bg_color=255):
def load_image(filename, color=None, keep_alpha=False, bg_color=255, exif_transpose=False, **kwargs):
assert (color is None or color in {"rgb", "gray"})
with open(filename, "rb") as f:
try:
im = Image.open(f)
return _load_image(im, filename, color=color, keep_alpha=keep_alpha, bg_color=bg_color)
return _load_image(im, filename, color=color, keep_alpha=keep_alpha, bg_color=bg_color,
exif_transpose=exif_transpose)
except UnidentifiedImageError:
return None, None
except Image.DecompressionBombError:
Expand All @@ -183,11 +190,12 @@ def load_image(filename, color=None, keep_alpha=False, bg_color=255):
return None, None


def decode_image(buff, filename=None, color=None, keep_alpha=False, bg_color=255):
def decode_image(buff, filename=None, color=None, keep_alpha=False, bg_color=255, exif_transpose=False, **kwargs):
with io.BytesIO(buff) as data:
try:
im = Image.open(data)
return _load_image(im, filename, color=color, keep_alpha=keep_alpha, bg_color=bg_color)
return _load_image(im, filename, color=color, keep_alpha=keep_alpha, bg_color=bg_color,
exif_transpose=exif_transpose)
except UnidentifiedImageError:
return None, None
except Image.DecompressionBombError:
Expand Down
4 changes: 2 additions & 2 deletions nunif/utils/wand_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
GAMMA_LCD = 45454


def decode_image(blob, filename=None, color=None, keep_alpha=False):
def decode_image(blob, filename=None, color=None, keep_alpha=False, **kwargs):
im = Image(blob=blob)

# normalize, build meta data
Expand Down Expand Up @@ -66,7 +66,7 @@ def decode_image(blob, filename=None, color=None, keep_alpha=False):
return im, meta


def load_image(filename, color=None, keep_alpha=False):
def load_image(filename, color=None, keep_alpha=False, **kwargs):
assert (color is None or color in {"rgb", "gray"})
with open(filename, "rb") as f:
return decode_image(f.read(), filename, color=color, keep_alpha=keep_alpha)
Expand Down
6 changes: 6 additions & 0 deletions waifu2x/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,15 @@ def initialize_component(self):
self.chk_recursive = wx.CheckBox(self.pnl_file, label=T("Process all subfolders"),
name="chk_recursive")
self.chk_recursive.SetValue(False)
self.chk_exif_transpose = wx.CheckBox(self.pnl_file, label=T("EXIF Transpose"),
name="chk_exif_transpose")
self.chk_exif_transpose.SetValue(True)
self.chk_exif_transpose.SetToolTip(T("Transpose images according to EXIF Orientaion Tag"))

sublayout = wx.BoxSizer(wx.HORIZONTAL)
sublayout.Add(self.chk_resume, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.chk_recursive, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
sublayout.Add(self.chk_exif_transpose, flag=wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)

layout = wx.GridBagSizer(vgap=4, hgap=4)
layout.Add(self.lbl_input, (0, 0), flag=wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
Expand Down Expand Up @@ -665,6 +670,7 @@ def on_click_btn_start(self, event):

rotate_right=rotate_right,
rotate_left=rotate_left,
disable_exif_transpose=not self.chk_exif_transpose.GetValue(),
vf=vf,
grain=(float(self.cbo_grain_noise.GetValue()) > 0.0 and self.chk_grain_noise.GetValue()),
grain_strength=float(self.cbo_grain_noise.GetValue()),
Expand Down
2 changes: 2 additions & 0 deletions waifu2x/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"Resume": "処理を再開"
"Skip processing when the output file already exists": "出力ファイルが存在する場合に処理をスキップ"
"Process all subfolders": "すべてのサブフォルダを処理"
"EXIF Transpose": "EXIF回転補正"
"Transpose images according to EXIF Orientaion Tag": "EXIF Orientation Tagに従って画像を転置"
"Play": "再生"

"Error": "エラー"
Expand Down
4 changes: 2 additions & 2 deletions waifu2x/models/winc_unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def _forward(self, x):

return z

def _forward_tile4x4(self, x):
def _forward_tile2x2(self, x):
tl = x[:, :, :64, :64]
tr = x[:, :, :64, -64:]
bl = x[:, :, -64:, :64]
Expand All @@ -260,7 +260,7 @@ def forward(self, x):
pass
else:
raise NotImplementedError()
return self._forward_tile4x4(x)
return self._forward_tile2x2(x)
else:
return self._forward(x)

Expand Down
7 changes: 5 additions & 2 deletions waifu2x/ui_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def process_images(ctx, files, output_dir, args, title=None):
os.makedirs(output_dir, exist_ok=True)
loader = ImageLoader(files=files, max_queue_size=128,
load_func=IL.load_image,
load_func_kwargs={"color": "rgb", "keep_alpha": True})
load_func_kwargs={"color": "rgb", "keep_alpha": True,
"exif_transpose": not args.disable_exif_transpose})
futures = []
with PoolExecutor(max_workers=cpu_count() // 2 or 1) as pool:
tqdm_fn = args.state["tqdm_fn"] or tqdm
Expand Down Expand Up @@ -232,6 +233,8 @@ def create_parser(required_true=True):
help="Rotate 90 degrees to the left(counterclockwise) (video only)")
parser.add_argument("--rotate-right", action="store_true",
help="Rotate 90 degrees to the right(clockwise) (video only)")
parser.add_argument("--disable-exif-transpose", action="store_true",
help="Disable EXIF orientation transpose")
parser.add_argument("--vf", type=str, default="",
help="video filter options for ffmpeg. (video only)")
parser.add_argument("--grain", action="store_true",
Expand Down Expand Up @@ -352,7 +355,7 @@ def waifu2x_main(args):
output_filename = args.output
if args.resume and path.exists(output_filename):
return
im, meta = IL.load_image(args.input, color="rgb", keep_alpha=True)
im, meta = IL.load_image(args.input, color="rgb", keep_alpha=True, exif_transpose=not args.disable_exif_transpose)
output = process_image(ctx, im, meta, args)
make_parent_dir(output_filename)
IL.save_image(output, filename=output_filename, meta=meta, format=fmt)
6 changes: 3 additions & 3 deletions waifu2x/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def fetch_image(request):
if not filename:
filename = str(uuid.uuid4())
im, meta = IL.decode_image(image_data, filename,
color="rgb", keep_alpha=True)
color="rgb", keep_alpha=True, exif_transpose=True)
else:
url = request.forms.get("url", "")
if url.startswith("http://") or url.startswith("https://"):
Expand All @@ -243,11 +243,11 @@ def fetch_image(request):
if image_data is not None:
logger.debug(f"fetch_image: load cache: {url}")
im, meta = IL.decode_image(image_data, filename,
color="rgb", keep_alpha=True)
color="rgb", keep_alpha=True, exif_transpose=True)
else:
image_data = fetch_url_file(url)
im, meta = IL.decode_image(image_data, filename,
color="rgb", keep_alpha=True)
color="rgb", keep_alpha=True, exif_transpose=True)
cache.set(key, image_data, expire=command_args.cache_ttl * 60)

if image_data is not None and meta is not None:
Expand Down

0 comments on commit 31c0137

Please sign in to comment.