简介
对于不同规则大小的图片,想要在不进行压缩的情况下对其进行裁剪或者填充背景颜色为黑色,在Python图像处理中找了半天,没找到相应的函数。。。后来发现可利用tensorflow中的:
tf.image.resize_image_with_crop_or_pad
示例代码
'''
Created on 2017-8-16
@author: XT
'''
import tensorflow as tf
import scipy.misc
#读取图像可任意大小
filenames = ['E://Parking186//ImgReshape//1Thumbnail25.jpg']
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
key,value = reader.read(filename_queue)
images = tf.image.decode_jpeg(value)#tf.image.decode_png(value)
CP_H = 360
CP_W = 300
# 裁切图片
with tf.Session() as sess:
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
reshapeimg = tf.image.resize_image_with_crop_or_pad(images,CP_H,CP_W)
#reimg1的类型是<class 'numpy.ndarray'>
reimg1 = reshapeimg.eval()
scipy.misc.imsave('E:\\Parking186\\ImgReshape\\crop_or_pad\\crop_or_pad.jpg', reimg1)
coord.request_stop()
coord.join(threads)
print('crop_or_pad successful!')
结果
原图
填充为宽300,高360后