1

I have a (numpy) array of pixels acquired as:

''' import numpy and matplotlib '''
image = Image.open('trollface.png', 'r')
width, height = image.size
pixel_values = list(image.getdata())


pixel_values = np.array(pixel_values).reshape((width, height, 3)) # 3 channels RGB
#height, width = len(pixel_values), len(pixel_values[0])

I need to compute digital negative of this image -

for y in range(0,height):
   for x in range(0,width):
       R,G,B = pixel_values[x,y]
       pixel_values[x,y] =(255 - R, 255 - G, 255 - B)

tried displaying image from above pixels with the help of this thread

plt.imshow(np.array(pixel_values).reshape(width,height,3))
plt.show()

But it just displays a blank (white) window, with this error in CLI:

4
  • The error message seems pretty self explanatory. The size of your reshaped array must be the same size as the original. What are the values of width and height? Commented Jan 8, 2018 at 9:17
  • let's say it is an image 210 by 210 px Commented Jan 8, 2018 at 9:19
  • I am really sorry for the typo, I actually tried to map the image into same width and height, but couldn't. Commented Jan 8, 2018 at 9:27
  • 1
    You are probably going to have to provide a Minimal, Complete, and Verifiable Example. Commented Jan 8, 2018 at 9:31

1 Answer 1

1

The aim here is to achieve a negative transformation of an image.

Pixel translations can be directly applied to the R, G, B band using Image.point method.

image = Image.open('trollface.png')

source = image.split()
r, g, b, a = 0, 1, 2, 3

negate = lambda i: 255 - i

transform = [source[band].point(negate) for band in (r, g, b)]
if len(source) == 4:  # should have 4 bands for images with alpha channel
    transform.append(source[a])  # add alpha channel

out = Image.merge(im.mode, transform)
out.save('negativetrollface.png')

EDIT using OP's procedure, you have:

im = Image.open('trollface.png')

w, h = im.size

arr = np.array(im)
original_shape = arr.shape


arr_to_dim = arr.reshape((w, h, 4))

# Note that this is expensive.
# Always take advantage of array manipulation implemented in the C bindings
for x in range(0, w):
    for y in range(0, h):
        r, g, b, a = arr_to_dim[x, y]
        arr_to_dim[x, y] = np.array([255 - r, 255 - g, 255 - b, a])


dim_to_arr = arr_to_dim.reshape(original_shape)

im = Image.fromarray(dim_to_arr)
out.save('negativetrollface.png')
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the suggestion, but I would appreciate it if you could help identify my mistake.
You need to reshape an ndim 2 array to ndim 3. What you're looking for is .reshape((width, height, 4))
tried setting 4 instead of 3 shows new error : plt.imshow(np.array(pixel_values).reshape((width,height,4))) ValueError: total size of new array must be unchanged
Please see edit. I was able to run this successfully.
this works [when I reshaped to w,h,3 instead of 4(no alpha)]! what do you mean implementing c bindings in this context?
|

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.