Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to read JPEG image using ImageIO.read(File file)

I'm having problems reading this one JPEG file using ImageIO.read(File file) - it throws an exception with the message "Unsupported Image Type".

I have tried other JPEG images, and they seem to work fine.

The only differance I've been able to spot is that this file seems to include a thumbnail - is that known to cause problems with ImageIO.read()?

Troublesome image

EDIT:

Added the resulting image:

Strange colors

like image 485
Malakim Avatar asked Mar 09 '10 11:03

Malakim


People also ask

Can ImageIO read JPG?

Just use the read method of the Java ImageIO class, and you can open/read images in a variety of formats (GIF, JPG, PNG) in basically one line of Java code.

Does Java support JPg?

Java 2D supports loading these external image formats into its BufferedImage format using its Image I/O API which is in the javax. imageio package. Image I/O has built-in support for GIF, PNG, JPEG, BMP, and WBMP.

What is ImageIO read?

Imageio is a Python library that provides an easy interface to read and write a wide range of image data, including animated images, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.5+, and is easy to install. Main website: https://imageio.readthedocs.io/

What kind of exception does the ImageIO class cause?

imageio. ImageIO. write and that failure is causing a null pointer exception.


2 Answers

Old post, but for future reference:

Inspired by this question and links found here, I've written a JPEGImageReader plugin for ImageIO that supports CMYK color models (both with original color model, or implicitly converted to RGB on read). The reader also does proper color conversion, using the ICC profile embedded in the JPEG stream, in contrast to other solutions mentioned here.

It's plain Java and does not require JAI. The source code and binary distributions are freely available at github.com/haraldk/TwelveMonkeys, and is covered by a BSD-style license.

Once you have it installed, it allows you to read CMYK JPEGs using ImageIO.read(...) like this:

File cmykJPEGFile = new File(/*path*/); BufferedImage image = ImageIO.read(cmykJPEGFile); 

I.e.: In most cases, it's not necessary to modify your code.

like image 78
Harald K Avatar answered Oct 05 '22 16:10

Harald K


Your image "Color Model" is CMYK, JPEGImageReader (the inner class that reads your file) reads only RGB Color Model.

If you insist on reading CMYK images, then you will need to convert them, try this code.

UPDATE

Read a CMYK image into RGB BufferedImage.

    File f = new File("/path/imagefile.jpg");      //Find a suitable ImageReader     Iterator readers = ImageIO.getImageReadersByFormatName("JPEG");     ImageReader reader = null;     while(readers.hasNext()) {         reader = (ImageReader)readers.next();         if(reader.canReadRaster()) {             break;         }     }      //Stream the image file (the original CMYK image)     ImageInputStream input =   ImageIO.createImageInputStream(f);      reader.setInput(input);       //Read the image raster     Raster raster = reader.readRaster(0, null);       //Create a new RGB image     BufferedImage bi = new BufferedImage(raster.getWidth(), raster.getHeight(),      BufferedImage.TYPE_4BYTE_ABGR);       //Fill the new image with the old raster     bi.getRaster().setRect(raster); 

UPDATE - March 2015 - Adding simulation images

Original images were removed from OP's dropbox. So I'm adding new images (not the originals) that simulates the problem that was happening with them.

First image is how a normal RGB image looks like.

Image RGB

Second image is how the same image will look like in CMYK color model.

You cannot actually see how it looks on the web because it will be converted to RGB by the host. To see exactly how it looks, take the RGB image and run it through an RGB to CMYK converter.

Third image is how the CMYK image will look like when read then written using Java ImageIO.

Image CMYK read through Java RGB

The problem that was happening with OP is they had something like image 2 which throws an exception when you try to read it.

like image 37
medopal Avatar answered Oct 05 '22 17:10

medopal