0. 前言

本篇博客主要总结一些图片处理相关的API。

1. 图片处理API

  • 导入图片数据
# 1. 导入图片数据
image_path = "data/girl.png"
file_contents = tf.compat.v1.read_file(image_path)
image_tensor = tf.image.decode_png(contents=file_contents, channels=0)
# image_tensor = tf.image.decode_image(contents=file_contents, channels=0)

此处的decode_png根据图片本身格式的不同进行调整,通用的为decode_image,即不明确格式的情况下使用;image_tensor = tf.image.decode_png(contents=file_contents, channels=0)是对读入的图片进行编码,channels默认是0,即以原始图片的通道为准,此处的channels若设置为1,则根据下面展示图片的函数if len(image.shape) == 3 and image.shape[2] == 1:,最终就会显示成黑白图片;channels:0、1、3(RGB)、4(RGBA)其中A表示透明度

关于 decode_*

  • decode_bmp, decode_jpeg and decode_png,,都返回一个3维数组,[height, width, num_channels]:宽度、高度、通道数;
  • decode_gif 动图返回4维数组 [num_frames, height, width, 3]
    :帧数、宽度、高度、通道数
  • 展示图片
    此处将图片展示写成一个函数
def image_tensor_show(image_tensor):
    """展示图片"""
    print(image_tensor)
    image = image_tensor.eval()
    print(image)
    print("图片的shape:{}".format(image.shape))
    if len(image.shape) == 3 and image.shape[2] == 1:
        # 黑白图片
        plt.imshow(image[:, :, 0], cmap='Greys_r')
        plt.show()
    elif len(image.shape) == 3:
        # 彩色图片
        plt.imshow(image)
        plt.show()
# 需要使用交互式会话
sess = tf.compat.v1.InteractiveSession()
# 2. 展示图片
image_tensor_show(image_tensor)

以上代码,运行之后:
在这里插入图片描述
在这里插入图片描述
700 分别表示该图片的高度和宽度,3是颜色通道,即 RGB

  • 改变图片大小
# 3. 改变图片大小
resize_image_tensor = tf.compat.v1.image.resize_images(images=image_tensor, size=(300, 300), method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
image_tensor_show(resize_image_tensor)

在这里插入图片描述
此处还是对上面的图片进行一个改变,原始的图片是(700,700,3),这里将其size改为size=(300, 300)method表示用什么方法来进行改变,这里的NEAREST_NEIGHBOR是最邻近插值(一般选择这个,失真最小),tf.image.ResizeMethod.BILINEAR是线性插值,失真比较严重;

  • 剪切及填充图片
crop_or_pad_image_tensor = tf.image.resize_with_crop_or_pad(image=image_tensor, target_height=300, target_width=300)
image_tensor_show(crop_or_pad_image_tensor)

设置剪切的对象,剪切后的高度和宽度tf.image.resize_with_crop_or_pad(image=image_tensor, target_height=300, target_width=300)
在这里插入图片描述
上边是对其进行裁剪,现在更改高度和宽度,使其大于原始图片本身的大小,就是要对其进行填充

crop_or_pad_image_tensor = tf.image.resize_with_crop_or_pad(image=image_tensor, target_height=1000, target_width=1000)
image_tensor_show(crop_or_pad_image_tensor)

在这里插入图片描述

central_crop_image_tensor = tf.image.central_crop(image=image_tensor, central_fraction=0.7)
image_tensor_show(central_crop_image_tensor)

裁剪图片的中心区域,central_fraction=0.7 是比例因子

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐