public static void saveImage(BufferedImage img, File output) throws IOException {
		output.delete();
		
		final String formatName = "png";
		
		for (Iterator<ImageWriter> iw = ImageIO.getImageWritersByFormatName(formatName); iw.hasNext();) {
		   ImageWriter writer = iw.next();
		   ImageWriteParam writeParam = writer.getDefaultWriteParam();
		   ImageTypeSpecifier typeSpecifier = ImageTypeSpecifier.createFromBufferedImageType(BufferedImage.TYPE_INT_RGB);
		   IIOMetadata metadata = writer.getDefaultImageMetadata(typeSpecifier, writeParam);
		   if (metadata.isReadOnly() || !metadata.isStandardMetadataFormatSupported()) {
		      continue;
		   }
		
		   setDPI(metadata, 300);
		
		   final ImageOutputStream stream = ImageIO.createImageOutputStream(output);
		   try {
		      writer.setOutput(stream);
		      writer.write(metadata, new IIOImage(img, null, metadata), writeParam);
		   } finally {
		      stream.close();
		   }
		   break;
		}
	}