Solved

resize a gif image using java

Posted on 2007-03-21
27
5,662 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
changeXy challenge 13 56
wordmultiple challenge 12 89
Starting to learn JAVA, 7 47
create a gui in perl 3 42
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now