1

I want to combine 5 png figures into one png or jpg file using the code in this answer. I want the final figure to look like:

enter image description here

and the pngs shouldn't be resized. I'm using the concatenate function:

import numpy as np
from PIL import Image
imgs = ['pics/myfig1.png',
        'pics/myfig2.png',
        'pics/myfig3.png',
        'pics/myfig4.png',
        'pics/myfig5.png']
concatenated = Image.fromarray(
  np.concatenate(
    np.array([
        [imgs[0], imgs[1], imgs[2]],
        [imgs[3], imgs[4]]])
  )
)
concatenated.save( 'finalfig.jpg' )

But I get this error:

    raise TypeError("Cannot handle this data type: %s, %s" % typekey) from e
TypeError: Cannot handle this data type: (1, 1), <U44
Medulla Oblongata
  • 3,771
  • 8
  • 36
  • 75
  • 2
    hint: `np.array(imgs)` is an array of strings, not their array representations. Also, what shape is it supposed to have? – Marat Aug 13 '21 at 01:55
  • You did not follow the linked code. You need to learn more about thinks like Python strings (filenames), loaded images, and arrays. Learn to load an image and convert ito array. Then practice joining an array to itself, either side by side or top to bottom. Only then can you hope to make something larger. And the no-resizing requirement will probably have to be dropped. – hpaulj Aug 13 '21 at 03:57
  • you can try this: https://stackoverflow.com/a/68509737/5239109 – ffsedd Aug 13 '21 at 09:05

1 Answers1

0

Concatenate is an array manipulation. It combines multiple arrays into a single array. Each image can be seen as a array with the shape height x width x num_channels (not that height and width are swapped).

If I take 3 gray scale image 200 by 100 pixels I have 3 arrays of 100 x 200 x 1. If I Concatenate them I get 1 array of 100 x 200 x 3. This concatenated image can be a color (RGB) image of 200 x 100.

But if you have 5 image this is not possible because image are either gray (1), RGB (3) or RGBA (4).

If you want to past multiple images next to each other into a bigger image need image.paste for pillow image https://pillow.readthedocs.io/en/stable/reference/Image.html Or tomni.img_paste for opencv/numpy images

Tom Nijhof
  • 542
  • 4
  • 11