Skip to content

Commit

Permalink
Preserve aspect ratio of content and style image when loaded into VGG
Browse files Browse the repository at this point in the history
  • Loading branch information
titu1994 committed Nov 28, 2016
1 parent d861569 commit 6a08eaa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
7 changes: 5 additions & 2 deletions INetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def str_to_bool(v):
style_weights.append(style_weight * args.style_scale)

# dimensions of the generated picture.
img_width = img_height = args.img_size
img_width = img_height = 0

img_WIDTH = img_HEIGHT = 0
aspect_ratio = 0
Expand All @@ -136,7 +136,7 @@ def str_to_bool(v):

# util function to open, resize and format pictures into appropriate tensors
def preprocess_image(image_path, load_dims=False, read_mode="color"):
global img_WIDTH, img_HEIGHT, aspect_ratio
global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio

mode = "RGB" if read_mode == "color" else "L"
img = imread(image_path, mode=mode) # Prevents crashes due to PNG images (ARGB)
Expand All @@ -155,6 +155,9 @@ def preprocess_image(image_path, load_dims=False, read_mode="color"):
img_HEIGHT = img.shape[1]
aspect_ratio = img_HEIGHT / img_WIDTH

img_width = args.img_size
img_height = int(img_width * aspect_ratio)

img = imresize(img, (img_width, img_height)).astype('float32')

# RGB -> BGR
Expand Down
7 changes: 5 additions & 2 deletions Network.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def str_to_bool(v):
style_weights.append(style_weight * args.style_scale)

# dimensions of the generated picture.
img_width = img_height = args.img_size
img_width = img_height = 0

img_WIDTH = img_HEIGHT = 0
aspect_ratio = 0
Expand All @@ -134,7 +134,7 @@ def str_to_bool(v):

# util function to open, resize and format pictures into appropriate tensors
def preprocess_image(image_path, load_dims=False, read_mode="color"):
global img_WIDTH, img_HEIGHT, aspect_ratio
global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio

mode = "RGB" if read_mode == "color" else "L"
img = imread(image_path, mode=mode) # Prevents crashes due to PNG images (ARGB)
Expand All @@ -153,6 +153,9 @@ def preprocess_image(image_path, load_dims=False, read_mode="color"):
img_HEIGHT = img.shape[1]
aspect_ratio = img_HEIGHT / img_WIDTH

img_width = args.img_size
img_height = int(img_width * aspect_ratio)

img = imresize(img, (img_width, img_height)).astype('float32')

# RGB -> BGR
Expand Down
7 changes: 5 additions & 2 deletions script_helper/Script/INetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def str_to_bool(v):
style_weights.append(style_weight * args.style_scale)

# dimensions of the generated picture.
img_width = img_height = args.img_size
img_width = img_height = 0

img_WIDTH = img_HEIGHT = 0
aspect_ratio = 0
Expand All @@ -136,7 +136,7 @@ def str_to_bool(v):

# util function to open, resize and format pictures into appropriate tensors
def preprocess_image(image_path, load_dims=False, read_mode="color"):
global img_WIDTH, img_HEIGHT, aspect_ratio
global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio

mode = "RGB" if read_mode == "color" else "L"
img = imread(image_path, mode=mode) # Prevents crashes due to PNG images (ARGB)
Expand All @@ -155,6 +155,9 @@ def preprocess_image(image_path, load_dims=False, read_mode="color"):
img_HEIGHT = img.shape[1]
aspect_ratio = img_HEIGHT / img_WIDTH

img_width = args.img_size
img_height = int(img_width * aspect_ratio)

img = imresize(img, (img_width, img_height)).astype('float32')

# RGB -> BGR
Expand Down
7 changes: 5 additions & 2 deletions script_helper/Script/Network.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def str_to_bool(v):
style_weights.append(style_weight * args.style_scale)

# dimensions of the generated picture.
img_width = img_height = args.img_size
img_width = img_height = 0

img_WIDTH = img_HEIGHT = 0
aspect_ratio = 0
Expand All @@ -134,7 +134,7 @@ def str_to_bool(v):

# util function to open, resize and format pictures into appropriate tensors
def preprocess_image(image_path, load_dims=False, read_mode="color"):
global img_WIDTH, img_HEIGHT, aspect_ratio
global img_width, img_height, img_WIDTH, img_HEIGHT, aspect_ratio

mode = "RGB" if read_mode == "color" else "L"
img = imread(image_path, mode=mode) # Prevents crashes due to PNG images (ARGB)
Expand All @@ -153,6 +153,9 @@ def preprocess_image(image_path, load_dims=False, read_mode="color"):
img_HEIGHT = img.shape[1]
aspect_ratio = img_HEIGHT / img_WIDTH

img_width = args.img_size
img_height = int(img_width * aspect_ratio)

img = imresize(img, (img_width, img_height)).astype('float32')

# RGB -> BGR
Expand Down

0 comments on commit 6a08eaa

Please sign in to comment.