2016-11-27 84 views
0

我在做什么现在,我得到的所有像素与var getPixels = require("get-pixels"),然后我循环的像素阵列与此代码在:像素化图像节点JS

var pixelation = 10; 
var imageData = ctx.getImageData(0, 0, img.width, img.height); 
var data = imageData.data; 

for(var y = 0; y < sourceHeight; y += pixelation) { 
    for(var x = 0; x < sourceWidth; x += pixelation) { 
     var red = data[((sourceWidth * y) + x) * 4]; 
     var green = data[((sourceWidth * y) + x) * 4 + 1]; 
     var blue = data[((sourceWidth * y) + x) * 4 + 2]; 

     //Assign 
     for(var n = 0; n < pixelation; n++) { 
      for(var m = 0; m < pixelation; m++) { 
       if(x + m < sourceWidth) { 
        data[((sourceWidth * (y + n)) + (x + m)) * 4] = red; 
        data[((sourceWidth * (y + n)) + (x + m)) * 4 + 1] = green; 
        data[((sourceWidth * (y + n)) + (x + m)) * 4 + 2] = blue; 
       } 
      } 
     } 

    } 
} 

这种方法的问题是,结果图像太尖锐。 enter image description here

我所寻找的是什么东西,类似这样的一个已经用ImageMagick的做-sale

enter image description here

,我已经用了第二个命令是

convert -normalize -scale 10% -scale 1000% base.jpg base2.jpg 

这种方法的问题是我不知道如何指定实际的像素大小。

那么有可能通过for循环得到第二个结果。或者,最好使用imagemagick -scale,但如果有人能够帮助数学,那么我可以让实际的像素尺寸变得更好。

回答

2

不知道你是挣扎什么数学,但如果我们有一个600×600像这样开始:

enter image description here

然后,如果你想在最终图像跨和5只5块状像素块状像素向下移动页面,你可以扩展它到5x5的,然后它的规模回升到原来的大小:

convert start.png -scale 5x5 -scale 600x600 result.png 

enter image description here

或者,如果你想要去到10×10块状像素:

convert start.png -scale 10x10 -scale 600x600 result2.png 

enter image description here

0

你写这个的方式,图像被处理成大小为nxn的像素,其中npixelation变量指定。增加pixelation将提供期望的“粗糙度”。