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).
Related
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 am working a project involves visualizing a star catalog and create a printable bmp file. If I want to print a 24 by 24 picture with 1200 dpi resolution, it would be 28800*28800 and roughly 2.32 Gb.
Generally when create a bmp file, one would make a BufferedImage, graph something with the setRGB method, and save it as a bmp file with ImageIO.write.
But as I try to distribute my program, the end user may not have sufficient free RAM, and it will cause an OutOfMemoryError.
Is there a way to avoid saving every pixel of the image on RAM? If I can horizontally cut the image into several bands, and save them one by one to a single bmp file, it would be great (since I can get ride of the saved buffers).
Something similar would be:
Before I find the imageIO and awt package, I wrote a Bitmap class that saves a bitmap as a csv file and use an external software to render it. It has a toString method, and I use the method to save the content of the csv as a string instead of actually save it on the disk. When I want to make a large image, I create a fileWriter and a printWriter. I buffer a horizontal band of the image, and write it to the disk with the printWriter. Then I buffer the next band and replace the old band, and write it with the printWriter again. When it is finished, I close the writers and have the complete bitmap as a csv file on my disk.
What you need is code that directly creates an image file without loading the data completely into memory (usually this is called tiled image processing). I don't know a Java library that allows to do so, therefore my recommendation would be to look at the BMP file format and just write the file manually. AFAIR the BMP file format is pretty simple.
Some time ago I have seen something similar for PNG output: PngXxlWriter.java
I basically have an matrix of bytes. Each row (meaning byte[]) represents an image. How do I create a movie out of that (any format - avi, mpeg, whatever), and save it as a file?
Each image can be one of the following:
int JPEG Encoded formats.
int NV16 YCbCr format, used for video.
int NV21 YCrCb format used for images, which uses the NV21 encoding format.
int RGB_565 RGB format used for pictures encoded as RGB_565.
int YUY2 YCbCr format used for images, which uses YUYV (YUY2) encoding format.
int YV12 Android YUV format: This format is exposed to software decoders and applications.
I can choose the format to whatever I like, as long as I get to create the movie.
public void createMovie(byte[][] images) {
// and ideas on what to write here?
}
I don't need the actual implementation, just let me know the idea and what external libraries I need (if I need any).
I also need to edit some of the images (the byte stream) before I create the movie (to add some text). How can I do that?
The solution needs to be "Java only"! No external programs, no external commands (but I can use external jars).
Thanks!
The solution seems to be to use Mencoder (or at least, that seems to be a semi-popular choice).
Here's a link that specifically addresses images-to-movies capabilities in Mencoder.
As for rendering text onto the frames before encoding them as part of the video, you can use Java2D's image manipulation libraries to simply draw text on top of the images beforehand For example:
Load up the images into BufferedImage objects via the ImageIO library's .read method
Use Graphics2D's .drawString method to render the text
That's one way to do it, and this FAQ should get you started in that direction with Java2D, font rendering, etc., and offer pointers to further resources.
The ImageIO library also allows you to read/write a number of image formats, effectively allowing you to transcode images from, say, .jpg -> BufferedImage -> .png, or any which way you need to do it, if you want to store the image files temporarily during the conversion process, and/or convert all the images to a single format when importing them for the conversion project, etc.
Depending on how many output formats you want to support, you'll probably do something like
public void createMovie(BufferedImage[] frames, String destinationFormat)
...where "destinationFormat" is something like "m4v", "mpeg2", "h.264", "gif", etc.
Have you heard about JMF (Java Media Framework), from the sample you can find this example : Generating a Movie File from a List of (JPEG) Images
You can try making a gif with this gif encoder.
I wrote an MJPEG reader and writer for playing videos inside of Java applets. MJPEG is not the most advanced video format but it is very easy to manipulate. The code is part of my computer vision library BoofCV, but you could just rip out this one class for your own purposes.
Download this file: CreateMJpeg.java
Look at main function. Where it reads in jpeg images put your byte[] data, but you will need to convert it to jpeg's first.
You can convert it into a jpeg using the standard java library
Run modified code and enjoy your movie
Sorry its not in a more user friendly format, but at least you don't need to mess with JNI like some other solutions.
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 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.