I have trimmed an image like this...
to minimise the black background around it using the PIL libary. As follows
But I need the result to maintain its thumbnail dimensions and maintain the aspect ratio and centring of the image. So in my example, (image2) I would need a little more height. I have tried to use the thumbnail function in PIL, opencv resize and also the imutils library without success. Keep in mind that the I have many of these images and the image of the Lungs in the middle will vary slightly in size so my trim function is dynamic to take care of that.