]> git.sesse.net Git - voxel-flow/blob - utils/image_utils.py
Initial commit
[voxel-flow] / utils / image_utils.py
1 import numpy as np
2 import scipy as sp
3 from PIL import Image
4 from scipy import misc
5
6
7 def imread(filename):
8   """Read image from file.
9   Args:
10     filename: .
11   Returns:
12     im_array: .
13   """
14   im = sp.misc.imread(filename)
15   # return im / 255.0
16   return im / 127.5 - 1.0
17
18
19 def imsave(np_image, filename):
20   """Save image to file.
21   Args:
22     np_image: .
23     filename: .
24   """
25   # im = sp.misc.toimage(np_image, cmin=0, cmax=1.0)
26   im = sp.misc.toimage(np_image, cmin=-1.0, cmax=1.0)
27   im.save(filename)
28
29 def imwrite(filename, np_image):
30   """Save image to file.
31   Args:
32     filename: .
33     np_image: .
34   """
35   # im = sp.misc.toimage(np_image, cmin=0, cmax=1.0)
36   im = sp.misc.toimage(np_image, cmin=-1.0, cmax=1.0)
37   im.save(filename)
38
39 def imwrite_batch(filenames, np_images):
40   """Save batch images to file.
41   Args:
42     filenames: 
43   """
44   #TODO
45   pass 
46
47 def imresize(np_image, new_dims):
48   """Image resize similar to Matlab.
49
50   This function resize images to the new dimension, and properly handles
51   alaising when downsampling.
52   Args:
53     np_image: numpy array of dimension [height, width, 3]
54     new_dims: A python list containing the [height, width], number of rows, columns.
55   Returns:
56     im: numpy array resized to dimensions specified in new_dims.
57   """
58   # im = np.uint8(np_image*255)
59   im = np.uint8((np_image+1.0)*127.5)
60   im = Image.fromarray(im) 
61   new_height, new_width = new_dims
62   im = im.resize((new_width, new_height), Image.ANTIALIAS)
63   return np.array(im)