1 import tensorflow as tf 2 import matplotlib.pyplot as plt 3 4 image_raw_data = tf.gfile.GFile("./picture.jpg", "rb").read() 5 with tf.Session() as sess: 6 """ 7 圖像編碼解碼處理 8 """ 9 # 解碼過程 10 img_data = tf.image.decode_jpeg(image_raw_data) 11 print(img_data.eval()) 12 13 # 編碼過程 14 encoded_image = tf.image.encode_jpeg(img_data) 15 with tf.gfile.GFile('./output.jpg', 'wb') as f: 16 f.write(encoded_image.eval()) 17 18 """ 19 圖像大小調整 20 """ 21 # 將圖片數據轉化爲實數類型,將0-255的像素值轉化爲0.0-1.0之間的實數 22 img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32) 23 24 # 第一個參數:原始圖像;第二個參數:調整後的圖像大小;第三個參數:method參數給出調整圖像大小的算法 25 resized = tf.image.resize_images(img_data, [300, 300], method=0) 26 27 # 目標尺寸小於原始尺寸,居中截取;否者填充 28 croped = tf.image.resize_image_with_crop_or_pad(img_data, 500, 300) 29 30 plt.figure() 31 plt.subplot(2, 2, 1) 32 plt.imshow(img_data.eval()) 33 plt.subplot(2, 2, 2) 34 plt.imshow(resized.eval()) 35 plt.subplot(2, 1, 2) 36 plt.imshow(croped.eval()) 37 38 """ 39 圖像翻轉 40 """ 41 # 上下翻轉 42 flipped_up_down = tf.image.flip_up_down(img_data) 43 # 50%的機率上下翻轉 44 flipped1 = tf.image.random_flip_up_down(img_data) 45 46 # 左右翻轉 47 flipped_left_right = tf.image.flip_left_right(img_data) 48 # 50%的機率左右翻轉 49 flipped2 = tf.image.random_flip_left_right(img_data) 50 51 # 沿對角線高翻轉 52 transpose_image = tf.image.transpose_image(img_data) 53 54 plt.figure() 55 plt.subplot(2, 2, 1) 56 plt.imshow(flipped_up_down.eval()) 57 plt.subplot(2, 2, 2) 58 plt.imshow(flipped_left_right.eval()) 59 plt.subplot(2, 2, 3) 60 plt.imshow(transpose_image.eval()) 61 plt.subplot(2, 2, 4) 62 plt.imshow(flipped2.eval()) 63 64 """ 65 圖像色彩調整 66 """ 67 # 亮度 -0.5 68 adjusted = tf.image.adjust_brightness(img_data, -0.5) 69 # 將其值截取在0.0-1.0之間,防止像素實數值超出0.0-1.0的範圍 70 adjusted_down = tf.clip_by_value(adjusted, 0.0, 1.0) 71 # 亮度 +0.5 72 adjusted_up = tf.image.adjust_brightness(img_data, 0.5) 73 # 在[-max_delta, max_delta]的範圍之間隨機調整圖像亮度 74 adjusted_random = tf.image.random_brightness(img_data, 0.2) 75 76 # 對比度 x0.5 77 adjusted1 = tf.image.adjust_contrast(img_data, 0.5) 78 # 對比度 增長5倍 79 adjusted2 = tf.image.adjust_contrast(img_data, 5) 80 # 在[lower, upper]的範圍內隨機調整圖像的對比度 81 adjusted3 = tf.image.random_contrast(img_data, 2, 4) 82 83 # 色相 +0.6 84 adjusted_hue1 = tf.image.adjust_hue(img_data, 0.6) 85 # 色相 -0.6 86 adjusted_hue2 = tf.image.adjust_hue(img_data, -0.6) 87 # 在[-max_delta, max_delta]的範圍內隨機調整圖像的色相,max_delta取值範圍[0, 0.5] 88 adjusted_hue3 = tf.image.random_hue(img_data, 0.3) 89 90 # 飽和度 -5 91 adjust_saturation1 = tf.image.adjust_saturation(img_data, -5) 92 # 飽和度 +5 93 adjust_saturation2 = tf.image.adjust_saturation(img_data, 5) 94 # 在[lower, upper]的範圍之間隨機調整圖像的飽和度,lower必須是非負值 95 adjust_saturation3 = tf.image.random_saturation(img_data, 0, 4) 96 plt.figure() 97 plt.subplot(4, 2, 1) 98 plt.imshow(adjusted_down.eval()) 99 plt.subplot(4, 2, 2) 100 plt.imshow(adjusted_up.eval()) 101 plt.subplot(4, 2, 3) 102 plt.imshow(adjusted1.eval()) 103 plt.subplot(4, 2, 4) 104 plt.imshow(adjusted2.eval()) 105 plt.subplot(4, 2, 5) 106 plt.imshow(adjusted_hue1.eval()) 107 plt.subplot(4, 2, 6) 108 plt.imshow(adjusted_hue2.eval()) 109 plt.subplot(4, 2, 7) 110 plt.imshow(adjust_saturation1.eval()) 111 plt.subplot(4, 2, 8) 112 plt.imshow(adjust_saturation2.eval()) 113 114 # 圖像數值標準化,均值爲0,方差爲1 115 adjust_standardization = tf.image.per_image_standardization(img_data) 116 plt.figure() 117 plt.imshow(adjust_standardization.eval()) 118 119 plt.show()