Skip to content

Instantly share code, notes, and snippets.

@kodekracker
Forked from SiddharthSudhakar/Crop_To_Bounded_Box
Created September 18, 2017 13:27
Show Gist options
  • Save kodekracker/8d9eb1fa782fbbd23fc28f15d1be2231 to your computer and use it in GitHub Desktop.
Save kodekracker/8d9eb1fa782fbbd23fc28f15d1be2231 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()
@hydra324
Copy link

hydra324 commented Jan 3, 2018

Hello, How do we save a batch of images? does tf.write_file work?

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