2

This is my first time handling multidimensional arrays and I'm having problems accessing elements. I'm trying to get the red pixels of a picture but just the first 8 elements within the array. Here's the code

import Image
import numpy as np

im = Image.open("C:\Users\Jones\Pictures\1.jpg")
pix = im.load() 

r, g, b = np.array(im).T
print r[0:8]
3
  • 1
    I'm not sure on this either. Would it be r[0][:8]? Commented Feb 17, 2013 at 4:30
  • 1
    @kufudo: for the first 8 pixels in the first row, yes. (r[0,:8] is a slightly shorter numpy-specific syntax for this). Commented Feb 17, 2013 at 4:32
  • both solutions work thanks guys i cant believe it was that simple at least I got half of the right answer god bless you Commented Feb 17, 2013 at 4:36

5 Answers 5

3

Since you're dealing with images, r is a 2-D array. To get the first 8 pixels in the image, try

r.flatten()[:8]

This will wrap around automatically if the first row has less than 8 pixels.

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

2 Comments

hello can you help me on the second part please
Please post a new question.
1

do you want all rows too? Try this r[:,:8]

only want the first row? Try this r[0,:8]

1 Comment

hey thanks for the help can you please help me on the second part of the question
1

You can do it like this:

r[0][:8]

Note, however, that this will not work if the first row has less than 8 pixels. To fix that, do this:

from itertools import chain
r = list(chain.from_iterable(r))
r[:8]

or (if you don't want to import an entire module):

r = [val for element in r for val in element]
r[:8]

2 Comments

ok thanks alot so much for your help god bless you and your family
sorry to bother you again but is there some way I can replace the first 8 integers in the multidimensional array with 8 integers of array that I created for example: array=[0, 3, 38, 13, 7, 18, 3, 715] and replace the integers in the multidimensional array to make the multidimensional array to look like [[50 43 39 ..., 85 91 98] [40 34 32 ..., 73 92 93] [40 34 25 ..., 42 78 91] ..., [80 70 43 ..., 40 84 83] [86 75 42 ..., 42 90 85] [84 72 34 ..., 31 80 88]]
1

I think it could be more simple. This example uses a random matrix (this will be your r matrix):

In [7]: from pylab import *                 # convention

In [8]: r = randint(0,10,(10,10))           # this is your image

In [9]: r
array([[7, 9, 5, 5, 6, 8, 1, 4, 3, 4],
       [5, 4, 4, 4, 2, 6, 2, 6, 4, 2],
       [1, 4, 9, 9, 2, 6, 1, 9, 0, 6],
       [5, 9, 0, 7, 9, 9, 5, 2, 0, 7],
       [8, 3, 3, 9, 0, 0, 5, 9, 2, 2],
       [5, 3, 7, 8, 8, 1, 6, 3, 2, 0],
       [0, 2, 5, 7, 0, 1, 0, 2, 1, 2],
       [4, 0, 4, 5, 9, 9, 3, 8, 3, 7],
       [4, 6, 9, 9, 5, 9, 3, 0, 5, 1],
       [6, 9, 9, 0, 3, 4, 9, 7, 9, 6]])

Then, extract first 8 columns and do something

In [17]: r_8 = r[:,:8]              # extract columns

In [18]: r_8
Out[18]: 
array([[7, 9, 5, 5, 6, 8, 1, 4],
       [5, 4, 4, 4, 2, 6, 2, 6],
       [1, 4, 9, 9, 2, 6, 1, 9],
       [5, 9, 0, 7, 9, 9, 5, 2],
       [8, 3, 3, 9, 0, 0, 5, 9],
       [5, 3, 7, 8, 8, 1, 6, 3],
       [0, 2, 5, 7, 0, 1, 0, 2],
       [4, 0, 4, 5, 9, 9, 3, 8],
       [4, 6, 9, 9, 5, 9, 3, 0],
       [6, 9, 9, 0, 3, 4, 9, 7]])

In [19]: r_8 = r_8 * 2              # do something

In [20]: r_8
Out[20]: 
array([[14, 18, 10, 10, 12, 16,  2,  8],
       [10,  8,  8,  8,  4, 12,  4, 12],
       [ 2,  8, 18, 18,  4, 12,  2, 18],
       [10, 18,  0, 14, 18, 18, 10,  4],
       [16,  6,  6, 18,  0,  0, 10, 18],
       [10,  6, 14, 16, 16,  2, 12,  6],
       [ 0,  4, 10, 14,  0,  2,  0,  4],
       [ 8,  0,  8, 10, 18, 18,  6, 16],
       [ 8, 12, 18, 18, 10, 18,  6,  0],
       [12, 18, 18,  0,  6,  8, 18, 14]])

Now, this is the trick. Replace the first 8 columns in r using hstack:

In [21]: r = hstack((r_8, r[:,8:]))             # it replaces the FISRT 8 columns, note the indexing notation 

In [22]: r
Out[22]: 
array([[14, 18, 10, 10, 12, 16,  2,  8,  3,  4],    # it does not touch the last 2 columns
       [10,  8,  8,  8,  4, 12,  4, 12,  4,  2],
       [ 2,  8, 18, 18,  4, 12,  2, 18,  0,  6],
       [10, 18,  0, 14, 18, 18, 10,  4,  0,  7],
       [16,  6,  6, 18,  0,  0, 10, 18,  2,  2],
       [10,  6, 14, 16, 16,  2, 12,  6,  2,  0],
       [ 0,  4, 10, 14,  0,  2,  0,  4,  1,  2],
       [ 8,  0,  8, 10, 18, 18,  6, 16,  3,  7],
       [ 8, 12, 18, 18, 10, 18,  6,  0,  5,  1],
       [12, 18, 18,  0,  6,  8, 18, 14,  9,  6]])

Comments

0

EDIT: as to what DSM pointed out, OP is infact using a numpy array.

i retract my answer as nneonneo's correct

3 Comments

The OP is using a numpy array though, not a Python list, and ndarrays have lots of functionality that lists don't.
indeed he is. in that case nneonneo's answer is the one to go with indeed. thanks for pointing that out!
could you help me on the second part of my question please

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.