Simple JPG creation from Processing

By popular demand. . .

// Daniel Shiffman
// http ://www.shiffman.net
// May 2006

// Create JPG

import javax.imageio.*;

PImage img;

void setup() {
  size(200,200);
  // Draw a blank PImage
  img = new PImage(200,200);
  img.loadPixels();
  for (int i = 0; i < img.pixels.length; i++) img.pixels[i] = color(100,50,250);
  img.updatePixels();
  saveJPG(img,"image.jpg");
  image(img,0,0);
  noLoop();
}

public void saveJPG(PImage img, String f) {
  String filename = f;
  // Create BufferedImage from PImage
  BufferedImage bImg = new BufferedImage(img.width, img.height, BufferedImage.TYPE_INT_RGB);
  bImg.setRGB(0,0,img.width,img.height,img.pixels,0,img.width);
  String filepath = sketchPath + "/" + filename;

  File file = new File(filepath);
  try {
    ImageIO.write(bImg, "jpg", file);
    System.out.println("Created JPG: " + filepath);
  }
  catch (IOException e) {
    System.out.println("Problem creating JPG: " + filepath);
    e.printStackTrace();
  }
}

No Responses to “Simple JPG creation from Processing”  

  1. No Comments

Leave a Reply