Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Compress and add javascript: prefix, then save as a bookmarklet
- function process(image){
- h = image.height; w = image.width;
- var buffer = document.createElement("canvas"); // Anonymous canvas
- buffer.width = w;buffer.height=h;
- var ct = buffer.getContext('2d');
- ct.drawImage(image,0,0);
- var data = ct.getImageData(0,0,w,h).data; // This may raise an exception for external images.
- var pxs=2; // Height-Width of the pixel block
- var str='<p style = "line-height:6px;font-size:6px;padding:0;">';
- for (j=0;j<(c=Math.ceil)(h/pxs);j++){
- for (i = 0; i < c(w/pxs); i++){
- t = [[],[],[],[]];
- // Processes a 4x4 px block and averages the color+alpha
- for (y=j*pxs;y<(j+1)*pxs;y++){
- for (x=i*pxs;x<(i+1)*pxs;x++){
- t[0].push(data[(ptr=y*w*4+x*4)]);
- t[1].push(data[ptr+1]);
- t[2].push(data[ptr+2]);
- t[3].push(data[ptr+3]);
- }
- }
- 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];
- str += "<span style='background-color:rgba("+color[0]+','+color[1]+','+color[2]+','+color[3]+")'> </span>";
- }
- str += "<br>";
- }
- str += "</p>";
- return str
- }
- function out(dt){
- try{
- nw = window.open()
- nw.document.writeln(dt);
- }catch(err){
- alert(err);
- }
- }
- images = document.images;s="";
- for(var abc = 0; abc < images.length; abc++){
- if ((w=(image = images[abc]).width) >= 30 && (h=image.height) >= 30){
- try{
- s += dr(image);
- }catch(err){
- // Image is on a different subdomain
- // For now, do not do anything.
- }
- }
- }
- out(s);
Add Comment
Please, Sign In to add comment