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

resize a gif image using java

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
hanoirules
Asked:
hanoirules
  • 9
  • 8
  • 5
  • +2
4 Solutions
 
CEHJCommented:
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
 
hanoirulesAuthor Commented:
I need to produce more files with different sizes starting from a given file.gif.. I'm stuck to this..
0
 
CEHJCommented:
Get one working first ;-)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TimYatesCommented:
Can you post the code which isn't working?
0
 
kawasCommented:
0
 
TimYatesCommented:
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
 
hanoirulesAuthor Commented:
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
 
TimYatesCommented:
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
 
TimYatesCommented:
Then call:

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

or

  resizeImage( "D:/zipBig.gif", "D:/zipBig_rescaled.gif", "gif" ) ;
0
 
hanoirulesAuthor Commented:
Same result: the resized img comes out empty :-((
0
 
TimYatesCommented:
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
 
CEHJCommented:
>>public static void resizeImage( String filename, String scaledName, String type )

Did you try passing "PNG" as 'type' there?
0
 
hanoirulesAuthor Commented:
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
 
TimYatesCommented:
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
 
CEHJCommented:
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
 
objectsCommented:
> it work if I used PNG!

naturally :) as will jpg. I thought the problem was saving it as a gif.
0
 
TimYatesCommented:
>> 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
 
hanoirulesAuthor Commented:
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
 
objectsCommented:
> 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
 
TimYatesCommented:
Yeah, just use the gif encoder to encode your resized object...

Basically replace ImageIO.write with the code for the encoder...
0
 
hanoirulesAuthor Commented:
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
 
CEHJCommented:
:-)
0
 
hanoirulesAuthor Commented:
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
 
kawasCommented:
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
 
TimYatesCommented:
You'll need to use a TYPE_INT_ARGB buffered image as your canvas as well
0
 
hanoirulesAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 8
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now