Solved

resize a gif image using java

Posted on 2007-03-21
27
5,809 Views
Last Modified: 2013-12-29
Hi everybody, I need to resize a gif image in various size using java.
I've tried in a lot of way: everything works fine for jpg format, but when I try with a gif format, my file comes out with size 0.
Does anybody have an idea about the path to follow to resize a gif?
Thanks in advance
0
Comment
Question by:hanoirules
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 5
  • +2
27 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 150 total points
ID: 18764461
Can you read the gif and display it at normal size? If so, write it as PNG instead of gif if there's a problem
0
 

Author Comment

by:hanoirules
ID: 18764524
I need to produce more files with different sizes starting from a given file.gif.. I'm stuck to this..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18764545
Get one working first ;-)
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 35

Expert Comment

by:TimYates
ID: 18764547
Can you post the code which isn't working?
0
 
LVL 13

Assisted Solution

by:kawas
kawas earned 50 total points
ID: 18764662
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18764708
I've got code running on the machine next to me which is resizing gifs as we speak...

Millions of them

PNGs are better as CEHJ says, as you can use smooth scaling rather than nearest neighbour, but it is perfectly possible...  Which implies there's something wrong with your code
0
 

Author Comment

by:hanoirules
ID: 18765005
Here is my code for test working with jpg:  
  public static BufferedImage toBufferedImage(Image image, int type) {
        int w = image.getWidth(null);
        int h = image.getHeight(null);
        BufferedImage result = new BufferedImage(w, h, type);
        Graphics2D g = result.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return result;
    }
   
    public static void resizeJpeg(){
      try{
        BufferedImage image = ImageIO.read(new File("D:/zipBig.jpg"));
        int w = image.getWidth(), h = image.getHeight();
        Image rescaled = image.getScaledInstance(w/2, h/2, Image.SCALE_AREA_AVERAGING);
        BufferedImage biRescaled = toBufferedImage(rescaled, BufferedImage.TYPE_INT_RGB);
        ImageIO.write(biRescaled, "jpg", new File("D:/zipBig_rescaled.jpg"));
      }
      catch(Exception e){
        e.printStackTrace();
      }
    }
Anyway, file format is not my choice and not even a decision I can discuss :-)).. Unfortunatelly
0
 
LVL 35

Assisted Solution

by:TimYates
TimYates earned 150 total points
ID: 18765133
As you know the width/height, why not pass them into your bufferedimage function?

  public static BufferedImage toBufferedImage(Image image, int type, int w, int h ) {
        BufferedImage result = new BufferedImage(w, h, type);
        Graphics2D g = result.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return result;
    }
   
    public static void resizeImage( String filename, String scaledName, String type ){
      try{
        BufferedImage image = ImageIO.read( new File( filename ) ) ;
        int w = image.getWidth(), h = image.getHeight() ;
        Image rescaled = image.getScaledInstance( w/2, h/2, Image.SCALE_AREA_AVERAGING ) ;
        BufferedImage biRescaled = toBufferedImage( rescaled, BufferedImage.TYPE_INT_RGB, w / 2, h / 2 );
        ImageIO.write( biRescaled, type, new File( scaledName ) ) ;
      }
      catch(Exception e){
        e.printStackTrace();
      }
    }
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18765164
Then call:

  resizeImage( "D:/zipBig.jpg", "D:/zipBig_rescaled.jpg", "jpg" ) ;

or

  resizeImage( "D:/zipBig.gif", "D:/zipBig_rescaled.gif", "gif" ) ;
0
 

Author Comment

by:hanoirules
ID: 18765224
Same result: the resized img comes out empty :-((
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18765878
Try:

  public static BufferedImage toBufferedImage(Image image, int type, int w, int h ) {
        BufferedImage result = new BufferedImage(w, h, type);
        Graphics2D g = result.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.setColor( Color.RED ) ;
        g.fillRect( 0, 0, 32, 32 ) ;
        g.dispose();
        return result;
    }

Do you get the red rectangle?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18766317
>>public static void resizeImage( String filename, String scaledName, String type )

Did you try passing "PNG" as 'type' there?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 150 total points
ID: 18767388
0
 

Author Comment

by:hanoirules
ID: 18779154
Hi to everybody!
For Tim: nope! I do not get the red rectangle!
For Cehj: it work if I used PNG!
For objects:I'm going to try your solutions!
I'll let you know!
Thanks!!
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18779277
Oh...hang on...  I'm being a moron...

Java 5 and below cannot write GIFs with ImageIO.write

Java 6 can.

You will have to use one of objects' gif encoding classes to write GIFs out if you cannot use Java 6

Sorry it took so long for my slow brain to get up to speed :-(

I forgot I was using Java 6 :-/
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18779336
If you're not committed to gif - forget it - you can do it all with png, although one caution- some versions (at least) of IE don't support transparency in png
0
 
LVL 92

Expert Comment

by:objects
ID: 18779349
> it work if I used PNG!

naturally :) as will jpg. I thought the problem was saving it as a gif.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18779382
>> some versions (at least) of IE don't support transparency in png

You can get round this with filters in IE (but it feels a bit hacky)
0
 

Author Comment

by:hanoirules
ID: 18779511
Hi again: objects' code is perfect if you want  to make your own animated gif; what I was looking for is a gif resizer; Cehj suggestion works great on simple gif; now i working on animated gif ...
0
 
LVL 92

Expert Comment

by:objects
ID: 18779558
> what I was looking for is a gif resizer

you can use the encoders I posted to resize.
Just load your gif, resize the image, then use the encoder to save the reisized gif.
Theres no need to change the format.
Let me know if you have any questions.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18779619
Yeah, just use the gif encoder to encode your resized object...

Basically replace ImageIO.write with the code for the encoder...
0
 

Author Comment

by:hanoirules
ID: 18779863
you already know my "thickness": I just realized that to resize an animated gif i have to extract any sigle frame, performe the resize and add them one by one to the target rescaled gif.. Arghh!
So, the gif encoder should be just perfect:I'll try! I hope that my (few) neurons can organize a nice synapsis and make it! :-)))
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18799330
:-)
0
 

Author Comment

by:hanoirules
ID: 18822884
Hi again! I'm facing another problem while encoding the new resized animated gif: the bgcolor comes out always black instead of transparent.. Any clues?where is my mistake?
Anyway, the decoder/encoder works really well!
0
 
LVL 13

Expert Comment

by:kawas
ID: 18823182
http://www.sun.com/software/imaging/JAI/11/bugs_codec.html:

'The GIF decoder does not handle GIF files with transparent background correctly. It ignores the background value if it's marked as transparent and the image comes out with a black background regardless of the original background index palette entry.'
0
 
LVL 35

Expert Comment

by:TimYates
ID: 18823198
You'll need to use a TYPE_INT_ARGB buffered image as your canvas as well
0
 

Author Comment

by:hanoirules
ID: 18823997
Hi, let me describe what is happening: my animated gif is composed of 4 frame: if i write the single frame, is just perfect. When I put them all together, 1st and 3rd are correct, the others are "in black".
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question