tensorflow 图像数据处理

来源:互联网 发布:电脑的记事软件 编辑:程序博客网 时间:2024/05/18 07:38

一、图像解编码、尺寸调整

       TensorFlow提供了常用图片格式(包括png和jpeg)的解码和编码操作,下面用一个jpg的图像演示:

          import matplotlib.pyplotas plt
          import tensorflowas tf

          image_raw_data = tf.gfile.FastGFile(
'.//image//img.jpg','rb').read()

          with tf.Session()as sess:
               img_data = tf.image.decode_jpeg(image_raw_data)
               print(img_data.eval())

               plt.imshow(img_data.eval())
               plt.show()


              
img_data = tf.image.convert_image_dtype(img_data,dtype = tf.float32)

               encoded_image = tf.image.encode_jpeg(img_data)
              
with tf.gfile.GFile(".//image//img1.jpg","wb")as f:
                    f.write(encoded_image.eval())

    tf.image.decode_jpeg为jpg解码过程,tf.image.encode_jpeg为jpg编码过程。

图像尺寸调整有三个函数:tf.image.resize_imagestf.image.resize_image_with_crop_or_pad、tf.image.central_crop。


原创粉丝点击