GraphicsMagick用于节点未屏蔽 [英] GraphicsMagick for node not masking

查看:104
本文介绍了GraphicsMagick用于节点未屏蔽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用),我分两步完成:

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

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


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!

解决方案

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

这篇关于GraphicsMagick用于节点未屏蔽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆