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

简介

对于不同规则大小的图片,想要在不进行压缩的情况下对其进行裁剪或者填充背景颜色为黑色,在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后
这里写图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

何以问天涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值