Batch copy from one image and paste in another

I’m right now in process of preparation of the translations to one of my clients’ sites. During this process, I have to generate tons of images for various languages. The source images are provided, and I have to copy part of it to my translated images.

The problem

The problem is – how to copy various areas of the source image and paste it to the destination. Ideally – with the least amount of effort. Luckily, for my source images, I have only three possible areas I have to take care of. One area on one type of image, one for the second type of image and one for the third type of image. I can prepare a list of images with the corresponding list of areas.

By the way, during this operation I will also reduce the size of the destination image, to match the source image size. So, my process looks more or less like this:

ImageMagick to help

You may know ImageMagick. This is a powerful tool to manipulate images. Since it works in the command line, it is easy to perform a batch job on all images I need to adjust.

The steps of the process will be:

  • load translated image
  • change the size of the translated image to match the source image
  • copy part of source image and paste it to the translated image
  • save the translated image

The command looks like this:

convert translated.jpg -resize 305x431 \( source.jpg -crop 90x100+205+50 +repage \) -geometry +205+50 -composite destination.jpg

Looking at the commands one by one:

  • -resize 305×431 resizes the translated image
  • the part in the parenthesis loads the source image and copies the area of size 90×100 px on the position of (205, 50) – interestingly, if you have multiple source images or multiple source areas, you can repeat the part in the parenthesis for each area you want to adjust
  • -geometry +205+50 -composite is pasting the copied area into the translated image

Once I have the list of images and locations to copy, it was easy to create a script to call ImageMagick with the proper parameters for each of them.