Skip to content

Instantly share code, notes, and snippets.

@thorstenwagner
Created August 10, 2020 08:04
Show Gist options
  • Save thorstenwagner/245d337eda9b0d0ef99d2d21d7721f3c to your computer and use it in GitHub Desktop.
Save thorstenwagner/245d337eda9b0d0ef99d2d21d7721f3c to your computer and use it in GitHub Desktop.
Filter
def filter_single_image(img_path, filter_cutoff, resize_to_shape=None):
try:
image = imagereader.image_read(img_path)
except Exception:
print(img_path + " is corrupted. Ignore it.")
return None
return filter_single_image_np(image,filter_cutoff,resize_to_shape)
def filter_single_image_np(image, filter_cutoff, resize_to_shape=None):
cutoff_factor = 1.0
if resize_to_shape is not None:
if not isinstance(resize_to_shape, (list, tuple)):
'Scale the short side to 1024'
# h w
ar = image.shape[0] / image.shape[1]
if ar < 1:
height = resize_to_shape
width = int(resize_to_shape / ar)
else:
height = int(resize_to_shape * ar)
width = resize_to_shape
resize_to_shape = [height, width]
cutoff_factor = image.shape[0] / resize_to_shape[0]
from PIL import Image
convert_to_array = Image.fromarray(image)
resized = convert_to_array.resize((resize_to_shape[1], resize_to_shape[0]),
resample=Image.BILINEAR)
image = np.asarray(resized)
mask_size_0 = next_power_of2(image.shape[0])
mask_size_1 = next_power_of2(image.shape[1])
filter_width = 2 * image.shape[1] * filter_cutoff * cutoff_factor
mask = window(filter_width, (mask_size_0, mask_size_1))
image_filtered = apply_fft_mask(image, mask)
del image
return image_filtered
@lucalusn
Copy link

def filter_single_image(img_path, filter_cutoff, resize_to_shape=None):
if isinstance(a, np.ndarray):
return filter_single_image_np(image,filter_cutoff,resize_to_shape)
try:
image = imagereader.image_read(img_path)

except Exception:
    print(img_path + " is corrupted. Ignore it.")
    return None
return filter_single_image_np(image,filter_cutoff,resize_to_shape)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment