I am using a common format all over application for images as png.Any jpg image uploaded still gets saved as png using code as below.
java.awt.image.BufferedImage bufferedImage = ImageIO.read(jpgImagePAth);
if(!IsExtensionPng(jpgImagePath)){
ImageIO.write(bufferedImage, "png", new File(pptFolder, justNamePng));
}
But this preserves alpha even though it was not there in the jpg so makes a 2MB Image 7MB and 6MB to 16MB . Is there anyway to save png without maintaining the alpha ?
The reason I need to conver to PNG is that later on when I add text on image it looses the actual resolution. I already tried loseless JPEG which didnt fix it.
It's not the alpha channel that is causing the file size to grow, it's the file type. JPG uses lossy compression; PNG is lossless compression. In other words, JPG is throwing out some data to reduce the size of the file. That's why you get to choose a "quality" level when saving to JPG - that determines how much is thrown out.
How do you know you're getting the alpha channel anyway? If you still want PNG and want to be sure you're dropping the alpha channel, set the image type to BufferedImage.TYPE_RGB, e.g.
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_RGB);
You'll have to get the graphics object associated with your new BufferedImage and copy the jpg onto it, then write it out. This question isn't quite the same as yours but has sample code you may find useful.
Paul
I don't know exactly in what situation you are. But I should keep JPEG JPEG. The only advantage of converting JPEG to PNG is wasting hdd space.
Related
I want to write informations (former: int/String) into a PNG-file without making the changes visible to the human eye.
In order to achieve this i extract the file in the form of a Byte array:
byte[] imageAsBytes = fileInputStream.readAllBytes();
In the next step i jump the header (8 Bytes, already tried up to 300Bytes). I already have my information processed and can extract it bitwise.
I replace the last Bit from the picture`s Bytes with the Bitwise extracted information.
This works well with the .bmp file format but applying this technique to PNG-files distorts the image.
My guess is that the PNG-file contains additional information after the header.
So is there any information stored after the header and if so what marks this "information-part" as one?
In order to read and decode a .png file we can use BufferedImage as it follows:
File imageFile = new File("myimage.png");
BufferedImage image = ImageIO.read(imageFile);
No we can manipulate its pixels using getRGB and setRGB methods:
int pixel = image.getRGB(x, y);
// do some byte manipulation on the pixel
// ....
image.setRGB(x, y, pixel);
After this the BufferedImage should be piped to an output stream to be able to save the new image.
I think the best way will be not look at file bytes, but open it with some png decoder then change pixels at image itslef then save it again using png. When You will play with file bytes itself for sure You will broke the format of png, You didnt in bmp becasue bmp its jsut pixel by pixel saved drectly to file wihout any packaging or encoding
Am using Thumbnailator to compress the image in my application. Everything is work fine alone when i try to convert the JPG image to PNG. At this process the size of an image getting twice after compressing. Following code is am used to convert image.
File a=new File("C:\\Users\\muthu\\Downloads\\SampleJPGImage_5mbmb.jpg");
Thumbnails.of(a).scale(1).outputQuality(0.5).toFile("C:\\Users\\muthu\\Downloads\\SampleJPGImage_5mbmb1.png");
using pure java also doing same and code is follows
BufferedImage bufferedImage = ImageIO.read(new File("C:\\Users\\muthu\\Downloads\\SampleJPGImage_5mbmb.jpg"));
ImageIO.write(bufferedImage, "png", new File("C:\\Users\\muthu\\Downloads\\javaPngimage.png"));
Ex: 5MB image file is converted to 32MB file. I should not go for resize to compress. Am stuck with this
JPEG and PNG are both compressed image formats.
JPEG compresses the pixels using frequency transforms and quantisation. It can be a lossy or lossless compression format.
PNG is a lossless compression format with different compression mechanisms. I dare say the "quality" parameter doesn’t actually change the image at all.
The biggest image file type would be BMP (.bmp), which is 3 bytes (RGB) for each pixel plus a header. It’s worth keeping this size in mind when deciding if an image file is "big" or not. JPEG compression is pretty good.
It sounds like your image has a lot of details that can be compressed well in the frequency domain (JPEG) but compress poorly as PNG.
Simplest solution: a JPEG format thumbnail. If you needed to use PNG, and you were resizing your image, I’d suggest resize JPEG then convert to PNG.
I would like to read an image and then change its format but without saving it.
For example I can read the image like this
BufferedImage img=ImageIO.read(new File(fileName));
Then I want to change the format of img, for example from jpeg to png.
The only way I found is to use ImageIO.read to write and then read the new image, but it does seem to be an efficient way to do it.
When you "read" the image via
BufferedImage img=ImageIO.read(new File('myimage.png'));
you are not only reading but also decoding it, i.e., transforming the raw bytes in the (say) PNG format to some "RAW" format that your aplication or API can manipulate (or display) - in this case, a BufferedImage. Once this is done, the fact that this image came from a PNG file is forgotten. To read it as PNG and save it as JPEG you need to decode it (as PNG) and then code it (as JPG).
I would like to read an image and then change its format but without saving it.
The "format" of the image (in the PNG/JPEG sense) gives you a way of packing an image in a sequence of bits. So, your desire makes little sense. At most, you could store those bits in memory (what for?), but that would be the same as "saving it" (to memory instead of disk).
The above answer that someone has suggest, converts my colored image to a black and white one. So it's not appropriate for my question.
File file = new File("path");
BufferedImage bufferedImage = ImageIO.read( file );
here is the code and below is the image. Download the image and save in your pc. And try to run the above code with a correct value of path, it will throw an exception in the topic
Download image: https://skydrive.live.com/?cid=19547371C4F3B839&id=19547371C4F3B839%21105
Simply if someone can obtain a java.awt.image.BufferedImage object from the image given that's enough (should not convert the image to gray scale one).
You are a genius if you can answer this :D. Plz help.
There's nothing wrong with your code here. I could read your image using my JPEGImageReader plugin for ImageIO. This image reader tries to be lenient about JPEG errors, and is slightly more capable than the standard Java JPEGImageReader.
However, your JPEG file seems to have a number of problems so it can't be read 100%:
First, the ICC color profile in the image has 4 color components, while the image data has only 3 color components (this is causing the exception you see). The root cause is probably bad conversion software. Use ExifTool or similar software to have a look at the metadata for further investigation. My reader will simply give a warning and ignore the ICC profile in this case.
Second, the JPEG stream ends prematurely (missing EOI). You'll notice that there's some garbage pixels at the lower right of the image. There's nothing you can do about that, except getting the original image (well, actually, the image contains a thumbnail and the thumbnail is undamaged; you could try to recreate the data from that if you really need to). The image returned from my reader is consistent with images read by native software.
I am generating lots of images in java and saving them through the ImageIO.write method like this:
final BufferedImage img = createSomeImage();
ImageIO.write( img, "png", new File( "/some/file.png" );
I was happy with the results until Google's firefox addon 'Page Speed' told me that i can save up to 60% of the size if i optimize the images. The images are QR codes, their size is around 900B each and the firefox-plugin optimized versions are around 300B.
I'd like to save such optimized 300B Images directly from java.
So here my question again: How to save optimized png images with java's ImageIO?
Use PngEncoderB to convert your BufferedImage into a PNG encoded byte array.
You can apply a filter to it, which helps prepare the image for better optimization. This is what OptiPNG does, only OptiPNG calculates which filter will get you the best compression.
You might have to try applying each filter to see which one is consistently better for you. With 2 bit color, I think the only filter that might help is "up", so I'm guessing that's the one to use.
Once you get the image to a PNG encoded byte array, you can write that directly to a file.