2

I am following a tutorial in image recognition https://www.youtube.com/watch?v=ry9AzwTMwJQ&list=PLQVvvaa0QuDffXBfcH9ZJuvctJV3OtB8A&index=9

But the code built is made to compare images that have an alpha value and the image I want to test doesn't have alpha value.

I have tried a lot of things, here is how my last try looks like :

from PIL import Image
import numpy as np 

i = Image.open('images/test.png')
iar = np.array(i)

def addAlpha(iar):
    b = []
    for eachRow in iar:
        b += [[255]]
    for eachRow in iar:
        eachRow = np.append(eachRow, b, axis= 1)

        print(eachRow)

    print (iar)

    return iar

iar = addAlpha(iar)

So when I print eachRow It looks like how I want it to be, but when I print iar nothing has changed, there are still only the RGB values.

I already thank you for any help and I apology for my bad English !

2
  • What is iar's shape? (Check iar.shape) Commented Apr 17, 2019 at 14:31
  • Printing iar.shape gives : (8, 8, 3) Commented Apr 17, 2019 at 14:39

1 Answer 1

1

Given that iar's shape is 8x8x3, we can tell we're working with an 8x8 image where each pixel has three channels (r, g, and b). We'd like to add a fourth channel for alpha, which will bring our shape to 8x8x4.

First, we create an array that contains all our alpha values:

alpha = 255 * np.ones((8, 8, 1))

Here, we create an 8x8 array of ones (with an extra dimension to make its axes line up with iar) and multiply it by 255 to give it the value we want.

Now, we can simply concatenate the two arrays:

iar = np.concatenate([iar, alpha], axis=2)

We concatenate along axis 2 which is what essentially lets us "paste" the alpha array to the back of iar, adding our fourth channel to the image.

Here it is in action.

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

1 Comment

Thanks for your reply, it did work well but it added some '.' before my values. I found a solution using this : iar = np.concatenate((iar, np.full( (8, 8, 1), 255, dtype= int)), axis = 2)

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.