473,378 Members | 1,617 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Resizing GIF images: please help!

Hi there.

I need to write a simple program that reads a GIF image from an input
stream, resizes it, then writes it back to an output stream in the
same format (GIF). (JPEG input/output is good too, but GIF is
preferrable). JAI can do this sort of but can't write to GIF format.
Also, it looks too complicated for what I need; I'm a bit in a
hurry...

Is there a simple way of doing what I want? Is there a free,
open-source library that does just that? I think I may know how to
read/decode and scale the image, but I don't know how to encode it to
a new, resized GIF file and I don't have time to write my own encoder.
(In fact I can't afford more than an hour on this!)

Any help will be highly appreciated.

Regards,
Clyde
Jul 17 '05 #1
2 11225

On 15 Oct 2003 16:45:31 -0700, ce****@msn.com (Clyde Ellul) wrote or
quoted :
Is there a simple way of doing what I want? Is there a free,
open-source library that does just that?


There are utilities like photoshop and Paint Shop Pro that likely can
be automated to process a batch of images.

In Java you can convert images to what amount to a matrix of ints with
r+b+g+alpha 8bit components. You can then write those back in
formats.

See http://minpprod.com/jgloss/jimi.html and follow links for tools to
do that.

That still leaves you to manipulate the image bit by bit. Java of
course can scale images for you happily with drawImage.
--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Jul 17 '05 #2
Clyde,

I've written a quick image resizing class, ImageResizer, using the
javax.JavaIO class (requires JDK1.4) as you mentioned. The encoding/decoding
is not that complicated, but as you already know it does not support writing
GIFs (since the GIF format is owned by AOL?). It does however support
reading GIFs, but from the little I have tested the code the results of
encoding a read GIF is not that satisfactory. The best would be to use PNG
or JPG with this approach.

You could however have a look at this reference page, "List of Java
libraries to read and write image files":
http://www.geocities.com/marcoschmid...ge-coding.html ...for
other alternatives.

The ImageResizer class can be started with the following command:
java ImageResizer <source image file> <destination image file> <x scaling
factor> <y scaling factor>

The javax.JavaIO supports the following formats:
jpg (read/write), png (read/write), gif (read only)
The code:
// Millian Brave [mi******@start.no], 2003/10/16

import java.io.File;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import javax.imageio.ImageIO;
public class ImageResizer
{

/**
* Constructor.
* @param srcImgName the source image name
* @param dstImgName the destination image name
*/
public ImageResizer(String srcImgName, String dstImgName,
float scaleX, float scaleY, String imgOutputFormat)
{
BufferedImage srcImg = null, dstImg = null;

try {
srcImg = ImageIO.read(new File(srcImgName));
dstImg = resizeImage(srcImg, scaleX, scaleY);
ImageIO.write(dstImg, imgOutputFormat, new File(dstImgName));
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Resizes the image according to the given scaling factors.
* @param srcImg the image to be resized
* @return resized image
*/
public BufferedImage resizeImage(BufferedImage srcImg,
float scaleX, float scaleY)
{
// get image dimensions
int srcW = srcImg.getWidth();
int srcH = srcImg.getHeight();
int dstW = (int) (srcW * scaleX);
int dstH = (int) (srcH * scaleY);

// Get data structures
BufferedImage dstImg = new BufferedImage(dstW, dstH, srcImg.getType());
Raster srcRaster = srcImg.getRaster();
WritableRaster dstRaster = dstImg.getRaster();
double[] tmpPix = {0, 0, 0};

// resize image
for (int y=0; y<dstH; y++) {
for (int x=0; x<dstW; x++) {
int xPos = (int) (x * (1/scaleX)); // (find corresponding src x pos)
int yPos = (int) (y * (1/scaleY)); // (find corresponding src y pos)
tmpPix = srcRaster.getPixel(xPos, yPos, tmpPix);
dstRaster.setPixel(x, y, tmpPix);
}
}

return dstImg;
}
/**
* Application starting point.
* @param argv <p>argv[0] --> the source image name</p>
* <p>argv[1] --> the destination image name</p>
* <p>argv[2] --> x scaling factor</p>
* <p>argv[3] --> y scaling factor</p>
*/
public static void main(String[] argv)
{
if (argv.length == 5) {
float scaleX = Float.parseFloat(argv[2]);
float scaleY = Float.parseFloat(argv[3]);
ImageResizer imageResizer = new ImageResizer(argv[0], argv[1],
scaleX, scaleY,
argv[4]);
}
else {
System.err.println("usage: java ImageResizer " +
"<srcImg> <dstImg> <scaleX> <scaleY> <output
format>");
System.exit(1);
}

}

}
---
Regards,
Millian Brave <mi****************@start.no>

"Clyde Ellul" <ce****@msn.com> skrev i melding
news:fd**************************@posting.google.c om...
Hi there.

I need to write a simple program that reads a GIF image from an input
stream, resizes it, then writes it back to an output stream in the
same format (GIF). (JPEG input/output is good too, but GIF is
preferrable). JAI can do this sort of but can't write to GIF format.
Also, it looks too complicated for what I need; I'm a bit in a
hurry...

Is there a simple way of doing what I want? Is there a free,
open-source library that does just that? I think I may know how to
read/decode and scale the image, but I don't know how to encode it to
a new, resized GIF file and I don't have time to write my own encoder.
(In fact I can't afford more than an hour on this!)

Any help will be highly appreciated.

Regards,
Clyde

Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: lawrence | last post by:
Can someone please tell me where I can get some open source code for resizing images? I know such code has been written a million times and I don't feel like doing it again from scratch.
4
by: Matthias Czapla | last post by:
Hi! I want to resize uploaded GIF images. Currently I do it like this: <?php // ... $img = imagecreatefromgif($path); $img_scaled = imagecreate($new_width, $new_height);...
2
by: Alex Hopson | last post by:
I'm using the code below to loop through some images and resize each image twice, once to create a thumbnail and once to create a small image. The page stops loading around the 38th image out of...
10
by: riki | last post by:
Hi, i have a big problem...i'm using one jscript for resizing of all of my pics in popUp...in main html i'm having many little pics and clicking on them they open in popUp and resize to larger...
13
by: Jon Yeager | last post by:
I need to display a bunch of pictures that are all of various dimensions into a fixed dimension space, like MSN Messenger does with its user photos in the chat windows. Forcing image dimensions...
5
by: Jim | last post by:
I've heard that resizing images through PHP (either GD2 or ImageMagick) is a processor intensive exercise. I'm setting up a site where users will be uploading up to 10 images along with the details...
0
by: E | last post by:
I wrote code which Resizes images and then saves them below is the 2 methods. The problem is that half of the resized image turns out gray, as if it wasn't completed. Could someone please tell me...
6
by: neverstill | last post by:
hi- So I wrote this nice little page that will allow the managers to add images to the products table. Without too many details to confuse everything, basically what I'm doing is: getting an...
10
by: mishrarajesh44 | last post by:
hii all, I am facing a problem currently.. i have a script for image uploading and resizing.. the image uploading takes place properly for every size images.. but, the resizing works for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.