|
1 | 1 | import sys as _sys |
2 | | -import numpy as _np |
3 | 2 |
|
4 | 3 |
|
5 | 4 | def printnow(fmt, *a, **kw): |
@@ -40,71 +39,3 @@ def finish(self): |
40 | 39 |
|
41 | 40 | def make_progressbar(prefix, data_size): |
42 | 41 | return SimpleProgressBar(data_size, prefix + ", processed {i} of {tot} ({pct:.2%})") |
43 | | - |
44 | | - |
45 | | -# Reading and resizing images |
46 | | -############################# |
47 | | - |
48 | | - |
49 | | -try: |
50 | | - import cv2 as _cv2 |
51 | | - |
52 | | - def imread(fname, dtype=_np.float32): |
53 | | - im = _cv2.imread(fname, flags=_cv2.IMREAD_UNCHANGED) |
54 | | - if im is None: |
55 | | - raise IOError("Couldn't open image file {}".format(fname)) |
56 | | - return im.astype(dtype) |
57 | | - |
58 | | - def imresize(im, h, w): |
59 | | - if im.shape[:2] == (h, w): |
60 | | - return im |
61 | | - |
62 | | - # Use AREA interpolation as soon as one dimension gets smaller to avoid moiree. |
63 | | - inter = _cv2.INTER_AREA if im.shape[0] < h or im.shape[1] < w else _cv2.INTER_LINEAR |
64 | | - return _cv2.resize(im, (w,h), interpolation=inter) |
65 | | - |
66 | | -except ImportError: |
67 | | - |
68 | | - try: |
69 | | - # This is what scipy's imread does lazily. |
70 | | - from PIL import Image as _Image |
71 | | - |
72 | | - def imread(fname, dtype=_np.float32): |
73 | | - # This does what CV_LOAD_IMAGE_ANYDEPTH does by default. |
74 | | - return _np.array(_Image.open(fname), dtype=dtype) |
75 | | - |
76 | | - def imresize(im, h, w): |
77 | | - if im.shape[:2] == (h, w): |
78 | | - return im |
79 | | - |
80 | | - # This has problems re-reading a numpy array if it's not 8-bit anymore. |
81 | | - assert im.max() > 1, "PIL has problems resizing images after they've been changed to e.g. [0-1] range. Either install OpenCV or resize right after reading the image." |
82 | | - img = _Image.fromarray(im.astype(_np.uint8)) |
83 | | - return _np.array(img.resize((w,h), _Image.BILINEAR), dtype=im.dtype) |
84 | | - |
85 | | - except ImportError: |
86 | | - |
87 | | - def imread(fname, dtype=None): |
88 | | - raise ImportError( |
89 | | - "Neither OpenCV nor the Python Imaging Library (PIL) is " |
90 | | - "installed. Please install either for loading images." |
91 | | - ) |
92 | | - |
93 | | - def imresize(im, h, w): |
94 | | - raise ImportError( |
95 | | - "Neither OpenCV nor the Python Imaging Library (PIL) is " |
96 | | - "installed. Please install either for resizing images." |
97 | | - ) |
98 | | - |
99 | | - |
100 | | -def imresizecrop(img, size): |
101 | | - assert not isinstance(size, tuple), "For now, `size` needs to be a single integer, i.e. it's being squared." |
102 | | - |
103 | | - # First, resize the smallest side to `size`. |
104 | | - img = imresize(img, h=img.shape[0]*size//min(img.shape[:2]), |
105 | | - w=img.shape[1]*size//min(img.shape[:2])) |
106 | | - |
107 | | - # Then, crop-out the central part of the largest side. |
108 | | - return img[(img.shape[0]-size)//2:img.shape[0]-(img.shape[0]-size)//2, |
109 | | - (img.shape[1]-size)//2:img.shape[1]-(img.shape[1]-size)//2, |
110 | | - :] |
0 commit comments