Add items to the list keeping the multidimensionality

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
iacoposk8
Posts: 1
Joined: Wed Nov 23, 2016 8:07 am

Add items to the list keeping the multidimensionality

Post by iacoposk8 » Wed Nov 23, 2016 8:14 am

Hi to all, In the code below I take a picture and convert it into a 3d array numpy (cordinates x, y of each pixel and color rgb) Then skim this array and sequentially extract of the matrices 5 x 5 x 3 (then arrays of 5 x 5 px most the rgb values 3) and insert them in an array

[code]from PIL import Image
import numpy as np

img = Image.open("img.jpg")
arr = np.array(img)

x = []
h,w,rgb = arr.shape
size = 5

for i in range(0,h):
for j in range(0,w):
part = arr[i:i+size,j:j+size]
if len(part)==size and len(part[0])==size:
x.append(part)[/code]

The result is something like:

[code]...
[[129, 166, 175],
[129, 166, 175],
[130, 167, 175],
[128, 166, 175],
[128, 166, 175]],

...

[[129, 166, 174],
[129, 166, 174],
[129, 166, 175],
[129, 166, 175],
[128, 166, 175]]], dtype=uint8), 129, 166, 175, array([[[129, 166, 175],
[128, 166, 175],
[128, 166, 175],
[128, 166, 175],
[128, 168, 176]],

...

[[129, 166, 174],
[129, 166, 175],
[129, 166, 175],
[128, 166, 175],
[129, 167, 176]]], dtype=uint8), 129, 166, 175, array([[[128, 166, 175],
[128, 166, 175],
[128, 166, 175],
[128, 168, 176],
[128, 168, 178]],

...

[[129, 166, 175],
[129, 166, 175],
[128, 166, 175],
[129, 167, 176],
[128, 168, 176]]], dtype=uint8), 128, 166, 175][/code]

While I would expect something like

[code]...
[[129, 166, 175],
[129, 166, 175],
[130, 167, 175],
[128, 166, 175],
[128, 166, 175]],

...

[[129, 166, 174],
[129, 166, 174],
[129, 166, 175],
[129, 166, 175],
[128, 166, 175]]], dtype=uint8), array([[[129, 166, 175],
[128, 166, 175],
[128, 166, 175],
[128, 166, 175],
[128, 168, 176]],

...

[[129, 166, 174],
[129, 166, 175],
[129, 166, 175],
[128, 166, 175],
[129, 167, 176]]], dtype=uint8), array([[[128, 166, 175],
[128, 166, 175],
[128, 166, 175],
[128, 168, 176],
[128, 168, 178]],

...

[[129, 166, 175],
[129, 166, 175],
[128, 166, 175],
[129, 167, 176],
[128, 168, 176]]], dtype=uint8)][/code]

So a matrix that is always 5 x 5 x 3 not in some one-dimensional points after the fact ", dtype=uint8)," I would expect a, array([[[128, 166, 175], and not a 128, 166, 175
thank you.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: Add items to the list keeping the multidimensionality

Post by deshipu » Wed Nov 23, 2016 9:24 pm

I didn't know there was numpy for MicroPython?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Add items to the list keeping the multidimensionality

Post by pythoncoder » Thu Nov 24, 2016 8:00 am

Or a PIL.
Peter Hinch
Index to my micropython libraries.

Post Reply