Limitation of minimum and maximum values in numpy

• How do we make sure he doesn't get zero when he adds value to the red picel channel?

``````import numpy as np
from PIL import Image
rd = int(input())
gd = int(input())
bd = int(input())
img1 = np.copy(Image.open("./Images_processing/image.jpg"))
img = img1.copy()
img[..., 0] += rd
img[..., 1] += gd
img[..., 2] += bd
a = np.where(img <= 255, img, 255)
b = np.where(a >= 0, a, 0)
img = Image.fromarray(b)
img.save("./Images_processing/image1.jpg")
``````

Here, I'm trying to change the color of the pickles so that if it was more than 255, it's 255, and if less than 0, it's still 0. But numpy changes these values (if grad 255 is 0, if grad 0, then 255). How could you fix that?

Example:

``````import numpy as np
from PIL import Image
rd = 255
gd = 0
bd = 0
img1 = np.copy(Image.open("frame.jpg"))
img = np.clip(img1, 0, 255)
print(img[-1], 'img')
img[..., 0] += rd
img[..., 1] += gd
img[..., 2] += bd
print(img[-1], 'img')
img = Image.fromarray(img)
img.save("image1.jpg")
print(b[-1], 'b')
img = Image.fromarray(b)
img.save("image1.jpg")
``````

Conclusion:

``````[[70 64 64]
[67 61 61]
[67 61 61]
...
[49 38 42]
[52 41 45]
[44 33 37]] img1
[[69 64 64]
[66 61 61]
[66 61 61]
...
[48 38 42]
[51 41 45]
[43 33 37]] img2
Process finished with exit code 0
``````

• Use the method. `numpy.clip()`:

``````import numpy as np
a = np.array([256, -1])
res = np.clip(a, 0, 255) # первый аргумент - массив, второй - минимальное,
# третий - максимальное значение в массиве
print(res)
``````

[255 0]

2

2

2

2

2

2

2

2

2

2

2

2

2

2

2