tensorflow image
1.tensorflow read image
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw_data_jpg = tf.gfile.FastGFile('./img1.jpg', 'rb').read() #'r'的形式报错
image_raw_data_png = tf.gfile.FastGFile('./img1.png', 'rb').read()
with tf.Session() as sess:
img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg) #图像解码
img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)
img_data_png = tf.image.decode_png(image_raw_data_png)
img_data_png = tf.image.convert_image_dtype(img_data_png, dtype=tf.float32)
print(sess.run(img_data_png))
plt.figure(1)
plt.imshow(img_data_jpg.eval())
plt.figure(2)
plt.imshow(sess.run(img_data_png))
plt.show()
2.tensorflow encode image
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw_data_jpg = tf.gfile.FastGFile('img1.jpg', 'rb').read()
with tf.Session() as sess:
img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg) #解码
img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.uint8)
encode_image_jpg = tf.image.encode_jpeg(img_data_jpg) #jpg编码
encode_image_png = tf.image.encode_png(img_data_jpg) #png编码
with tf.gfile.GFile('encode_jpg.jpg', 'wb') as f:
f.write(encode_image_jpg.eval())
with tf.gfile.FastGFile('encode_png.png', 'wb') as f:#无阻赛以较快的方式获取文本操作句柄
f.write(sess.run(encode_image_png))
3.tensorflow resize image
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw_data_jpg = tf.gfile.FastGFile('img1.jpg', 'rb').read()
with tf.Session() as sess:
img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)
img_data_jpg = tf.image.convert_image_dtype(img_data_jpg,dtype=tf.float32)
resize0 = tf.image.resize_images(img_data_jpg, (200, 200), method=0)
resize1 = tf.image.resize_images(img_data_jpg, (300, 300), method=1)
resize2 = tf.image.resize_images(img_data_jpg, (400, 400), method=2)
resize3 = tf.image.resize_images(img_data_jpg, (500, 500), method=3)
print(img_data_jpg.get_shape())
print(resize0.get_shape())
print(resize1.get_shape())
print(resize2.get_shape())
print(resize3.get_shape())
plt.figure(0)
plt.imshow(img_data_jpg.eval())
plt.figure(1)
plt.imshow(resize0.eval())
plt.figure(2)
plt.imshow(resize1.eval())
plt.figure(3)
plt.imshow(resize2.eval())
plt.figure(4)
plt.imshow(resize3.eval())
plt.show()
"""
class ResizeMethod(object):
BILINEAR = 0
NEAREST_NEIGHBOR = 1
BICUBIC = 2
AREA = 3
0:双线性差值。1:最近邻居法。2:双三次插值法。3:面积插值法。
def resize_images(images,
size,
method=ResizeMethod.BILINEAR,
align_corners=False):
"""
C:\Python36\python.exe C:/tensorflow/imageresize.py
(?, ?, ?)
(200, 200, ?)
(300, 300, ?)
(400, 400, ?)
(500, 500, ?)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
4.tensorflow image crop or pad
import matplotlib.pyplot as plt
import tensorflow as tf
image_raw_data_jpg = tf.gfile.FastGFile('img1.jpg', 'rb').read()
with tf.Session() as sess:
img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)
img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, dtype=tf.float32)
crop = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 200, 200)
pad = tf.image.resize_image_with_crop_or_pad(img_data_jpg, 5000, 5000)
crop1 = tf.image.central_crop(img_data_jpg, 1) #原图
pad1 = tf.image.central_crop(img_data_jpg, 0.5) #原图的一半
plt.figure('crop')
plt.imshow(crop.eval())
plt.figure('pad')
plt.imshow(pad.eval())
plt.figure('crop1')
plt.imshow(crop1.eval())
plt.figure('pad1')
plt.imshow(pad1.eval())
plt.show()
5.色彩调整
import tensorflow as tf
import matplotlib.pyplot as plt
image_raw_data_jpg = tf.gfile.FastGFile('img1.jpg', 'rb').read()
with tf.Session() as sess:
img_data_jpg = tf.image.decode_jpeg(image_raw_data_jpg)
img_data_jpg = tf.image.convert_image_dtype(img_data_jpg, tf.float32)
img1 = tf.image.adjust_brightness(img_data_jpg, 0.5)#调整图像的亮度
img2 = tf.image.adjust_contrast(img_data_jpg, 5)#调整图像的对比度
img3 = tf.image.adjust_hue(img_data_jpg, 0.8)#调整色相
img4 = tf.image.adjust_saturation(img_data_jpg, 5)#调整饱和度
plt.figure('org')
plt.imshow(img_data_jpg.eval())
plt.figure('adjust_brightness')
plt.imshow(img1.eval())
plt.figure('adjust_contrast')
plt.imshow(img2.eval())
plt.figure('adjust_hue')
plt.imshow(img3.eval())
plt.figure('adjust_saturation')
plt.imshow(img4.eval())
plt.show()