import matplotlib.pyplot as plt
import tensorflow as tf
image_bytes = tf.gfile.FastGFile("11.jpg", 'rb').read() # 字节
with tf.Session() as session:
img = tf.image.decode_jpeg(image_bytes)#解码
# 给定截取框大小
bounding_boxes = tf.constant([[[0.31, 0.22, 0.46, 0.38]]]) # 设置一个RGB,设置四个角的比例位置
# 选择相关图像截取算法截图
# Bounding boxes are supplied and returned as `[y_min, x_min, y_max, x_max]`.
begin, size, bboxes = tf.image.sample_distorted_bounding_box(
tf.shape(img), bounding_boxes=bounding_boxes,
)
# 生成概要
# img_with_box = tf.image.draw_bounding_boxes(tf.expand_dims(tf.image.convert_image_dtype(img, dtype=tf.float32), 0), bboxes)
# tf.summary.image('img_with_box', img_with_box)
# print(begin.eval(), size.eval())
# 截图
distorted_img = tf.slice(img, begin, size)
img_array = distorted_img.eval()
plt.imshow(img_array)
plt.show()
随机截取图片