-
Notifications
You must be signed in to change notification settings - Fork 22
/
image_augmentation.py
84 lines (67 loc) · 2.18 KB
/
image_augmentation.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright 2021 DeepMind Technologies Limited. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Examples of image augmentations with Pix."""
from absl import app
import dm_pix as pix
import jax.numpy as jnp
import numpy as np
import PIL.Image as pil
_KERNEL_SIGMA = 5
_KERNEL_SIZE = 5
_MAGIC_VALUE = 0.42
def main(_) -> None:
# Load an image.
image = _get_image()
# Flip up-down the image and visual it.
flip_up_down_image = pix.flip_up_down(image=image)
_imshow(flip_up_down_image)
# Apply a Gaussian filter to the image and visual it.
gaussian_blur_image = pix.gaussian_blur(
image=image,
sigma=_KERNEL_SIGMA,
kernel_size=_KERNEL_SIZE,
)
_imshow(gaussian_blur_image)
# Change image brightness and visual it.
adjust_brightness_image = pix.adjust_brightness(
image=image,
delta=_MAGIC_VALUE,
)
_imshow(adjust_brightness_image)
# Change image contrast and visual it.
adjust_contrast_image = pix.adjust_contrast(
image=image,
factor=_MAGIC_VALUE,
)
_imshow(adjust_contrast_image)
# Change image gamma and visual it.
adjust_gamma_image = pix.adjust_gamma(
image=image,
gamma=_MAGIC_VALUE,
)
_imshow(adjust_gamma_image)
# Change image hue and visual it.
adjust_hue_image = pix.adjust_hue(
image=image,
delta=_MAGIC_VALUE,
)
_imshow(adjust_hue_image)
def _get_image():
return jnp.array(pil.open("./assets/jax_logo.jpg"), dtype=jnp.float32) / 255.
def _imshow(image: jnp.ndarray) -> None:
"""Showes the input image using PIL/Pillow backend."""
image = pil.fromarray(np.asarray(image * 255.).astype(np.uint8), "RGB")
image.show()
if __name__ == "__main__":
app.run(main)