Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using the <code>java.awt.image</code> (<a href="http://docs.oracle.com/javase/6/docs/api/java/awt/image/package-summary.html">javadoc</a>) and <code>javax.imageio</code> (<a href="http://docs.oracle.com/javase/6/docs/api/javax/imageio/package-summary.html">javadoc</a>) APIs, you can easily loop through the image's pixels and perform the pixelation yourself.</p> <p>Example code follows. You will need at least these imports: <code>javax.imageio.ImageIO</code>, <code>java.awt.image.BufferedImage</code>, <code>java.awt.image.Raster</code>, <code>java.awt.image.WritableRaster</code>, and <code>java.io.File</code>.</p> <p>Example:</p> <pre><code>// How big should the pixelations be? final int PIX_SIZE = 10; // Read the file as an Image img = ImageIO.read(new File("image.jpg")); // Get the raster data (array of pixels) Raster src = img.getData(); // Create an identically-sized output raster WritableRaster dest = src.createCompatibleWritableRaster(); // Loop through every PIX_SIZE pixels, in both x and y directions for(int y = 0; y &lt; src.getHeight(); y += PIX_SIZE) { for(int x = 0; x &lt; src.getWidth(); x += PIX_SIZE) { // Copy the pixel double[] pixel = new double[3]; pixel = src.getPixel(x, y, pixel); // "Paste" the pixel onto the surrounding PIX_SIZE by PIX_SIZE neighbors // Also make sure that our loop never goes outside the bounds of the image for(int yd = y; (yd &lt; y + PIX_SIZE) &amp;&amp; (yd &lt; dest.getHeight()); yd++) { for(int xd = x; (xd &lt; x + PIX_SIZE) &amp;&amp; (xd &lt; dest.getWidth()); xd++) { dest.setPixel(xd, yd, pixel); } } } } // Save the raster back to the Image img.setData(dest); // Write the new file ImageIO.write(img, "jpg", new File("image-pixelated.jpg")); </code></pre> <hr> <p>Edit: I thought I should mention -- the <code>double[] pixel</code> is, as far as I can tell, just the RGB color values. For example, when I dumped a single pixel, it looked like <code>{204.0, 197.0, 189.0}</code>, a light tan color.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload