Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

JAVA: Read and Write PPM, PGM images in JAVA

Download the code library from HERE
Also you can download some sample PPM and PGM images from here.

The following helps to setup the project using Eclipse.
This requires 2 Image Processing libraries which are included in the zip. [jai_imageio.jar and clibwrapper_jiio.jar]
If you are not using eclipse, please add these 2 libraries to your project.

After opening Eclipse:

1. Goto File->New->Java Project
2. In the "contents" section, select the radio button "create project from existing source"
3. Click browse to point to the downloaded ImgProcTool folder (Assuming u have unzipped the file)
4. Click Finish on the "new Java Project" window.

TEST IT BY EXECUTING THE PROJECT
A simple sample program IpToolTester.java is included, which reads and writes a sample PGM image.
 public class IpToolTester {  
     public static void main(String[] args) {  
           ImageTool imageTool = new ImageTool();  
           String srcPath = "lena_384.pgm";  
           
           Image srcImg = new Image(srcPath);  
           Image tgtImg = new Image();  
           tgtImg.init(srcImg.getNumRows(), srcImg.getNumCols());  
           imageTool.copyImg(srcImg, tgtImg);  
           tgtImg.save("lena_write.pgm");  
           System.out.println("Done");  
      }  
 }  
5. Right click on the project and select "Run as->java application"
6. If everything is fine, there will be console output ending with "Done".
7. Refresh the project, you will see a new .pgm file "lena_write.pgm".

Mac: Running 32-bit JVM on Mac OSX using eclipse

I had to use SMILE API on 64-bit Mac OSX machine.
SMILE API provides 32-bit version of their binaries.

I found a way to use 32-bit JVM in Mac OSX 64bit machines using Eclipse.
By default Mac OSX comes with both 32 bit and 64bit JVM installed.
Eclipse uses 64-bit JVM by default.

In order to use the 32-bit JVM, do the following in Eclipse
1. Right Click on your project, goto "Run As", Select "Run Configurations"
2. Select the Arguments tab,
3. Add “-d32″ in VM arguments field.


Draw Smooth Circles in JAVA instead of jagged edge circles.

Usually when we draw circles/ovals using Graphics2D in JAVA, we get jagged edges.
Following example creates smooth edges to the circle.
The key is g2d.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class DrawSmoothCircle {
public static void main(String[] argv) throws Exception {
BufferedImage bufferedImage = new BufferedImage(100,100, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();

g2d.setRenderingHint (RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.green);
g2d.fillOval(10, 10, 50, 50);
g2d.dispose();

ImageIO.write(bufferedImage, "png", new File("newimage.png"));
}
}

Python contextlib for Timing Python code

If you've ever found yourself needing to measure the execution time of specific portions of your Python code, the `contextlib` module o...