🔍

infinite poster – A1111 inpaint in Processing IDE


import processing.data.JSONArray;
import processing.data.JSONObject;
import processing.core.PGraphics;
import processing.core.PImage;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.commons.codec.binary.Base64;

import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

PGraphics canvas;
PGraphics pg;
PImage croppedImage;

int dimens = 256;
PImage generated_img;

float noiseScale = 0.02;

int lastExecutionFrame = 0;
int executionInterval = 1 * 3; // 5 seconds (assuming 60 frames per second)

void setup() {
  size(1000, 1000);
  frameRate(1);
  //canvas = createGraphics(1600, 1600);
//  canvas.imageMode(CENTER);
  pg = createGraphics(1600  , 1600 );
  drawPerlinNoise();  
  callSD();
}


void drawPerlinNoise() {
  pg.beginDraw();
  pg.background(25);

  for (int x = 0; x < pg.width; x++) {
    for (int y = 0; y < pg.height; y++) {
      float noiseVal = noise(x * noiseScale, y * noiseScale) * 255;
      pg.stroke(noiseVal);
      pg.point(x, y);
    }
  }

  pg.endDraw();
}


void draw() {
  // Display the result on the screen
  background(255);
   image(pg,0,0,width,height);
  if (generated_img != null) {
    //image(generated_img, 0, 0);
  }
  
   if (croppedImage != null) {
   // image(croppedImage, 0, 300,100,100);
  }
  
  
   // Check if 5 seconds have passed since the last execution
  if (frameCount - lastExecutionFrame >= executionInterval) {
    // Call your function here
    callSD();

    // Update the last execution frame
    lastExecutionFrame = frameCount;
  }
}


void mousePressed(){

    callSD();

}

// ----------------------------------------------------------
// ----------------------------------------------------------
// ----------------------------------------------------------



int ax = 0;
int ay = 0;


void callSD() {
  // Create a JSON object with the required data
  JSONObject call = new JSONObject();
  call.setString("prompt", "black white pinhole photography androgyn adult (male:.1)  traditional crochet costume shaman  , bokeh, depth of field,  <lora:epi_noiseoffset2:0.6>");
  call.setString("negative_prompt", "ugly, 1girl, illustration, render, deformed, deformed iris");
  call.setInt("width", dimens);
  call.setInt("height", dimens);
  call.setInt("steps",12);
  call.setFloat("denoising_strength", 0.53);
   call.setFloat("image_cfg_scale", 9.0);
  

  
  int rx = int(random(150))*8;
  int ry = int(random(150))*8;

  // Crop a 256x256 region from the PGraphics
  croppedImage = pg.get( rx,ry, dimens, dimens);
  
  ax=rx;
  ay=ry;

  // Convert the cropped PImage to a base64 string
  String base64Image = imageToBase64(croppedImage);


  PImage mask_img = loadImage("mask.png");
   String base64Image_mask = imageToBase64(mask_img);
   
   call.setString("mask", base64Image_mask);


  // Add the base64 string to the "init_images" field
  JSONArray initImagesArray = new JSONArray();
  initImagesArray.append(base64Image);
  call.setJSONArray("init_images", initImagesArray);

  // Convert JSON object to a string
  String jsonString = call.toString();

  // Define the API endpoint
  String apiEndpoint = "http://127.0.0.1:7860/sdapi/v1/img2img";

  try {
    // Create URL object
    URL url = new URL(apiEndpoint);

    // Open connection
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    // Set request method
    connection.setRequestMethod("POST");

    // Set request headers
    connection.setRequestProperty("accept", "application/json");
    connection.setRequestProperty("Content-Type", "application/json");

    // Enable input/output streams
    connection.setDoOutput(true);

    // Write data to the server
    try (java.io.OutputStream os = connection.getOutputStream()) {
      os.write(jsonString.getBytes("UTF-8"));
    }

    // Get response code
    int responseCode = connection.getResponseCode();

    // Check if the request was successful (HTTP 200 OK)
    if (responseCode == HttpURLConnection.HTTP_OK) {
      // Read the response
      InputStream is = connection.getInputStream();
      StringBuilder response = new StringBuilder();
      int charRead;
      while ((charRead = is.read()) != -1) {
        response.append((char) charRead);
      }
      is.close();

      // Handle the response
      JSONObject responseObject = parseJSONObject(response.toString());
      JSONArray imagesArray = responseObject.getJSONArray("images");

      // Assuming the "images" array contains only one item
      String resultBase64Image = imagesArray.getString(0);

      // Decode base64 to byte array
      byte[] imageBytes = Base64.decodeBase64(resultBase64Image);

      // Create a temporary file
      File tempGeneratedFile = File.createTempFile("tempGenerated", ".png");

      // Write the byte array to the temporary file
      try (FileOutputStream fos = new FileOutputStream(tempGeneratedFile)) {
        fos.write(imageBytes);
      }

      // Load the image from the temporary file
      generated_img = loadImage(tempGeneratedFile.getAbsolutePath());

      if (generated_img != null) {
        // Now the image is fully loaded, you can display it
        println("Image created");
        pg.beginDraw();
         pg.image(generated_img, ax, ay);
        pg.endDraw();
        
      
        
      } else {
        println("Failed to load image.");
      }

      // Delete the temporary files
      tempGeneratedFile.delete();

    } else {
      println("HTTP request failed with response code: " + responseCode);
    }

    // Disconnect the connection
    connection.disconnect();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

 


 

private String imageToBase64(PImage img) {
  String encodedBase64 = null;

  // Create a temporary file to save the PImage
  File tempFile = new File(sketchPath("temp_image.png"));
  img.save(tempFile.getAbsolutePath());

  try {
    FileInputStream fileInputStreamReader = new FileInputStream(tempFile);
    byte[] bytes = new byte[(int)tempFile.length()];
    fileInputStreamReader.read(bytes);
    encodedBase64 = new String(Base64.encodeBase64(bytes));
    fileInputStreamReader.close();
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    // Delete the temporary file
    tempFile.delete();
  }

  return encodedBase64;
}