Tile four images together using Node.js and GraphicsMagick

前端 未结 1 1391
無奈伤痛
無奈伤痛 2021-02-01 08:01

I have four 256x256 px images: a.jpg, b.jpg, c.jpg and d.jpg. I would like to merge them together to produce 2x2 mosaic image. The resulting image should be also 256x256 px.

相关标签:
1条回答
  • 2021-02-01 08:36

    Found the solution! It seems that the public API of gm does not provide any proper methods for what I needed. The solution was to use not-so-public .in method which makes possible to insert custom GraphicsMagick arguments.

    The following code takes in four 256x256 images, merges them to 2x2 grid on 512x512 canvas, halves the size to 256x256 using fast linear interpolation and saves the result to output.jpg.

    var gm = require('gm');
    
    // a b c d  ->  ab
    //              cd
    gm()
        .in('-page', '+0+0')  // Custom place for each of the images
        .in('a.jpg')
        .in('-page', '+256+0')
        .in('b.jpg')
        .in('-page', '+0+256')
        .in('c.jpg')
        .in('-page', '+256+256')
        .in('d.jpg')
        .minify()  // Halves the size, 512x512 -> 256x256
        .mosaic()  // Merges the images as a matrix
        .write('output.jpg', function (err) {
            if (err) console.log(err);
        });
    
    0 讨论(0)
提交回复
热议问题