Hi all,
I wrote a program in Processing which writes the 1D coordinates of black pixels in an image to a text file.
I wrote a second program which should read the text file and recreate the black pixels on a white image to verify the functionality of script #1
The problem is that the second program which should recreate the black pixels on a white image is not doing so.
For the first script, The user opens the gui, hits 2 to open the file browser, then loads a file. The code resizes the file and changes it to black and white. The code then scans for black pixels and writes the black pixel coordinates to a text file with one coordinate per line.
The second script should simply recreate the original image by adding black pixels in those coordinates to a white image. This is to verify the functionality of script #1.
Here’s the code for script #1:
boolean stopt = false;
boolean writin = false;
PrintWriter output;
PImage input;
int limiter = 0;
void setup() {
size(350, 500);
// Create a new file in the sketch directory
output = createWriter("DIRECTIONS.TXT");
}
void draw(){
background(0, 25, 51);
//show processed image here.
text("Click and type '1' to stop", 10, 440);
text("Click and type '2' to convert image to command file", 10, 460);
text("Copy DIRECTIONS.TXT to the printer's memory card after", 10, 480);
if (stopt) {
writin = false;
text("finished or stopped.", 10, 420);
}
if (!stopt) {
text(" ", 10, 420);
}
if (writin) {
stopt = false;
text("translating image to printer commands", 10, 420);
}
if (!writin) {
text(" ", 10, 420);
}
if(input!= null){
input.resize(300, 300);
input.filter(THRESHOLD, 0.5);
image(input, 10, 10, 330, 330);
input.loadPixels();
//Loop through each pixel (use image size instead of pixels.length!)
if (limiter <= 90000) {
for (int x = 0; x < width; x++ ) {
for (int y = 0; y < height; y++ ) {
color black = color(0, 0, 0);
color c = get(x, y);
if (c == black) {
int loc = x + y * width;
output.println(loc);
writin = true;
}
}
limiter = limiter + 1;
}
}
else if (limiter > 90000) {
stopt = true;
}
}
}
void imageSelected(File selection) {
if (selection == null) {
println("Window was closed or the user hit cancel.");
} else {
input = loadImage (selection.getAbsolutePath());
}
}
void keyPressed() {
if (key == '1') {
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
stopt = true;
//exit(); // Stops the program
}
if (key == '2') {
selectInput("Select an image to process", "imageSelected");
}
}
And here’s the code for script 2:
PImage input;
int tex = 0;
void setup() {
input = loadImage("white.png"); //mostly blank image in sketch folder 300 x 300 px.
size (300, 300);
}
void draw() {
loadPixels();
input.loadPixels();
String[] lines = loadStrings("direct.TXT"); //text file with each line being a 1D pixel coordinate in linear order.
for (int i = 0; i < lines.length; i++) {
tex = Integer.parseInt(lines[i]);
for (int x = 0; x < width; x++ ) {
for (int y = 0; y < height; y++ ) {
color black = color(0, 0, 0);
//color c = get(x, y);
int loc = x + y * width;
if (loc == tex) {
pixels[loc] = black;
}
}
}
}
updatePixels();
}