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