Guest User

Untitled

a guest
Jul 16th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // Compress and add javascript: prefix, then save as a bookmarklet
  2.  
  3. function process(image){
  4. h = image.height; w = image.width;
  5. var buffer = document.createElement("canvas"); // Anonymous canvas
  6. buffer.width = w;buffer.height=h;
  7. var ct = buffer.getContext('2d');
  8. ct.drawImage(image,0,0);
  9. var data = ct.getImageData(0,0,w,h).data; // This may raise an exception for external images.
  10. var pxs=2; // Height-Width of the pixel block
  11. var str='<p style = "line-height:6px;font-size:6px;padding:0;">';
  12. for (j=0;j<(c=Math.ceil)(h/pxs);j++){
  13. for (i = 0; i < c(w/pxs); i++){
  14. t = [[],[],[],[]];
  15. // Processes a 4x4 px block and averages the color+alpha
  16. for (y=j*pxs;y<(j+1)*pxs;y++){
  17. for (x=i*pxs;x<(i+1)*pxs;x++){
  18. t[0].push(data[(ptr=y*w*4+x*4)]);
  19. t[1].push(data[ptr+1]);
  20. t[2].push(data[ptr+2]);
  21. t[3].push(data[ptr+3]);
  22. }
  23. }
  24. var color=[(sm = function(l){sg=0;for(id=0;id<l.length;id++)sg+=l[id]==undefined?0:l[id];sg=c(sg/l.length);return sg>255?255:sg;})(t[0]),sm(t[1]),sm(t[2]),sm(t[3])/256];
  25. str += "<span style='background-color:rgba("+color[0]+','+color[1]+','+color[2]+','+color[3]+")'>&nbsp;&nbsp;&nbsp;</span>";
  26. }
  27. str += "<br>";
  28. }
  29. str += "</p>";
  30.  
  31. return str
  32. }
  33.  
  34. function out(dt){
  35. try{
  36. nw = window.open()
  37. nw.document.writeln(dt);
  38. }catch(err){
  39. alert(err);
  40. }
  41. }
  42.  
  43. images = document.images;s="";
  44. for(var abc = 0; abc < images.length; abc++){
  45. if ((w=(image = images[abc]).width) >= 30 && (h=image.height) >= 30){
  46. try{
  47. s += dr(image);
  48. }catch(err){
  49. // Image is on a different subdomain
  50. // For now, do not do anything.
  51. }
  52. }
  53. }
  54. out(s);
Add Comment
Please, Sign In to add comment