Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Decoding, resizing, then re-encoding, large JPG with memory straints

74 views
Skip to first unread message

davedosanjh

unread,
Jan 22, 2010, 10:05:32 PM1/22/10
to
I need to resize a JPG without ever reading the entire image into
memory. I figure it must be possible, but I know nothing of
compression algorithms, so I'm not sure where to start. Examples in
any language would be appreciated, if any exist.

Eventually this would be implemented in a Java applet in order to
resize images client side prior to uploading. The code must work in a
default JRE without an extended heap, and support images as big as
3000x3000px.

Is anyone aware of code that does this? Thanks.

George Johnson

unread,
Jan 23, 2010, 1:15:30 AM1/23/10
to
"davedosanjh" <davidd...@gmail.com> wrote in message
news:d10c01f3-188a-4583...@a32g2000yqm.googlegroups.com...

Is GOOGLE broke for you?

Upload the image to
http://www.resizemypicture.com/

You may upload JPG, GIF, or PNG images up to 3MB in size and 3000 pixels
wide.
How to Resize a Picture
To start your picture resize, simply click the "Browse..." (or "Choose
File" for Mac and Safari Users) button above under "Step 1".

After you have chosen your file, you can simply click the box that
shows one of our predefined widths and continue on. Or, you can click "More
Options" to click a height instead of a width, or specify your own custom
resized height and width.

If the picture you are resizing supports a quality setting (usually
only JPG or JPEG images support quality changes), you can click a lower
quality in order to lower the final file size (in bytes), or just leave it
at 100%.

Finally, click the "click here to resize" button. After your image is
resized, you can save it to your computer using the formon the next page

Resizing a photo has never been easier!
When to Resize a Picture
Sometimes, you need to resize your picture to fit in a given space, or
to be sent through email without being bounced for being too large. You may
also need to perform a picture resize if a forum or blog requires that your
"avatar" picture be set to a certain height and width. Other times, it can
be useful to resize a picture for a website rather than using the <img>
tag's width and height attributes. If you use the <img> tag to scale an
image, you may be using up bandwidth sending a large picture that is only
going to be resized anyway. Resizing the picture to its final dimensions can
also significantly decrease the amount of time it takes to download the
picture, since the file size will be smaller.
.

George Johnson

unread,
Jan 23, 2010, 1:17:40 AM1/23/10
to
"davedosanjh" <davidd...@gmail.com> wrote in message
news:d10c01f3-188a-4583...@a32g2000yqm.googlegroups.com...

Is GOOGLE broke for you?

http://www.inspire-soft.net/?nav=soft_easyimagemodifier
Overview


Change the resolution, orientation, format and name of a single image or a
collection in a comfortable and easy way.
Even advanced operations like removing meta informations, flipping, resizing
unproportionally or sorting your images are easy to manage.
Furthermore the used drag'n'drop technique simplifies the selection of
wanted files.
The key feature is the pure easiness. For example you can change the
resolution just with some clicks.


Main Features


a.. modify multiple images just with one single click
b.. rename and sort in various ways
c.. shrink and/or enlarge the resolution to a dimension or by a percentage
d.. change the orientation: flip or rotate the images
e.. optional changing of the file format
f.. extra options for jpg files: remove meta infos, set compression
g.. advanced resize options: ignore a dimension, resize unproportional
h.. rename & replace or save at a self-chosen location
i.. multilingualism: English, German, Spanish, French, Portuguese, Swedish
and Romanian
j.. portable: extremely small, just one executable, no installation
k.. free of charge!
Download


Version: 1.2
File: EasyImageModifier1.2.zip
http://www.inspire-soft.net/files/EasyImageModifier1.2.zip
Size: 226 KB

George Johnson

unread,
Jan 23, 2010, 1:25:43 AM1/23/10
to
"davedosanjh" <davidd...@gmail.com> wrote in message
news:d10c01f3-188a-4583...@a32g2000yqm.googlegroups.com...

Is GOOGLE broken for you?

GOOGLE incantation [ code custom resize images ]
http://www.google.com/#&q=code+custom+resize+images


Note you have mentioned JAVA, but have not specified which programming
language you prefer.
==========

http://weblogs.asp.net/gunnarpeipman/archive/2009/04/02/resizing-images-without-loss-of-quality.aspx
Custom resizing


Now let�s see the code that makes clean resizing. This code doesn�t use
GetThumbnailImage method and operates therefore on full size image. Also you
can see that this code tries to save as much quality as possible.


--------------------------------------------------------------------------------

public void ResizeImage(double scaleFactor, Stream fromStream, Stream
toStream)

{

var image = Image.FromStream(fromStream);

var newWidth = (int)(image.Width * scaleFactor);

var newHeight = (int)(image.Height * scaleFactor);

var thumbnailBitmap = new Bitmap(newWidth, newHeight);

var thumbnailGraph = Graphics.FromImage(thumbnailBitmap);

thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;

thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;

thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;

var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);

thumbnailGraph.DrawImage(image, imageRectangle);

thumbnailBitmap.Save(toStream, image.RawFormat);

thumbnailGraph.Dispose();

thumbnailBitmap.Dispose();

image.Dispose();

}


--------------------------------------------------------------------------------

And the resulting picture is here.

============

Quick Snip - Lightweight image resizer
By Aaron Sherman

Quick Snip is an incredibly lightweight application to make "web sized"
pictures out of large images

http://www.codeproject.com/KB/graphics/quick_snip.aspx


George Johnson

unread,
Jan 23, 2010, 1:31:29 AM1/23/10
to
"davedosanjh" <davidd...@gmail.com> wrote in message
news:d10c01f3-188a-4583...@a32g2000yqm.googlegroups.com...

Now if you're aiming for a custom resizing without much fuss, download:

http://www.irfanview.com/
And then download the Irfanview Plugins
http://www.irfanview.com/plugins.htm

One of the plugins is called "Save for Web".

Use it, click the "Compress to Size" button on the side if you need the
image to be a specific minimum file size.
Otherwise, if you click SAVE AS... and open the JPEG/GIF OPTIONS side
window there is a choice for [_] SET FILE SIZE [_____] KB (RIOT Plugin).


George Johnson

unread,
Jan 23, 2010, 1:52:25 AM1/23/10
to
"davedosanjh" <davidd...@gmail.com> wrote in message
news:d10c01f3-188a-4583...@a32g2000yqm.googlegroups.com...

And there are plenty of purchasable code locations to buy stuff from.
I figure you want Freebie Code so I've pointed you in that direction
first.
Learning basic image reduction code is actually fairly simple, but for
"not wasting time reinventing the wheel in a tire store" simplicity I would
suggest using GOOGLE first.

GOOGLE is your friend. Learn to use it.

You may ask yourself, "Why is this stranger being a very helpful royal
pain in the ass to me?"
Because you can find this information yourself with very little effort.
Granted you won't meet nice helpful people this way and you may meet
some very helpful jerks too...
...but you WILL remember these answers bit longer than if I just spat out
your answer like some wonderful wish machine from the heavens. Helpful
jerks teach lazy questioners to be a bit more incentivized toward learning
how to seek out answers on their own.

That knowledge will help you far more than a quick effortless easy
answer.
After all, if I did your Algebra homework for you, then what would you
do when you actually have to remember how to Algebra when you've been
coasting on the efforts of other helpful souls all this time?

=======

http://www.efg2.com/Lab/Library/Delphi/Graphics/ImageProcessing.htm
Envision Image Library provides image file I/O for popular file formats
(TIFF, PNG, JPEG, BMP, PCX, TGA, ICO, WFM, EMF), printing, scanning and
image processing features. Twain scanning. Image processing: anti-aliasing,
brightness, contrast, convert to gray, edge detection, emboss, sharpen, flip
vertical and horizontal, gamma adjustment, negative, interpolated resize,
rotate 90, 180, 270 and arbitrary angle, cropping, format conversion
including Floyd Steinberg dithering and color quantization. For Delphi 3, 4
or 5, or Builder 4.

=======

Java image resizer servlet
I�m working on a photo gallery application running on Java 6 using Tomcat 6,
JQuery for the client side, and images and xml generated from Picasa. I
needed several sizes of images for thumbnails and animations and I wasn�t
about to create multiple image sizes with Fireworks (I�m a lazy developer).
Doing what every lazy developer does, I search Google for an image resize
solution that would run on the application server and give me the sizes that
I needed and take the manual work out of the equation. I found several PHP
examples and disjointed Java examples, but no complete solutions. So,
unfortunately I had to do some work to put something together.


Down to brass tax, here�s the image resizer servlet code: (copy paste, use
as you please)

http://greatwebguy.com/programming/java/java-image-resizer-servlet/

=======

Now for the costly SDK stuff.

http://www.aurigma.com/Products/GraphicsMilldotNET/PricingLicensing.aspx

Graphics Mill 5 Standard - SDK License $499.00
351 Graphics Mill 5 Pro - SDK License $1,299.00

========

http://www.componentsource.com/products/victor-image-processing-library/index.html
Load, edit, and save image files within your application. Victor Image
Processing Library gives your programs powerful image processing, color
reduction, display, TWAIN scanning, printing, file handling, and converting
capabilities. Victor Image Processing Library gives your applications the
ability to handle a wide variety of image file formats including BMP, JPG,
GIF, PCX, PNG, TGA, and TIFF. Use the Victor Image Processing Library to
crop, mirror, flip, resize, and rotate to any angle. Extensive image
processing is also included such as brightness, contrast, sharpness,
embossing, posterization, and watercolor painting. Capture Images via TWAIN
devices and even print them when finished. With Victor Image Processing
Library you can create desktop or online ASP.NET. Compatible with Visual
Basic, VB.NET, C/C++, C#, Java and many more DLL compatible enviroments.
Victor Library Static Linkable Library is optimised for use with C/C++ .
Source code available.

Victor Library with Source Code V6.13
$ 881.021 Developer License with Source Code


George Johnson

unread,
Jan 23, 2010, 2:03:26 AM1/23/10
to
"davedosanjh" <davidd...@gmail.com> wrote in message
news:d10c01f3-188a-4583...@a32g2000yqm.googlegroups.com...

Again, effortless search via GOOGLE...
GOOGLE [ code custom resize images "memory friendly" ]
http://www.google.com/#&q=code+custom+resize+images+%22memory+friendly%22
===============
High-Quality Image Resize with Java
Learn a trick to shrink images using the Java2D API, which creates
high-quality image icons for your applications.
http://www.componenthouse.com/article-20
===========
Stefan Krause
24-Feb-2008 09:00 GMT
Okay, but why not use a real high quality (and fast) algorithm?
http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

Romain Guy
26-Feb-2008 18:41 GMT
This is an interesting approach but unfortunately flawed. First of all you
setup a bunch of useless rendering hints (like antialiasing). But the real
problem is that a blur requires more operations than a bilinear resize. An
easy and fast way to produce good looking thumbnails is to scale the image
by half its size, using BILINEAR, until you reach the final size you want.

I won't go further into details but I highly recommend you to read Chris
Campbell's article on the matter (that Stefan already posted):
http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html

The algorithm proposed in Chris' article is *not* fancy at all. And it's
pretty fast. Chet and I timed the various approaches and discussed them at
length in our book Filthy Rich Client and in a couple of JavaOne
presentations and the progressive bilinear approach is very very fast.
==========
Hugo Teixeira
26-Feb-2008 20:41 GMT
@Romain Guy
Hi Romain, thanks for your nice feedback.
I agree with your comments and your expertise is very important at this
moment.
I just want to clarify some points that might have been misunderstood:

1 - The rendering hints in the resize() method aren't that useless. They do
produce better results (compared to the same method without them), although
not as good as a high-quality resize (the hints in the blur() method might
be useless though).

2 - You are right in the sense that a blur requires more operations than a
bilinear resize, but I don't run the blur in the original image. First I
reduce the size to height/width = 100 and then I run the blur. This is a
completely different situation and creates a whole new scenario for
performance analysis. So the question is: Is a blur in a small image faster
than having a bilinear resize run several times? The fact is, we don't know
the answer and it depends on the situation. So "flawed" might be a strong
word in this sense.

Anyway, I am not fully concerned about the fate of this approach since we
have a correct way of doing things. But I have to admit that it was nice to
develop it and funny at the same time due to the unusual implementation.

Best regards,
Hugo Teixeira

==========
((((USENET REPOSTING WARNING, copy the code from the linked webpage))))
http://www.componenthouse.com/article-20
((((USENET will probably introduce code-breaking line breaks if you are
doing a blind COPY+PASTE))))

KAMEL HADDAD
17-Jul-2008 10:21 GMT
I test the giving code but it give a bad quality, i founded an other java
code can give you a more better quality


package com.diwansoft.resizer;

import com.sun.image.codec.jpeg.JPEGImageEncoder;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.Kernel;
import java.awt.image.ConvolveOp;

public class ImageUtil {

public static void resize(File originalFile, File resizedFile, int newWidth,
float quality) throws IOException {

if (quality 1) {
throw new IllegalArgumentException("Quality has to be between 0 and 1");
}

ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;

int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);

if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth,
Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth,
Image.SCALE_SMOOTH);
}

// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();

// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null),
temp.getHeight(null),
BufferedImage.TYPE_INT_RGB);

// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();

// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();

// Soften.
float softenFactor = 0.05f;
float[] softenArray = {0, softenFactor, 0, softenFactor, 1-(softenFactor*4),
softenFactor, 0, softenFactor, 0};
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);

// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);

// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

param.setQuality(quality, true);

encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
}

// Example usage
public static void main(String[] args) throws IOException {
File originalImage = new File("C:\Documents and Settings\Kamal
Haddad\workspace\Flex\WebContent\projects\test\snapshots\fullsize\1.jpeg");
resize(originalImage, new File("C:\Documents and Settings\Kamal
Haddad\workspace\Flex\WebContent\projects\test\snapshots\fullsize\1-7.jpeg"),
400, 0.7f);
resize(originalImage, new File("C:\Documents and Settings\Kamal
Haddad\workspace\Flex\WebContent\projects\test\snapshots\fullsize\1-1.jpeg"),
400, 1f);
}
}
==========

Jamie
12-Nov-2008 19:36 GMT
@Michael
Hi Michael,
The Image.getScaledInstance is VERY slow.. I was using that initially and
was disappointed. The quality is good, but because it's done all in software
it is very slow.

I finally decided upon:
org.jdesktop.swingx.graphics.GraphicUtilities
and the GraphicUtilities.createThumbnail() method.
It is part of the SwingX download from http://swinglabs.org/

It uses multiple intermediate bi-linear resizes from a managed
bufferedimage's drawImage to resize in steps to preserve image quality.


See the following bug report with good comments about the getScaledImage()
method
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6196792

============
Devella
04-Feb-2009 23:01 GMT
I have developed a library called ThumbMaster which is specifically targeted
at Java programmers who need to resize images with JAI -- and get high
quality results. It is implemented as a new JAI interpolation, and so the
standard built-in operators (and their native acceleration) can all be used.
Image quality and file size are both nearly identical to the results of
ImageMagick, as they are both based heavily on the same image processing
algorithms.

For more information and a trial download, visit
http://www.devella.net/thumbmaster

==========
perils-of-image-getscaledinstance.html
Tue, 2007-04-03
Chris Campbell
http://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html


glen herrmannsfeldt

unread,
Jan 23, 2010, 4:52:50 AM1/23/10
to
davedosanjh <davidd...@gmail.com> wrote:

> I need to resize a JPG without ever reading the entire image into
> memory. I figure it must be possible, but I know nothing of
> compression algorithms, so I'm not sure where to start. Examples in
> any language would be appreciated, if any exist.

I am not sure this is what you ask, but I have thought at times
that I would like to be able to do simple operations on JPEG
without all the work that photoshop goes through.

First is a 90 degree rotation, which should be simple, though
likely time consuming. (The bits are likely all in the wrong
place for a fast rotation.)

Next: Crop, presumably along block boundaries so it is easy.

And finaly, as you ask, resize, sometimes called resample.

Maybe not what you ask, but a resample by a factor of eight
resulting in a high quality, if low resolution, should be easy.
The image is coded in 8 by 8 blocks, so decode each one and
recode as one pixel. You need to do anti-alias filtering,
but that shouldn't be so hard. It is much harder if you allow
any arbitrary factor.

-- glen

Noob

unread,
Jan 26, 2010, 4:44:47 AM1/26/10
to
Glen wrote:

> I am not sure this is what you ask, but I have thought at times
> that I would like to be able to do simple operations on JPEG
> without all the work that photoshop goes through.
>
> First is a 90 degree rotation, which should be simple, though
> likely time consuming. (The bits are likely all in the wrong
> place for a fast rotation.)
>
> Next: Crop, presumably along block boundaries so it is easy.
>
> And finaly, as you ask, resize, sometimes called resample.
>
> Maybe not what you ask, but a resample by a factor of eight
> resulting in a high quality, if low resolution, should be easy.
> The image is coded in 8 by 8 blocks, so decode each one and
> recode as one pixel. You need to do anti-alias filtering,
> but that shouldn't be so hard. It is much harder if you allow
> any arbitrary factor.

I think all these features are supported by the latest libjpeg.

http://jpegclub.org/

http://jpegclub.org/jpegtran/

1. Lossless resize feature
2. Lossless rotation and related transforms
3. Lossless crop 'n' drop (cut & paste)

http://jpegclub.org/djpeg/
http://jpegclub.org/cjpeg/

-scale M/N Scale image by fraction M/N
"Alter the nominal spatial resolution of a given source image
while compressing to JPEG without additional resampling."

Also of interest:
Direct DCT scaling to a fourth and to a half
http://jpegclub.org/jidctred/

Regards.

glen herrmannsfeldt

unread,
Jan 26, 2010, 4:49:37 AM1/26/10
to
Noob <ro...@127.0.0.1> wrote:
(snip, I wrote)

>> First is a 90 degree rotation, which should be simple, though
>> likely time consuming. (The bits are likely all in the wrong
>> place for a fast rotation.)

>> Next: Crop, presumably along block boundaries so it is easy.

>> And finaly, as you ask, resize, sometimes called resample.

(snip)



> I think all these features are supported by the latest libjpeg.

> http://jpegclub.org/

> http://jpegclub.org/jpegtran/

Maybe, I will have to look. But what I wanted was nice simple
programs to do each one. Especially the rotate and resize,
which should be very simple. If you do it with photoshop it
like to resample even when it isn't needed, and you end up
with a larger file than you started with for no reason at all.



> 1. Lossless resize feature
> 2. Lossless rotation and related transforms
> 3. Lossless crop 'n' drop (cut & paste)

> http://jpegclub.org/djpeg/
> http://jpegclub.org/cjpeg/

> -scale M/N Scale image by fraction M/N
> "Alter the nominal spatial resolution of a given source image
> while compressing to JPEG without additional resampling."

It is


> Also of interest:
> Direct DCT scaling to a fourth and to a half
> http://jpegclub.org/jidctred/

-- glen

human

unread,
Jan 26, 2010, 2:11:17 PM1/26/10
to
glen herrmannsfeldt wrote:
>
> > I think all these features are supported by the latest libjpeg.
>
> > http://jpegclub.org/
>
> > http://jpegclub.org/jpegtran/
>
> Maybe, I will have to look. But what I wanted was nice simple
> programs to do each one. Especially the rotate and resize,
> which should be very simple. If you do it with photoshop it
> like to resample even when it isn't needed, and you end up
> with a larger file than you started with for no reason at all.
>
> 1. Lossless resize feature
> 2. Lossless rotation and related transforms
> 3. Lossless crop 'n' drop (cut & paste)

Jpegcrop Windows application from above address is a simple and
easy to use program that supports all these features, see also:
http://www.softpedia.com/get/Multimedia/Graphic/Graphic-Editors/Jpegcrop.shtml
Screenshots:
http://www.softpedia.com/progScreenshots/Jpegcrop-Screenshot-91058.html

For a list of more programs see:

http://jpegclub.org/losslessapps.html

The lossless resize is pretty new, though,
so this will take a while to be adopted.
Note that Photoshop is not on the list.

Ciao
human

human

unread,
Jan 26, 2010, 3:01:49 PM1/26/10
to
See this also:

http://jpegclub.org/jpegcrop/

"Save With Zoom" is the new lossless resize feature.

ciao
human

0 new messages