Reputation: 855
So I have this code
from PIL import Image
def get_concat_h_cut(im1, im2):
dst = Image.new('RGB', (im1.width + im2.width,
min(im1.height, im2.height)))
dst.paste(im1, (0, 0))
dst.paste(im2, (im1.width, 0))
return dst
def get_concat_v_cut(im1, im2):
dst = Image.new(
'RGB', (min(im1.width, im2.width), im1.height + im2.height))
dst.paste(im1, (0, 0))
dst.paste(im2, (0, im1.height))
return dst
FileA = Image.open("dog.jpg")
FileB = Image.open("cat.jpg")
get_concat_v_cut(FileA, FileB).save('pillow_concat_v_cut.jpg')
But since the reolsution of the cat image is small, the program resizes the entire image, thus I can't see most of the dog image. How do I make it so I can see both the cat and dog?
Cat image: https://i.ibb.co/j58TRnt/cat.jpg
Dog image: https://i.ibb.co/d6jdsBC/dog.jpg
Image that the program generates: https://i.ibb.co/WkDdPTD/pillow-concat-v-cut.jpg
Thank you for your help.
Upvotes: 0
Views: 136
Reputation: 26
If you wish to keep the width of the dog image the same, which will fill in black space to the right of the cat image on the output image, you can change the size argument for Image.new in your concat function to
(max(im1.width, im2.width), im1.height+im2.height)
The same would apply to your h-cut concat function, just applied to the height argument.
If you're trying to scale them to the same size, you can invoke the .resize() method on whichever one you want to scale, and pass it the other image's height and width variables as it's size argument.
You can find more info here: https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.resize
Upvotes: 1