zhangyuanwei / node-images

Cross-platform image decoder(png/jpeg/gif) and encoder(png/jpeg) for Nodejs
https://npmjs.org/package/images
MIT License
1.55k stars 205 forks source link

对同一个对象执行size()会导致画质降低 #215

Closed kajweb closed 4 years ago

kajweb commented 4 years ago

应该如何从源对象进行size()操作?

    reduceWidth( step=false ){
        if( !step )
            step = this.step;
        let imgSize = this.image.size();
        let newWidth = imgSize.width;
        if( Number.isFinite(step) ){
            newWidth -= step;
        } else {
            switch( step.substr(0,1) ){
                case '/':
                    newWidth /= parseInt(step.substr(1))
                    break;
                default:
                    newWidth -= step;
                    break;
            }
        }
        // this.image = deepCopy(this._image);
        this.image = images(this.obj);
        this.image.size( newWidth );
        return this;
    }
oxUnd commented 4 years ago

使用 resize

kajweb notifications@github.com 于2020年8月27日周四 上午3:01写道:

应该如何从源对象进行size()操作?

reduceWidth( step=false ){

  if( !step )

      step = this.step;

  let imgSize = this.image.size();

  let newWidth = imgSize.width;

  if( Number.isFinite(step) ){

      newWidth -= step;

  } else {

      switch( step.substr(0,1) ){

          case '/':

              newWidth /= parseInt(step.substr(1))

              break;

          default:

              newWidth -= step;

              break;

      }

  }

  // this.image = deepCopy(this._image);

  this.image = images(this.obj);

  this.image.size( newWidth );

  return this;

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/zhangyuanwei/node-images/issues/215, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAE2DGYL3Z3H5GXQYFVTDXTSCVLYBANCNFSM4QME3STQ .

kajweb commented 4 years ago

我这边测试过,使用resize后压缩的效果比size的效果还差。

kajweb commented 4 years ago

测试完整代码如下:

// 调整宽高

var images = require("images");
var path = require("path");
var fs = require("fs");

// let i = 2;
let image = images("in/3.jpg")
// image.save( "out/0.jpg", { quality : 50 })

let imgSize = image.size()

while( imgSize.width>0 || imgSize.height>0 ){
    let filename = `${imgSize.width}_${imgSize.height}.jpg`;
    console.time( filename );
    image.save( "out/"+filename, { quality : 100 })
    newWidth = imgSize.width - 100;
    if( newWidth>0 ){
                /*          image = images("in/3.jpg")       */
        image.resize( newWidth )
        imgSize = image.size()
    } else {
        imgSize = false;
    }
    console.timeEnd( filename );
}

需要在image.resize( newWidth )前重新执行images("in/3.jpg")才可以获得较清晰图片

oxUnd commented 4 years ago

你就用 resize 就行,算法是 PS 用的一个算法,整体效果还可以;

kajweb commented 4 years ago

ok~