[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 690
  • Last Modified:


heya, i have written a java program which takes in a jpg, gif or png, adds text to that image then saves it as another file.

everything works fine, excecpt for transparent *.gif files.

Is it possible to read transparent gifs in java? i have the 1.4. something sdk.

worthy of note, is that i can open a jpg and save it as a gif fine.

code posted below:

import java.awt.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;

public class ImageMod{
      public static void main(String[] args) throws Exception {
             * takes in 3 arguments;
             * input file
             * output file
             * text to be added to output file
            if (args.length != 3) {
                  System.err.println("Usage: 3 arguments: args[0] file input, args[1] file output, args[2] the text");
            // load input image
            BufferedImage image = ImageIO.read(new File(args[0]));
            // do the text
            Graphics graphics = image.getGraphics();
            //Color color = new Color(Color.BLUE);
            Font theFont = new Font("Times", Font.PLAIN, 18);
                  10, image.getHeight() - 10);
            // save modified image
            String format = "JPG";
            if (args[0].toLowerCase().endsWith(".png")) {
                  format = "PNG";
          if (args[0].toLowerCase().endsWith(".gif")) {
                  format = "GIF";
            ImageIO.write(image, format, new File(args[1]));
            // System.exit(0)
  • 6
  • 6
1 Solution
what exactly is the problem?
twansAuthor Commented:
>>everything works fine, excecpt for transparent *.gif files.
>>Is it possible to read transparent gifs in java? i have the 1.4. something sdk.

sorry, maybe i should have explained a little better.

When i take a transparent GIF as my input , after modifying and saving. the output.gif has a zero byte size.

As i can take a jpg and store it as a GIF i presume its not the saving in *.GIF format but rather reading GIFs.

i was wondering whether i am reading the GIF correctly (and if this method is possible for transparent gifs) with

ImageIO.write(image, format, new File(args[1]));

and if you can write transparent gifs, or if ImageIO will automatically add a background.


i don't think imageio supports writing gifs.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

twansAuthor Commented:
it also seems that ImageIO doesnt supoprt transparent files in its read method.

any ideas of ways around it?

unfortunatnly transparent gifs are essential for the application.
what makes you think it doesn't support reading transparent gifs?
twansAuthor Commented:
if i use the following input:


it creates the output.gif as a zero byte file (obviously unreadable)

furthermore, if you do

"input.gif","output.jpg","objects" it also creates a zero byte sized file output.jpg

however, if the input is input.jpg, regardless of the output (.png, .gif, .jpg) it will always work.

hence i am leaning towards the possiblility the transperancy and/or reading of a gif is what is not supported.

any ideas would be much appreciated

> "input.gif","output.gif","objects"
> it creates the output.gif as a zero byte file (obviously unreadable)

no it can't write it, have you tried displaying the read image.

twansAuthor Commented:
havnt tried displaying the read image >> will do that now.

>>no it can't write it

like i said, if you use input.jpg as the input and output to a GIF it works, whether its properly encoded as a GIF im not sure as im no specialist with imagining, however it successfully displays in a webpage when calling <img src="whatever.gif">.
try the following to see what is supported:

twansAuthor Commented:
great. thanks for that link. backed up exactly what you were saying :)

still interesting that although the ImageIO is meant to support gif's as readable.. they are causing the zero byte files to be saved (reguardless of the format, jpg etc)

it is also interesting to note that this problem with reading gif's is not dependent upon transperency.

this issue arises when displaying the gif within a Toolkit viewer (taking your advice to display the input file, before outputting).

so what can this be said about java's 'ability' to read gif's through the imageio.

pleaes understand i have tried (and still am) furiously on google to answer some of my questions, but i cannot unfortunantly find anything suitable to my questions.

twansAuthor Commented:
i am sorry, my previous post is incorrect.

the gif is supported with both transparent and normal backgrounds for viewing.

i have found a java gif encoder and will pursue trying to use that to solve my issue.

thankyou objects for your help


no worries :)

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now