I have a file that when I try to convert it will not, but i can convert the file in a online converter. What could be the cause of this?
FileSeekableStream fss = new FileSeekableStream(tifFile);
ImageDecoder decoder = ImageCodec.createImageDecoder("tiff", fss, null);
RenderedImage image = decoder.decodeAsRenderedImage();
ImageIO.write(image, "png", new File(imageFolder + "/" + baseName + ".png"));
Edit:
Trying to be clear about the question, what may cause some tiff files to convert and some not to? What are possible things in a tiff file that I can check to see why it will not convert or things I can change before I try making a tiff to a png?
This is the image
I am not sure what you mean by "cannot convert". I ran into an issue a couple of years ago converting tiff to png - the images converted, but the colors were way off and looked horrible.
The cause was actually that the input image (tifFile) was CMYK and the output file was RGB.
I know its not really an answer, but I am unable to comment here at this point...
I found out that the file that was not converting had a completely different color model than the images that were converting, so I changed the color model of the image that was not converting and it worked, color was off a little but I made progress, thanks for the GIMP suggestion.
Related
How do you convert bmp to jpg in Java? I know how to use the ImageIO way but is there a much faster or better way of doing it?
This is the ImageIO way of doing that I found on the web.
`//Create file for the source
File input = new File("c:/temp/image.bmp");
//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);`
//Create a file for the output
File output = new File("c:/temp/image.jpg");
//Write the image to the destination as a JPG
ImageIO.write(image, "jpg", output);
If I use this way will I lose quality?
Thanks
Yes you will. Actually regardless of the way to convert a BMP (lossless) to JPG (lossy) you always lose quality. You can limit the damage if you set the JPG quality to 100% (which kind of defeats the purpose in my opinion).
Use this tutorial to fix it.
How do you convert bmp to jpg in Java? I know how to use the ImageIO way but is there a much faster or better way of doing it?
This is the ImageIO way of doing that I found on the web.
`//Create file for the source
File input = new File("c:/temp/image.bmp");
//Read the file to a BufferedImage
BufferedImage image = ImageIO.read(input);`
//Create a file for the output
File output = new File("c:/temp/image.jpg");
//Write the image to the destination as a JPG
ImageIO.write(image, "jpg", output);
If I use this way will I lose quality?
Thanks
Yes you will. Actually regardless of the way to convert a BMP (lossless) to JPG (lossy) you always lose quality. You can limit the damage if you set the JPG quality to 100% (which kind of defeats the purpose in my opinion).
Use this tutorial to fix it.
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.
I have a problem to read a specific jpeg image (CMYK color space) which have been created with ImageMagick :
inputStream = new ByteArrayInputStream(bytesImage);
bufferedImage = ImageIO.read(inputStream);
//IIOException : unsupported image type
I tried to use JAI instead of ImageIO but it still doesn't work :
seekableStream = new ByteArraySeekableStream(bytesImage);
bufferedImage = JAI.create("Stream", seekableStream).getAsBufferedImage();
//Unable to render RenderedOp for this operation
Any idea to solve my problem?
Reading JPEG images with CMYK colors is rather tricky in Java. But I've posted a complete solution here. It solves the problem of dark colors becoming white as well.
Since I have found my image has a CMYK color space, I have found a solution on stackoverflow : Problem reading jpeg image using imageio
Now, the problem is the quality of my resulting image... dark colors became white...
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.