민서네집

[python] Image Crop/Resize , JPEG Image를 TensorFlow 입력으로 변환 본문

Python

[python] Image Crop/Resize , JPEG Image를 TensorFlow 입력으로 변환

브라이언7 2016. 8. 18. 07:19

from PIL import Image

new_width = 256
new_height = 256

image = Image.open("original.jpg")
width, height = image.size   # Get dimensions

left = (width - new_width)/2
top = (height - new_height)/2
right = (width + new_width)/2
bottom = (height + new_height)/2

image = image.crop((left, top, right, bottom))

image.save("cropped_image.jpg")

imageCrop.py



JPEG 이미지를 Tensorflow 입력으로 변환 (CIFAR10 참조) 
http://blog.naver.com/PostView.nhn?blogId=kjpark79&logNo=220787920384&redirect=Dlog&widgetTypeCall=true


JPEG 이미지를 Tensorflow 입력으로 변환 (MNIST 참조)

[출처

https://www.facebook.com/groups/TensorFlowKR/permalink/325407324466999


import tensorflow as tf
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
from PIL import Image

'''
Example: image resizing with TensorFlow API

[Reference]
https://github.com/thomaspark-pkj/tf-image-classification/blob/master/convert.py
http://stackoverflow.com/questions/20060096/installing-pil-with-pip

'''

cur_dir = os.getcwd()

FLAGS = tf.app.flags.FLAGS

FLAGS.height=256
FLAGS.width=256
FLAGS.depth=3

def convert_images(sess):
	value = tf.read_file('original.jpg')
	decoded_image = tf.image.decode_jpeg(value, channels=FLAGS.depth)
	resized_image = tf.image.resize_images(decoded_image, FLAGS.height, FLAGS.width)
	resized_image = tf.cast(resized_image, tf.uint8)

	try:
		image = sess.run(resized_image)
	except Exception as e:
		print e.message

	plt.imshow(np.reshape(image.data, [256, 256, FLAGS.depth]))
	plt.show()
	
	img = Image.fromarray(image, "RGB")
	img.save(os.path.join(cur_dir,"resized.jpg"))

def main(argv = None):
    with tf.Session() as sess:
        convert_images(sess)
		
if __name__ == '__main__':
    tf.app.run()

tensorflow_resize.py


Comments