Converting an Photo into a sketch in Java

One of my most popular Facebook app is Sketch your profile pic in which Profile photo of the user is converted into a sketch. For this I had used an external library Jhlabs which can be downloaded here

Here is the code which will convert the given buffered image into a sketch. It basically applies the grayscale filter to bring black and white effect followed by Point filter and Gaussian filter

                BufferedImage src = null;//Get buffered image
		BufferedImage target = null;
		src = ImageUtils.convertImageToARGB(src);
		// DitherFilter,InvertFilter,CircleFilter-filter.setRadius(400);
		// filter.setHeight(500),DisplaceFilter
		 * SphereFilter filter = new
		 * SphereFilter();filter.setRadius(src.getWidth()); target =
		 * filter.filter(src, null);
		// transformations begin=============
		// gray scale
		PointFilter grayScaleFilter = new GrayscaleFilter();
		BufferedImage grayScale = new BufferedImage(src.getWidth(),
				src.getHeight(), src.getType());
		grayScaleFilter.filter(src, grayScale);
		// inverted gray scale
		BufferedImage inverted = new BufferedImage(src.getWidth(),
				src.getHeight(), src.getType());
		PointFilter invertFilter = new InvertFilter();
		invertFilter.filter(grayScale, inverted);
		// gaussian blurr
		GaussianFilter gaussianFilter = new GaussianFilter(20);
		BufferedImage gaussianFiltered = new BufferedImage(src.getWidth(),
				src.getHeight(), src.getType());
		gaussianFilter.filter(inverted, gaussianFiltered);
		// color dodge
		ColorDodgeComposite cdc = new ColorDodgeComposite(1.0f);
		CompositeContext cc = cdc.createContext(inverted.getColorModel(),
				grayScale.getColorModel(), null);
		Raster invertedR = gaussianFiltered.getRaster();
		Raster grayScaleR = grayScale.getRaster();
		BufferedImage composite = new BufferedImage(src.getWidth(),
				src.getHeight(), src.getType());
		WritableRaster colorDodgedR = composite.getRaster();
		cc.compose(invertedR, grayScaleR, colorDodgedR);
		// ==================================
		target = composite;

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *