From fbabfe6b108d4b6d0f7bd69d5e12a55bd7440323 Mon Sep 17 00:00:00 2001 From: kmader Date: Sat, 27 Feb 2016 20:58:05 +0100 Subject: [PATCH 1/2] fixing grayscale image support --- fauxtograph/fauxtograph.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/fauxtograph/fauxtograph.py b/fauxtograph/fauxtograph.py index 3f29a263..ad16f399 100755 --- a/fauxtograph/fauxtograph.py +++ b/fauxtograph/fauxtograph.py @@ -238,6 +238,8 @@ def read(fname): return im/255. x_all = np.array([read(fname) for fname in tqdm.tqdm(filepaths)]) x_all = x_all.astype('float32') + if len(x_all.shape)<4: # for grayscale images + x_all = x_all.reshape(list(x_all.shape)+[1]) if self.mode == 'convolution': x_all = x_all.transpose(0, 3, 1, 2) print("Image Files Loaded!") @@ -659,6 +661,8 @@ def read(fname): return im/255. x_all = np.array([read(fname) for fname in tqdm.tqdm(filepaths)]) x_all = x_all.astype('float32') + if len(x_all.shape)<4: # for grayscale images + x_all = x_all.reshape(list(x_all.shape)+[1]) if self.mode == 'convolution': x_all = x_all.transpose(0, 3, 1, 2) print("Image Files Loaded!") @@ -1235,6 +1239,8 @@ def read(fname): return im/255. x_all = np.array([read(fname) for fname in tqdm.tqdm(filepaths)]) x_all = x_all.astype('float32') + if len(x_all.shape)<4: # for grayscale images + x_all = x_all.reshape(list(x_all.shape)+[1]) if self.mode == 'convolution': x_all = x_all.transpose(0, 3, 1, 2) print("Image Files Loaded!") @@ -1587,7 +1593,7 @@ def get_paths(directory): return fnames -def image_resize(file_paths, new_dir, width, height): +def image_resize(file_paths, new_dir, width, height, color = True): '''Resizes all images with given paths to new dimensions. Uses up/downscaling with antialiasing. @@ -1609,6 +1615,8 @@ def image_resize(file_paths, new_dir, width, height): os.makedirs(os.path.dirname(new_dir)) for f in tqdm.tqdm(file_paths): - img = Image.open(f).resize((width, height), Image.ANTIALIAS).convert('RGB') + img = Image.open(f).resize((width, height), Image.ANTIALIAS) + if color: img = img.convert('RGB') + else: img = img.convert('L') new = os.path.join(new_dir, os.path.basename(f)) img.save(new) From 21d63af10eb940da173f1781e79a435c6c8a48e8 Mon Sep 17 00:00:00 2001 From: kmader Date: Tue, 1 Mar 2016 11:27:56 +0100 Subject: [PATCH 2/2] added squeeze to imshow command so (wid,hei,1) get changed to (wid,hei) and shown correctly --- fauxtograph/fauxtograph.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fauxtograph/fauxtograph.py b/fauxtograph/fauxtograph.py index ad16f399..956703ab 100755 --- a/fauxtograph/fauxtograph.py +++ b/fauxtograph/fauxtograph.py @@ -1451,19 +1451,19 @@ def _plot_img(self, img_data, samples, img_path='./', epoch=1, batch=1, save_pic for i in range(width): plt.subplot(4, width, i+1) - plt.imshow(z[i]) + plt.imshow(z[i].squeeze()) plt.axis("off") for i in range(width): plt.subplot(4, width, width+i+1) - plt.imshow(z_rec[i]) + plt.imshow(z_rec[i].squeeze()) plt.axis("off") for i in range(width): plt.subplot(4, width, 2*width+i+1) - plt.imshow(x_pics[i]) + plt.imshow(x_pics[i].squeeze()) plt.axis("off") for i in range(width): plt.subplot(4, width, 3*width+i+1) - plt.imshow(y_pics[i]) + plt.imshow(y_pics[i].squeeze()) plt.axis("off") if save_pic: if img_path[-1] != '/':