How to write an image to file – Java ImageIO

The “javax.imageio.ImageIO” is a handy class to read and write image into local system. In this example, we show you how to use “ImageIO” to read an image from an URL and write it into three file formats :

  1. “jpg”
  2. “gif”
  3. “png”

Full example to demonstrate the ImageIO usage.


package com.mkyong.image;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class WriteImage 
{	
    public static void main( String[] args )
    {
    	BufferedImage image = null;
        try {
          
            URL url = new URL("http://www.mkyong.com/image/mypic.jpg");
            image = ImageIO.read(url);
            
            ImageIO.write(image, "jpg",new File("C:\\out.jpg"));
            ImageIO.write(image, "gif",new File("C:\\out.gif"));
            ImageIO.write(image, "png",new File("C:\\out.png"));
            
        } catch (IOException e) {
        	e.printStackTrace();
        }
        System.out.println("Done");
    }
}

Reference

  1. http://java.sun.com/j2se/1.4.2/docs/api/javax/imageio/ImageIO.html

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
sanjana
Guest
sanjana

Nice tutorial. It helped me a lot.

nextwebinsight
Guest
nextwebinsight

It is not working for the url – https://lh5.ggpht.com/FJsccpuphUKRxZTFzTiXXgTO4Vqi8IRmW6oRLmzZpY90UVmi1EwC9Y9fyYidPnPCxE0=w300-rw

Please let me know if any fix.

Yashwanth
Guest
Yashwanth

Hi i am not able write the pixel values to rgb image properly.. after writing the pixel value to image by using ImageIO.write..the pixel value have changed while reading it..

RAFEE SHAIK
Guest
RAFEE SHAIK

I am not able read the image from ur; “http://www.hdwallsource.com/img/2014/7/harley-davidson-wallpaper-16885-17439-hd-wallpapers.jpg”

aCard Marring
Guest
aCard Marring

Thanks for sharing this nifty code :DD

ram
Guest
ram

Thanks,

but it is not woking for me. please help me, i am getting error shown in below.

javax.imageio.IIOException: Can’t get input stream from URL!
at javax.imageio.ImageIO.read(Unknown Source)
at Image.main(Image.java:17)

Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)

thanks for your understand

Shinu Varghese
Guest
Shinu Varghese

Hi,
How do you incorporate scribling of text onto an image in java…..

Shinu Varghese
Guest
Shinu Varghese

I have a java project in which i have to scribble text using a writing pad onto an existing image in a label…
please help me on the above case……

walid
Guest
walid

After converting the image to 0101 and encrypted, I can not bring it back as an image please help

Amit
Guest
Amit
I am trying to upload a file path through action class to the server from local system.I have done it statically if(customerBean.getUploadFileFileName() != null) { String filePath = “E:\\photos\\customer\\”; String photoPath = “E:/photos/customer/”+customerBean.getUploadFileFileName(); File fileToCreate = new File(filePath, customerBean.getUploadFileFileName()); if(!fileToCreate.exists()) { setErrormessage(“Please check if Uploaded Photo exists in the Customer Folder”); return “failure”; } status = custDAO.insertUser(customerBean,photoPath); } Could you please help me out with a working code where i can upload images dynamically on the server and store the image path in server datatbase. Thanks, Amit.
ramesh
Guest
ramesh

Respected sir,
iam doing project on visual cryptography
i.e: based on two images generate another image. here one image is orinal image with text. another is indecipheralble form. generate another image is also indecipherable form.both are combine whatever the text in original image. that will be visible. plz help me sir. plz

ramesh
Guest
ramesh

based on two images generate another image here one image is orinal image with text another is indecipheralble form generate another image is also indecipherable form. plz help me sir

ramesh
Guest
ramesh

sir plz help me iam doing project on visual cryptography.i want code in java

Fernando
Guest
Fernando

When i user an image *.png and try to get the *.bmp the file generated is a 0 bytes file.

May be you could help me to know why is this happening??

Thanks

tom costa
Guest
tom costa

Above code works on my machine , thanks for sharing

Jason
Guest
Jason

Exception in thread “main” java.lang.IllegalArgumentException: im == null!

Below is the code :

 

BufferedImage bImageFromConvert = ImageIO.read(new ByteArrayInputStream(binaryStream));		
ImageIO.write(bImageFromConvert, "jpg", new File("D:\\"+ fileName +".jpg")); 

swastik
Guest
swastik

Hi,
For Java its working, while I try to put the code for a managed bean in JSF in NetBean IDE the file is created under build\web directory instead of web. Can you please help on this

FacesContext con = FacesContext.getCurrentInstance();
ExternalContext contx = con.getExternalContext();
String path = contx.getRealPath(“/”);
URL url = new URL(“http://www.mkyong.com/image/mypic.jpg”);
image = ImageIO.read(url);

ImageIO.write(image, “jpg”,new File(path +”out.jpg”));

Thanks