Delete white background of images

Replace white pixels of all images with transparent. Requires Canvas support.

  1. // ==UserScript==
  2. // @name Delete white background of images
  3. // @description Replace white pixels of all images with transparent. Requires Canvas support.
  4. // @include http://devdb.ru/*
  5. // @grant none
  6. // @version 0.0.1.20140630035011
  7. // @namespace https://greatest.deepsurf.us/scripts/68
  8. // ==/UserScript==
  9. // History
  10. // 2013-10-23 : Created first version
  11.  
  12. var imgs = document.getElementsByTagName('img');
  13.  
  14. for (var i = 0;i<imgs.length;) {
  15. var node = imgs[i], newNode = document.createElement("canvas");
  16. node.parentNode.replaceChild(newNode, node);
  17. newNode.id = node.id;
  18. newNode.class = node.class;
  19. var width = node.width || node.clientWidth;
  20. var height = node.height || node.clientHeight;
  21. newNode.setAttribute('width', width);
  22. newNode.setAttribute('height', height);
  23.  
  24. var canvas = newNode.getContext("2d");
  25. canvas.drawImage(node, 0, 0);
  26. imageData = canvas.getImageData(0,0,width,height);
  27. index = 0;
  28. for (y = 0; y < height; y++) {
  29. for (x = 0; x < width; x++) {
  30. if (imageData.data[index] > 247 &&
  31. imageData.data[index+1] > 247 &&
  32. imageData.data[index+2] > 247)
  33. imageData.data[index+3] = 0;
  34. index += 4;
  35. }
  36. }
  37.  
  38. canvas.putImageData(imageData, 0, 0);
  39. }