Add a random value from [-offset, offset] to each channel
(img, offset)
| 146 | |
| 147 | |
| 148 | def color_cast(img, offset): |
| 149 | '''Add a random value from [-offset, offset] to each channel''' |
| 150 | x = np.asarray(img, dtype='uint8') |
| 151 | x.flags.writeable = True |
| 152 | cast_value = [0, 0, 0] |
| 153 | for i in range(3): |
| 154 | r = random.randint(0, 1) |
| 155 | if r: |
| 156 | cast_value[i] = random.randint(-offset, offset) |
| 157 | for w in range(x.shape[0]): |
| 158 | for h in range(x.shape[1]): |
| 159 | for c in range(3): |
| 160 | if cast_value[c] == 0: |
| 161 | continue |
| 162 | v = x[w][h][c] + cast_value[c] |
| 163 | if v < 0: |
| 164 | v = 0 |
| 165 | if v > 255: |
| 166 | v = 255 |
| 167 | x[w][h][c] = v |
| 168 | new_img = Image.fromarray(x.astype('uint8'), 'RGB') |
| 169 | return new_img |
| 170 | |
| 171 | |
| 172 | def enhance(img, scale): |