Tensorflowでディレクトリ内にある画像を一括で水増ししたいのですが、このコードだと最後の画像のみしか水増しされません。for文をうまく使えば実行できると思うのですがどなたかご教授ください。
参考にしたサイト
このサイトと同じようなことがしたいです。
python
1# -*- coding:utf-8 -*- 2import tensorflow as tf 3import numpy as np 4import os 5from PIL import Image 6 7height = 80 8width = 80 9 10images = os.listdir("./data1/") 11array_list = [] 12 13for i in images: 14 if i == "Thumbs.db": 15 continue 16 17 image = np.array(Image.open("./data1/"+i)) 18 image_resize = image.reshape(image.size) 19 array_list.append(image_resize) 20 cropped = tf.random_crop(image, [height, width, 3]) 21 22with tf.Session() as sess: 23 coord = tf.train.Coordinator() 24 threads = tf.train.start_queue_runners(coord=coord) 25 26for i in range(5): 27 img = sess.run(cropped) 28 print(img.shape) 29 Image.fromarray(np.uint8(img)).save('./data1/img{}.jpg'.format(i))
変更後のプログラム
python
1# -*- coding:utf-8 -*- 2import tensorflow as tf 3import numpy as np 4import os 5from PIL import Image 6 7height = 80 8width = 80 9 10images = os.listdir("./data1/") 11array_list = [] 12 13for i in images: 14 if i == "Thumbs.db": 15 continue 16 17 image = np.array(Image.open("./data1/"+i)) 18 array_list.append(i) 19 cropped = tf.random_crop(image, [height, width, 3]) 20 21 with tf.Session() as sess: 22 coord = tf.train.Coordinator() 23 threads = tf.train.start_queue_runners(coord=coord) 24 25 for a in range(4): 26 image = sess.run(cropped) 27 print(image.shape) 28 Image.formarray(np.unit8(image)).save('./data1/img{}.jpg'.format(a))
実行結果
プログラムのエラーは発生しないが、ディレクトリ内の画像に変化なし
変更後のプログラム(2回目)
python
1... 2for i in images: 3 if i == "Thumbs.db": 4 continue 5 6 image = np.array(Image.open("./data1/"+i)) 7 array_list.append(i) 8 cropped = tf.random_crop(image, [height, width, 3]) 9 10 with tf.Session() as sess: 11 coord = tf.train.Coordinator() 12 threads = tf.train.start_queue_runners(coord=coord) 13 14 for a in range(4): 15 image = sess.run(cropped) 16 print(image.shape) 17 Image.fromarray(np.uint8(image)).save('./data1/img{}.jpg'.format(a))
実行結果
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
(80, 80, 3)
ファイル名が重複し画像が上書きされている。最終的に4枚の画像しか作成されない。
回答2件
あなたの回答
tips
プレビュー