我正在尝试加载一个图像,并随着时间的推移将其像素化。
我下载了一些代码,使我能够根据鼠标的位置对图像进行像素化。
我想慢慢地像素化并循环,类似于在没有任何用户输入的情况下交错的工作方式。(例如,请参见gif(
我试过使用(millis(函数,但我做不到。
交错示例
PImage img;
int pixls=5;
color average;
int x,y,yinc;
void setup()
{
size(710,710);
background(255);
noStroke();
img = loadImage("p.jpg");
img.resize(710,710);
}
void draw()
{
background(0);
loadPixels();
pixls=int(map(mouseY,0,height,5,100));
for(int i=0;i<pixls;i++)
{
for(int j=0;j<pixls;j++)
{
float r = red(img.pixels[((height/pixls)*(j))*(width)+(width/pixls*(i))]);
float g = green(img.pixels[((height/pixls)*(j))*(width)+(width/pixls*(i))]);
float b = blue(img.pixels[((height/pixls)*(j))*(width)+(width/pixls*(i))]);
fill(r,g,b);
rect((width/pixls*(i)),(height/pixls)*(j),width/pixls,height/pixls);
yinc=(height/pixls)*j;
}
}
}
更新代码
import java.awt.Robot;
PImage img;
int pixls = 0;
int x, y, yinc;
int XOffset = 0;
int YOffset = 30;
int counter = YOffset;
Robot robot;
void setup() {
size(900, 900);
//noCursor();
noStroke();
img = loadImage("p.jpg");
img.resize(900, 900);
//surface.setLocation(XOffset, YOffset);
}
void draw() {
//loadPixels();
frameRate(14);
pixls = (int)map(mouseY, height, 20, 250, 20); //pixellation
for (int i = 0; i < pixls; i++) {
for (int j = 0; j < pixls; j++) {
float r = red(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
float g = green(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
float b = blue(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
fill(r, g, b);
rect((width/pixls)*i, (height/pixls)*j, width/pixls, height/pixls);
yinc = (height/pixls)*j;
}
}
try {
robot = new Robot();
robot.mouseMove(XOffset, counter); //counter is start of mouse
if (counter > height + YOffset)
{
// counter = YOffset + 30; // +30 for menubar
}
}
catch (Exception e) {
//println("error = ", e);
}
counter++;
}
您可以使用robot.mouseMove((自动移动鼠标,然后让它循环回图像顶部以重复。唯一的缺点是,当应用程序运行时,用户会失去对鼠标的控制。在Mac电脑上,你必须使用command-Q才能退出。
import java.awt.Robot;
PImage img;
int pixls = 0;
int x, y, yinc;
int XOffset = 100;
int YOffset = 50;
int counter = YOffset;
Robot robot;
void setup() {
size(710, 710);
noStroke();
img = loadImage("myImage.jpg");
img.resize(710, 710);
surface.setLocation(XOffset, YOffset);
}
void draw() {
pixls = (int)map(mouseY, 0, height, 5, 350);
for (int i = 0; i < pixls; i++) {
for (int j = 0; j < pixls; j++) {
float r = red(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
float g = green(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
float b = blue(img.pixels[(height/pixls)*j*width+(width/pixls)*i]);
fill(r, g, b);
rect((width/pixls)*i, (height/pixls)*j, width/pixls, height/pixls);
yinc = (height/pixls)*j;
}
}
try {
robot = new Robot();
robot.mouseMove(XOffset + 10, counter);
if (counter > height + YOffset + 30) {
counter = YOffset + 30; // + 30 for menubar
}
}
catch (Exception e) {
println("error = ", e);
}
counter++;
}