简体   繁体   中英

How to compose multiple rectangles of different sizes into one using Python?

What I Want to Do : I have a png image file, and I want to make multiple copies of it(each scaled to a random ratio and rotated to a random degree), finally I need to compose all the image objects into one big png file.

My Thought Process : After scaling and rotating the images, then I need to detect the minimum bounding box(rectangle) of each small png image object, then compose all the rectangles to one big rectangle(the final big png file).

My Question : I can't find a way to do the final process: composing.

Any help is appreciated. I'm relatively new to programming(I'm learning Python), so if you can give some details about how to do it, that would be best.

Thanks in advance.

So there are some details missing from the question here which make it difficult to answer. Firstly, the way that you're loading images. I'm going to assume that you're using the Pillow image library, but you could just as easily have used (eg) imageio to load the images into numpy arrays, or directly have read the files using the inbuilt open and put the results into a bunch of list s.

Secondly, there are a number of ways to "compose" images. You could mean create an output image with all of the input images arranged next to each other left-to-right. You could also mean create the smallest possible image that fits all of the rotated images together without overlap (a vastly more difficult task). It's also not clear whether the PNG images you're dealing with have an alpha channel (ie are partially transparent).

If you're using Pillow and you just need to arrange the images next to each other, then this link should answer your question.

Is that what you meant?

You should be able to use paste() with a mask like I do in this answer to composite your small images onto a background canvas.

When you rotate() your rectangles, you will need to set the fill colour as transparent else the empty areas around your rotated shape will show up as black... and clobber anything underneath when you paste() .

The best way for that is to use keras.preprocessing.image , in this answer I am using that you have already done the image augmentation that you mentioned so I will deal with "composing" answer for you.

Once you have have did the augmentation you want to use them in numpy array using img_to_array function which you can use as

from keras.preprocessing import image

# Say your image is stored in the variable img

x = image.img_to_array(img)

Once you do this for all basically you can then start to concatenate them say with some numpy.zeros for indentation.

Then use the function save_img directly on the numpy array to save this image to a path. This can be done as follows.

image.save_img('to the path you want', x)

You said that you are new to programming, so just for basic intro keras is actually used in the Artificial Intelligence for deep learning related stuff. These preprocessing functions are actually amazing, for such stuff. I am also attaching a screenshot of them in use.

使用中的 ResNeXt

Edit: Here is a useful link that I used long time ago to understand them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM