Skip to content

Instantly share code, notes, and snippets.

@SiddharthSudhakar
Created August 27, 2017 19:49
Show Gist options
  • Save SiddharthSudhakar/895731b98f4870bb71a675c181733ce6 to your computer and use it in GitHub Desktop.
Save SiddharthSudhakar/895731b98f4870bb71a675c181733ce6 to your computer and use it in GitHub Desktop.
Tensorflow Crop to bounded box and save output image file
import tensorflow as tf
# Import the image
image = tf.image.decode_png(tf.read_file("./data/Input_image.png"), channels=1)
## Set the variable values here
# Offset variables values
offset_height= 20
offset_width = 20
# Target variables values
target_height = 20
target_width = 20
# Begin the session
session = tf.InteractiveSession()
print(session.run(image))
# Crop the image as per the parameters
cropped_image_tensor = tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
output_image = tf.image.encode_png(cropped_image_tensor)
# Create a constant as filename
file_name = tf.constant('./data/Ouput_image.png')
file = tf.write_file(file_name, output_image)
# print(session.run(file))
print("Image Saved!")
session.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment