1

I installed node-canvas and imagediff in a local directory and wrote up a test example that compares two already existing image files. It runs just fine with the code below:

var Canvas = require('canvas')
    , imagediff = require('../lib/imagediff.js')
    , fs = require('fs')
    , path = require('path');

var goodFilePath = path.join(__dirname + '/../diffs/single/test_1_good.png')
    , newFilePath = path.join(__dirname + '/../diffs/single/test_1_new.png')
    , diffFilePath = path.join(__dirname + '/../diffs/single/test_1_diff.png');

var test = function () {
    var img_good = new Canvas.Image()
            , img_new = new Canvas.Image()
            , img_good_data = null
            , img_new_data = null
            , tolerance = 10
            , equal = false
            , diff_result = null;

          fs.readFile(goodFilePath, function (err, squid_good) {
              if (err) throw err;  
              img_good.src = squid_good; 
          }); 
          fs.readFile(newFilePath, function (err, squid_new) {
            if (err) throw err;
            img_new.src = squid_new;
          });

          img_new.onload = function () {
            // convert to image data 
              img_good_data = imagediff.toImageData(img_good);
              img_new_data = imagediff.toImageData(img_new);

              //compare and save diffences in new image file
              equal = imagediff.equal(img_good, img_new, tolerance);
              console.log('Image Good === Image New: ' + equal);
              diff_result = imagediff.diff(img_good_data, img_new_data, tolerance);
              imagediff.imageDataToPNG(diff_result, diffFilePath);
          }          
}


test();

but if you noticed the require statement at the top I am pulling in the actual imagediff.js file that I copied into a lib folder that I created. My problem is that when I try to use the regular require('imagediff') statement the programs gives the following Image or Canvas expected error:

/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
    context.drawImage(image, 0, 0);
            ^
TypeError: Image or Canvas expected
    at toImageDataFromImage (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120:13)
    at toImageData (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:108:35)
    at Object.imagediff.toImageData (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:365:14)
    at img_new.onload (/Users/willko/Desktop/DEBUGGING_IMGDIFF/specs/simple.js:30:45)
    at Image.inspect [as src] (/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/canvas/lib/image.js:29:17)
    at /Users/willko/Desktop/DEBUGGING_IMGDIFF/specs/simple.js:25:29
    at fs.js:266:14
    at Object.oncomplete (fs.js:107:15)

I have even tried to install the same version of canvas as imagediff uses and it resulted in the following seg fault error:

/Users/willko/Desktop/DEBUGGING_IMGDIFF/node_modules/imagediff/imagediff.js:120
    context.drawImage(image, 0, 0);
            ^
[1]    34636 segmentation fault  node simple.js

My Question:

Why can't I use this module with the normal require? Or why is it working with the file path require statement but failing otherwise?

willko747
  • 517
  • 1
  • 6
  • 13
  • It'd be helpful if you could also include your package.json, so we can see versions, etc. – Brigand Jul 22 '14 at 18:27
  • I am pretty new to Node.js so I didn't create a package.json file. The current versions I am using are node: v0.10.28, npm: v1.4.10, node-canvas: v1.1.5, and imagediff: v1.0.6. However I tried with node-canvas: 1.0.4 and it also resulted in an error as mentioned above. – willko747 Jul 22 '14 at 22:06
  • follow this https://github.com/harthur/kittydar/issues/20 . Check the version of canvas you installed and the version of canvas in the imagediff and see if both are same and updated same in the package,json of your project. – user3050590 Apr 28 '15 at 18:14

0 Answers0