1

I am using PIL in python to combine images vertically and horizontally from dermen's answer in here. However, I am getting a warning:

/home/ceren/Documents/Python/combine_image.py:17: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
  imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs ) )

I am a python beginner, so I cannot quite understand what to do to resolve this issue. I thought [f2, f4, f5] in the code is already a list. However, the warning says it is not?? I know it is not an essential thing to fix, but learning how to fix it would be nice.

What should I change in my code so that I do not receive that warning any further?

My code is below:

Inside main:

fcom1 = 'Image.png'
ci.combine_horizontal([f2, f4, f5], fcom1, date)

In image combining function:

def combine_horizontal(f, fcom, date):
    list_im = f
    imgs    = [ PIL.Image.open(i) for i in list_im ]
    min_shape = sorted( [(np.sum(i.size), i.size ) for i in imgs])[0][1]
    imgs_comb = np.hstack( (np.asarray( i.resize(min_shape) ) for i in imgs )    
    imgs_comb = PIL.Image.fromarray( imgs_comb)
    imgs_comb.save(fcom)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Ceren
  • 65
  • 5

1 Answers1

2

The warning message tells you exactly which line is causing the problem, and why.

np.hstack does not (or won't) accept generator expressions as an input. The following line contains a generator expression:

np.hstack(np.asarray( i.resize(min_shape)) for i in imgs)

You can make the problem go away by consuming the generator in a list comprehension:

np.hstack([np.asarray( i.resize(min_shape)) for i in imgs])

Or wrap it in a tuple (which would be my personal aesthetic preference:

np.hstack(tuple(np.asarray( i.resize(min_shape)) for i in imgs))
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • Ah, I thought that the problem came from the way I call the function. I understand now. I modified my code as you said and the issue has been solved. Thank you very much! – Ceren May 20 '21 at 18:46