GraphicsMagick for node not masking

天涯浪子 提交于 2019-12-23 06:02:29

问题


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.

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


回答1:


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()
        })
      }


来源:https://stackoverflow.com/questions/20828951/graphicsmagick-for-node-not-masking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!