bamlab / generator-rn-toolbox

The React Native Generator to bootstrap your apps
MIT License
1.2k stars 119 forks source link

getPixelColor script not actually cropping before identify #226

Open felipewil opened 5 years ago

felipewil commented 5 years ago

From getPixelColor.js file:

...
gm(imagePath)
.crop(x, y)
.identify('%[hex:s]', (error, imageMagickColor) => { ... })
...

So, the code above is supposed to crop the given image and then execute the identify. But actually only the identify command is getting called.

Testing a bit and looking into the gm source, it seems that the crop function adds some params to an _out property but not execute it until write, stream or toBuffer is called.

To test it easier, I extracted those lines into a separate file. Here is what I tried:

const result = gm('some_image.png').crop(1, 1).identify((err, response) => {
  console.log(response);
});

With debug on, only the following command is executed: gm identify "-ping" "-verbose" "some_image.png"

The result var value is:

gm {
  ...
  _out: [ '-crop', '1x1+0+0' ],
  _subCommand: 'convert',
  source: 'some_image.png',
  ...
}

The crop command is still there, not executed. And the response from the identify:

{
  Format: 'PNG (Portable Network Graphics)',
  format: 'PNG',
  'Mime type': 'image/png',
  Class: 'DirectClass',
  Geometry: '2208x2208+0+0',
  size: { width: 2208, height: 2208 },
  ...
}

2208x2208 is the actual size of the image I used. Now, if I call toBuffer before identify, like:

const result = gm('some_image.png').crop(1, 1).toBuffer((err, buffer) => {
  gm(buffer).identify((err, response) => {
    console.log(response);
  });
});

Then both these command are executed:

gm convert "some_image.png" "-crop" "1x1+0+0" "-"
gm identify "-ping" "-verbose" "-"

Result var value:

gm {
  ...
  _out: [],
  _subCommand: 'convert',
  source: 'some_image.png',
  ...
}

Now _out is empty, the crop was executed. And the identify response is:

{
  'Base filename': '-',
  Format: 'PNG (Portable Network Graphics)',
  format: 'PNG',
  'Mime type': 'image/png',
  Class: 'PseudoClass',
  Geometry: '1x1+0+0',
  size: { width: 1, height: 1 }
  ...
}

Applying the change too getPixelColor.js it correctly crops before identifying.