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.
Add items to the list keeping the multidimensionality
Re: Add items to the list keeping the multidimensionality
I didn't know there was numpy for MicroPython?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: Add items to the list keeping the multidimensionality
Or a PIL.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.