I am using below code for reducing image size. It reduces size from 1MB to 250 KB. Its fine but it changes image colour.
File input = new File("ImageTOCompress.jpg");
BufferedImage image = ImageIO.read(input);
File compressedImageFile = new File("CompressedImage.jpg");
OutputStream os =new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, null), param);
os.close();
ios.close();
writer.dispose();
Please help me to maintain image colour as it is...Thanks!!!
Related
I'm trying to compress an image to upload to S3 Bucket but after the image is compressed, the size of the compressed image is larger than the original. From 227KB -> 236KB
Can anybody explain for me why this happened?
Here's my code:
BufferedImage bufferedImage = ImageIO.read(photoFile);
File compressedImageFile = new File("compressed_image.jpeg");
OutputStream outputStream = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter imageWriter = writers.next();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
ImageWriteParam imageWriteParam = imageWriter.getDefaultWriteParam();
if (imageWriteParam.canWriteCompressed()) {
imageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
imageWriteParam.setCompressionQuality(0.5f);
}
imageWriter.write(null, new IIOImage(bufferedImage, null, null), imageWriteParam);
log.info("Close stream");
outputStream.close();
imageOutputStream.close();
imageWriter.dispose();
Here's my image:
original - 227KB
compressed - 236KB
I tried to change the compression quality but it doesn't seem working, and this problem happens in some case, not all
jpeg images are already compressed. “Compressing” an already compressed file makes it a bit bigger (header data) each time.
A text file on the other hand will get much smaller.
We have generated a JPG file by using
org.openqa.selenium.TakesScreenshot class
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File dest = new File("C:\\Users\\admin\\Desktop\\Images\\29_18-03-20-07-11-47.jpg");
FileHandler.copy(src, dest);
As the generated image is having file size of 1.5MB to 2.9MB size. We want to reduce the file size to kbs.
we tried ImageIO but the generated image is not having original file color space/color profile.
File input = new File("C:\\Users\\admin\\Desktop\\Images\\29_18-03-20-07-11-47.jpg");
File compressedImageFile = new File("C:\\Users\\admin\\Desktop\\Images\\compressed_image7.jpg");
BufferedImage image = ImageIO.read(input);
OutputStream os = new FileOutputStream(compressedImageFile);
Iterator<ImageWriter writers = ImageIO.getImageWritersByFormatName("jpg");
ImageWriter writer = (ImageWriter) writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
writer.write(null, new IIOImage(image, null, null), param);
Could not share the image or screen shot
Could someone let me know how can I reduce the filesize
Try using lossless JPEG
ImageWriter writer= (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();
ImageWriteParam param= writer.getDefaultWriteParam();
param.setCompressionMode(param.MODE_EXPLICIT);
param.setCompressionType("JPEG-LS");
writer.setOutput(ImageIO.createImageOutputStream(new File(path)));
writer.write(null, new IIOImage(image, null, null), param);
Code works fine for other formats of images but for .png compression is negligible or no compression at all
here is a sample how the image is loaded and saved
Or someone suggest me better way of compressing png image
This is myfirst question posted on stackoverflow. please ignore any mistakes
I have taken sample images from local folder
File input = new File("C:/Users/Public/Pictures/Sample Pictures/bz_nela19911.png");
InputStream is = new FileInputStream(input);
BufferedImage image = ImageIO.read(is);
File compressedImageFile = new File("C:/Users/Public/Pictures/Sample Pictures/me1.png");
OutputStream os =new FileOutputStream(compressedImageFile);
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("png");
// works fine for other formats of images
ImageWriter writer = null;
while (writers.hasNext()) {
ImageWriter candidate = writers.next();
if (candidate.getClass().getSimpleName().equals("CLibPNGImageWriter")) {
writer = candidate; // This is the one we want
break;
}
else if (writer == null) {
writer = candidate;
}
}
ImageOutputStream ios = ImageIO.createImageOutputStream(os);
writer.setOutput(ios);
ImageWriteParam param = writer.getDefaultWriteParam();
if (param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.1f);
}
writer.write(null, new IIOImage(image, null, null), param);
I used the following code to write an image to a file using BI_RLE8 compression. How can I read back the image saved in that file?
BufferedImage source = new BufferedImage(200, 100, BufferedImage.TYPE_BYTE_GRAY);
ImageWriter writer = ImageIO.getImageWritersByFormatName("bmp").next();
ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionType("BI_RLE8");
writer.setOutput(new FileImageOutputStream(new File("c:\\RLE.bmp")));
writer.write(null, new IIOImage(source, null, null), param);
writer.dispose();
I am using java and the following code. Is there a way to use RenderingHints to accomplish this?
try {
sourceImage = ImageIO.read(new File("images.jpg"));
BufferedImage dstImage = null;
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorConvertOp op = new ColorConvertOp(colorSpace, null);
dstImage = op.filter(sourceImage, null);
ImageIO.write(dstImage, "jpeg", new File("output.jpg"));
System.out.println("processing complete");
} catch (IOException e) {
e.printStackTrace();
}
I got this working using ImageWriteParam.setCompressionQuality
Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
ImageWriter writer = (ImageWriter)iter.next();
// instantiate an ImageWriteParam object with default compression options
ImageWriteParam iwp = writer.getDefaultWriteParam();
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(0.5f); // an integer between 0 and 1
// 1 specifies minimum compression and maximum quality
File file = new File("output.jpg");
FileImageOutputStream output = new FileImageOutputStream(file);
writer.setOutput(output);
IIOImage image = new IIOImage(dstImage, null, null);
writer.write(null, image, iwp);
writer.dispose();