0

I am pretty much new with PIL for python and I was wondering how to control picture positions with PIL. For example I have 4 pictures.

list_im = ['test1.png', 'test2.png', 'test3.png', 'test4.png' ]
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( 'test.png')

This code returns 4 merged pictures in shape (1,4), what If I would like to have (2,2) or (1,3) or (3, 1)? Can't seem to understand the logic behind the code

Noob Programmer
  • 698
  • 2
  • 6
  • 22
  • I think the key is the `np.hstack` method. if you look it up, there might also be a `np.vstack` method (horizontal vs. vertical). If you want (2,2) you will need to first stack the pairs horizontally and then stack the result vertically – Magellan88 Sep 27 '19 at 12:50
  • You can do whatever you like! It depends on how you want to handle two images placed side-by-side that have different heights, and likewise how you want to handle two images that are different widths stacked above/below each other. It also depends whether you want the images abutted directly together, or you want spacers between them of some background colour or transparent spacers. In general you can align the tops, the middles or the bottoms of the images. This kind of thing is very easy to play around with using ImageMagick rather than writing code. – Mark Setchell Sep 27 '19 at 13:10
  • Add some images and a specific layout and I may be able to help you more. Also, say if ImageMagick is an option. https://stackoverflow.com/a/54260935/2836621 and also https://stackoverflow.com/a/40282074/2836621 – Mark Setchell Sep 27 '19 at 13:13
  • I figured something out, first got two pictures horizontaly and then combined it vertically. Does PIL has a function like plt has. Tightlayout(), because one picture is a bit covering the other. – Noob Programmer Sep 28 '19 at 10:46

0 Answers0