Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Convert PNG to GIF in Java

  1. #1
    Join Date
    Oct 2008
    Location
    White House
    Beans
    361

    Convert PNG to GIF in Java

    Hi folks,
    how do I convert (say the attached TFT.png image) to a GIF without losing the transparency.
    Using Gimp there's no problem doing it, I just go to File->Save as a gif image and I'm done, the result is attached as TFT.gif.

    However Java replaces the transparent pixels with black ones when saving to GIF, see image TFT.java.gif

    Does anyone know how to solve this issue?

    Heres' the code I'm using:
    Code:
    ...
    // File f is a PNG file
    FileInputStream fin = new FileInputStream(f);
    BufferedImage image = ImageIO.read(fin);
    //now the extension becomes .gif
    String sPath = f.getPath() + ".gif";
    //saving as GIF..
    ImageIO.write(image, "GIF", new File(sPath));
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2008
    Beans
    3

    Re: Convert PNG to GIF in Java

    I have exactly the same problem.

    Did you find a solution?

  3. #3
    Join Date
    Jul 2008
    Beans
    1,491

    Re: Convert PNG to GIF in Java

    There are multiple kinds of color models for BufferedImage; in particular: those that work with ARGB colors and those that use RGB colors. Read the API docs.

  4. #4
    Join Date
    May 2009
    Beans
    303

    Re: Convert PNG to GIF in Java

    Quote Originally Posted by Reiger View Post
    There are multiple kinds of color models for BufferedImage; in particular: those that work with ARGB colors and those that use RGB colors. Read the API docs.
    There's even more than that, but can anyone actually give a working example?

  5. #5
    Join Date
    Jun 2008
    Beans
    3

    Re: Convert PNG to GIF in Java

    Jepp...i tried it with ARGB right from the beginning...
    ...but it didn't work.

    It seems, that the color i defined for the Text (via GlyphVector) later on in the code is also becoming the background color. I tried several things, but none worked.

    ...its kinda frustrating

  6. #6
    Join Date
    Aug 2005
    Beans
    85

    Re: Convert PNG to GIF in Java

    http://www.eichberger.de/2007/07/tra...s-in-java.html has code and comment on Sun's "support" for gif and IndexColourModel.

  7. #7
    Join Date
    May 2009
    Beans
    303

    Re: Convert PNG to GIF in Java

    Folks, according to my test this bug has been fixed in Java 7, I ran this code:
    Code:
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileInputStream;
    import javax.imageio.ImageIO;
    
    public class Main {
    
        public static void main(String[] args) {
            try {
                File f = new File("/home/fox/Desktop/TFT.png");
                FileInputStream fin = new FileInputStream(f);
                BufferedImage image = ImageIO.read(fin);
    //now the extension becomes .gif
                String sPath = f.getPath() + ".gif";
    //saving as GIF..
                ImageIO.write(image, "GIF", new File(sPath));
                System.out.println("Done saving.");
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    }
    And Java 7 (latest build), unlike Java 6, actually yields a transparent gif file as expected, without extra efforts! See attached images for comparison between Java 7 and Java 6 output.

    Get latest Java/JDK 7 builds:
    http://download.java.net/jdk7/binaries/

    PS: the "fullscreen" image preview feature on this forum seems to not be able to handle transparent gifs, so save the files and open them to see the difference.
    Attached Images Attached Images
    Last edited by froggyswamp; June 11th, 2009 at 07:18 AM.

  8. #8

    Re: Convert PNG to GIF in Java

    Why are you using giff anyway? it only supports 1 bit transparency so looks ugly and aliased. If you want a palliated format, use a pelleted png, which dousent suffer from the uglyness caused by one bit transparency.
    Last edited by hessiess; June 11th, 2009 at 08:07 AM.
    im dyslexic, please don't comment on my spelling
    blender 3d artist, visit my portfolio
    Quad-Ren, Open source, resolution independent 2D graphics engine
    Screen space is a precious resource, don't waste it

  9. #9
    Join Date
    May 2009
    Beans
    303

    Re: Convert PNG to GIF in Java

    Quote Originally Posted by hessiess View Post
    Why are you using giff anyway? it only supports 1 bit transparency so looks ugly and aliased. If you want a palliated format, use a pelleted png, which dousent suffer from the uglyness caused by one bit transparency.
    You are absolutely right.
    But people need it for example because of Internet Explorer 6, which doesn't support transparent PNGs, thus for such users you can (have to) convert them on the fly (or cache them) as transparent GIFs.
    When such bad software like IE6 goes into oblivion we will need this feature (much) less, there are already a few % of web devs who dropped support for IE6, which is good.

    PS: also, those JavaScript fixes for IE's transparency issues in my experience don't always work as expected so I have to "fix" it on the server side.
    Last edited by froggyswamp; June 11th, 2009 at 09:53 AM.

  10. #10

    Re: Convert PNG to GIF in Java

    Quote Originally Posted by froggyswamp View Post
    You are absolutely right.
    But people need it for example because of Internet Explorer 6, which doesn't support transparent PNGs, thus for such users you can (have to) convert them on the fly (or cache them) as transparent GIFs.
    When such bad software like IE6 goes into oblivion we will need this feature (much) less, there are already a few % of web devs who dropped support for IE6, which is good.

    PS: also, those JavaScript fixes for IE's transparency issues in my experience don't always work as expected so I have to "fix" it on the server side.
    Sure, but IE6 is finally going the way of the Dodo, for example http://www.w3schools.com/browsers/browsers_stats.asp. And as its such a pain in the *** to develop for, why bother?
    im dyslexic, please don't comment on my spelling
    blender 3d artist, visit my portfolio
    Quad-Ren, Open source, resolution independent 2D graphics engine
    Screen space is a precious resource, don't waste it

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •