pythonimagemagickwandmagickwand

Place image on another image with aspect fill


I'm using wand 0.4.1 and want to place an image on top another. The crux is that the image to be placed is lets say 400px wide but I want it to stretch to a different width, lets say 700px. The image should be stretched correctly with the aspect ratio staying the same.

I thought I could somehow do it with composite, but didn't know how to, since the only option I seem to be able to pass is top and left.

My current code is like this:

bg_image = open(bg_image_url, 'rb')
fg_image = open(fg_image_url, 'rb')
with Image(file=bg_image) as bg:
    with Image(file=fg_image) as fg:
        bg.composite(fg, left=100, top=100)
    bg.save(filename='composited_image.jpg')

How would I accomplish this with wand?


Solution

  • Wand's Transform is the method that I was looking for.

    This can be used just right before the composite:

    fg.transform(resize='300x') #resize 'fg' to a width of 300px 
                                #and height is set dynamically, 
                                #respecting the original aspect ratio