tensorflow裁剪填充指定大小图片(一)

来源:互联网 发布:淘宝上怎么卖点卡 编辑:程序博客网 时间:2024/05/17 23:04

简介

对于不同规则大小的图片,想要在不进行压缩的情况下对其进行裁剪或者填充背景颜色为黑色,在Python图像处理中找了半天,没找到相应的函数。。。后来发现可利用tensorflow中的:

tf.image.resize_image_with_crop_or_pad

示例代码

'''Created on 2017-8-16@author: XT'''import tensorflow as tfimport 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 = 360CP_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后
这里写图片描述

阅读全文
0 0
原创粉丝点击