Skip to content

Instantly share code, notes, and snippets.

@SubhadityaMukherjee
Last active January 21, 2020 16:22
Show Gist options
  • Save SubhadityaMukherjee/1165960d85b1e46092e87be868701f97 to your computer and use it in GitHub Desktop.
Save SubhadityaMukherjee/1165960d85b1e46092e87be868701f97 to your computer and use it in GitHub Desktop.
downres

Download

  • We first download the image
  • Resize it for faster computation
url = 'https://nicolekessler.files.wordpress.com/2013/04/hellish_demons.jpg?w=1024'
def download(url, max_dim=None):
    name = "demons.jpg"
    image_path = tf.keras.utils.get_file(name, origin=url)
    img = PIL.Image.open(image_path)
    if max_dim:
        img.thumbnail((max_dim, max_dim))
    return np.array(img)

Deprocess

def deprocess(img):
    img = 255 * (img + 1.0) / 2.0
    return tf.cast(img, tf.uint8)

Show

  • just a wrapper to convert the tensor into an array and display
def show(img):
    display.display(PIL.Image.fromarray(np.array(img)))
original_img = download(url, max_dim=500)
show(original_img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment