1

I am working on changing the lightness of a grayscale image. However, I have problem using numpy array when I tried to add 100 to the each pixel value and set it to 255 when the value goes over 255

import numpy as np
import matplotlib.pyplot as plt
from scipy import misc

for i in range(0, img.shape[0]):
    for j in range(0, img.shape[1]):
        im2[i,j] = img[i,j] + 100
        if im2[i,j] > 255:
            im2[i,j] = 255     

plt.imshow(im2, cmap = 'gray')
plt.show()

these are the codes and I got an error message saying

if im2[i,j] > 255:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Can anyone help me out with it?

8
  • Is im2[i, j] a list? Commented Mar 29, 2018 at 14:17
  • @chrisz it's an array Commented Mar 29, 2018 at 14:19
  • 1
    Then that's your issue. You can't compare an array to a single value. Commented Mar 29, 2018 at 14:19
  • What are you trying to do with if im2[i, j] > 255: ? What's the purpose of this line ? Commented Mar 29, 2018 at 14:20
  • @chrisz you can compare arrays to a single value, but you get an array as a result Commented Mar 29, 2018 at 14:21

3 Answers 3

2

You can simply use the numpy clip function for this purpose:

im2 = np.clip(img + 100, 0, 255)

Explanation:
The first argument is the array you want to clip, in your case that is img with 100 added to each element. The second argument is the minnimum value, that should be 0. The third argument is the maximum value, in your case 255.

Sign up to request clarification or add additional context in comments.

2 Comments

The version using np should be prefered to my solution imo.
This won't work if - as is often the case - the dtype is uint8. Better: np.clip(img, None, 155) + 100
0

If you want, you can simplify the code is the following manner:

im2 = (np.asarray (img) + 100)
im2[im2>255] = 255

The first line will convert the img into numpy array (if img is list) and will add 255 in each element.

And the line will replace the value by 255 of im2 if it is greater than 255.

Comments

0

When you think about your error, The truth value of an array with more than one element is ambiguous, it seems logical.

You are trying to say Is my array (say [1, 2, 3]) superior to 255 ? Is its length above 255 ? Are you trying to know if the first element is superior ? The second ? The sum of the entire array? The root square of all the entire array ?

What you want to do, if I follow your comment correctly, is :

for idx in range(len(im2[i, j])):
    if im2[i, j][idx] > 255:
        im2[i, j][idx] = 255

7 Comments

Your answer makes it sound like im2[i,j] > 255 is the same as len(im2[i,j]) > 255
@FlyingTeller , do you mean considering the code, or with the 2nd paragraph ?
@IMCoins i want to compare the each single values in the array.
The start of the second paragraph makes it sound a bit like that. It might be helpful to point out that np.array([1,2,3,4]) > 2 will result in np.array([False, False, True, True]) and that one needs an additional for loop as you have suggested
@finites that is what the proposed for loop is for
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.