Domanda

I'm using GraphicsMagick for node to take a source image, resize, crop and then apply a mask:

      gm(tempfile)
        .quality(90)
        .resize(null, 38)
        .gravity('Center')
        .crop(20, 34)
        .mask('./public/assets/mask.png')
        .write(thumb, function (err) {
          if (err) throw err
          console.log('success')
        })

After running, The image is resized and cropped successfully, but the mask is not applied. No error is thrown (i.e. the console prints 'success').

Attached to this is also the mask image I'm trying to use. I want the image to draw only on the black part. I've tried using a transparent png (the gm docs say it masks based on alpha channel), as well as a black and white jpg, but the result is the same.

sample mask img

I'm sure I'm missing something obvious, but I'm stumped Thanks!

È stato utile?

Soluzione

So after another day or so on this I've figured it out:

Mask doesn't do anything on it's own, it's pretty useless really. It merely takes the supplied mask image and uses it to write protect the masked pixels from subsequent alteration if additional processing / drawing is performed on the image.

Because Node GM doesn't support composite, my solution is to apply the mask using a system call. Because there doesn't seem to be any way to combine cropping and composite in a single step (graphicsmagick composite and crop in the same command), I've done it in two steps:

      var exec = require('child_process').exec
      gm = require('gm')

      gm(tempfile)
        .quality(90)
        .resize(null, thumbOffset)
        .gravity('Center')
        .crop(thumbWidth, thumbHeight)
        .write(thumb, function (err) {
          if (err) throw err
          console.log('thumb sized')
          compositeMask(thumb, mask, function(){
            console.log('mask1 done')
          })
        })

      function compositeMask(thumb, mask, next) {
        var gmComposite = 'gm composite -compose in ' + thumb + ' ' + mask + ' ' + thumb
        exec(gmComposite, function(err) {
          if (err) throw err
          pathUpdate(entryID, { thumb: thumb })
          next()
        })
      }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top