0

So far, i do what i want to do is as follows

ar2 = [[0 for t in range(maxy-miny)] for t in range(maxx-minx)]

for first,x in enumerate(range(minx, maxx)):
    for second,y in enumerate(range(miny, maxy)):
        ar2[first][second] = a[x][y]

io.imshow(ar2)

The problem is that it's really slow. So i'm considering numpy.

ar = np.zeros((maxx-minx, maxy-miny), dtype=np.ndarray) 

for first,x in enumerate(range(minx, maxx)):
    for second,y in enumerate(range(miny, maxy)):
        ar[first][second] = a[x][y] 

io.imshow(ar)

Though in the second case, the image won't show

TypeError: Image data can not convert to float

I tried to check what might be going on, so i tested ar[0] & ar2[0]

Output of 2-d Array :

[array([230, 197, 204], dtype=uint8) array([241, 209, 214], dtype=uint8)
array([233, 201, 206], dtype=uint8) array([214, 183, 188], dtype=uint8)...

Output of Numpy Array :

[array([230, 197, 204], dtype=uint8), array([241, 209, 214], dtype=uint8),

array([233, 201, 206], dtype=uint8), ...

So apparently, numpy is using commas but i can't understand how and why it happens.

4
  • @AndyHayden It's an 2d array of the original image. I have found which parts interest me, and i want to transfer them alone to a new one. Commented Dec 20, 2014 at 20:49
  • As @sebix says it's not a proper 2D numpy array (the dtype is not correct). The spaces vs commas thing is a red herring, that is purely how the objects are printed. Commented Dec 20, 2014 at 20:54
  • @AndyHayden How can i provide more info? Which part could be done differently? my a is done as follows "a = io.imread('C:\Users\Dimitrios\Desktop\polimesa\\punk.jpg')" Commented Dec 20, 2014 at 20:56
  • I can't promise anything myself, but usually helpful to provide reproducible example (i.e. something we can try at home), in this case this means showing us a or at least the first few elements/rows/columns of a. Commented Dec 20, 2014 at 21:05

1 Answer 1

1

Your used dtype is not a valid number-like type, you used np.ndarray as type for individual values in your array.

import matplotlib.pyplot as plt
import numpy as np

ar = np.zeros((maxx-minx, maxy-miny))
a = plt.imread('something.jpg')

for first,x in enumerate(range(minx, maxx)):
    for second,y in enumerate(range(miny, maxy)):
        ar[first][second] = a[x][y] 

plt.imshow(ar)

This should work - however you haven't provided enough information to test that reliably.

I also recommend to use plt as abbreviation for matplotlib.pyplot instead of io, which can easily be mixed up with the actual io-module.

However, you can achieve the same goal but not looping at all:

ar3[0:maxx-minx, 0:maxy-miny] = a[minx:maxx,miny:maxy]
np.allclose(ar2, ar3)  # True
Sign up to request clarification or add additional context in comments.

2 Comments

i get "ValueError: setting an array element with a sequence" though . I also used "from skimage import io" before.
I cannot reproduce that. Please provide a fully reproducible code piece. I also added a solution which works without loops.

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.