English 中文(简体)
Is there a clever way to compress images to a max filesize instead of a quality level?
原标题:

Google App Engine gets angry about image files over 1M. http://code.google.com/appengine/docs/python/images/overview.html#Quotas_and_Limits

Given an arbitrary user-provided image, and the ability to run C code to pre-process it, is there any clever way to get down to 1M with maximum quality?

The brute-force way is to search for the compression level and resolution that result in a <1M file, by trying a series of JPEG quality settings and a series of scale factors.

Other than brute-force search, any ideas on a clever way to pick the best JPEG quality and scale factor to achieve 1M file size?

What might be some good heuristics, maybe assumptions about the percent compression achieved at various JPEG quality levels?

Brute force has the virtue of simplicity and likely it ll be fast enough anyway, but just curious.

最佳回答

the standard binary search assumes random data, which is certainly not the case here. A more efficient approach is to do linear interpolation. This function Size of Compressed Image (Size of uncompressed image) , like any sensible function, is linear given a small enough interval. So, on each interation, assume a linear response. This will come up with the answer DRAMATICALLY faster than the binary search. E.G. compressed at 50% quality, .75 M, so use (1/.75) * 50% ~ 62%. Lets say that results in an image of 1.5 M Now we have two points. (X = 50%, Y = .75 M) and (X=62%, Y= 1.5 M). The slope is (1.5-.75)/(62-50)=.75/12 So our second guess would be .25M X (12/.75)=4%, 50%+4%=54% Take the closest guesses so far, and repeat the process until the result makes you happy. You could use higher order interpolation, such as Newtons method, which would probably converge even faster.

问题回答

Simple alghorythm — create jpeg with 100 quality, if it is less than 1M, use it, if more, create with 50, if now less than 1M, than try 75, else try 25…

Dear Google AppEngine team:

Please eliminate the 1MB cap on image files. There are already quotas and pricing associated with storage, processing, bandwidth, etc to maintain developer incentives to keep file sizes down.

Thank you for everything.
Sincerely,

The developer community

This article from Jeff Atwood himself seems to imply that there is a way to "standardize on a JPEG compression factor of 15": A Comparison of JPEG Compression Levels and Recompression (I have not completely read the article, so I could have misunderstood the message when I gazed over it).

If you can set the compression factor, you can set the desired size.

The table on the Wikipedia article looks interesting. Qualtiy = 50 -> Compression factor = 15:1 (proven by empirical measurement on wikipedia :-) ... I am procrastinating, I should be doing something else right now...)





相关问题
Using QCView and iSight to capture image

I have a QCView that loads a Quartz file which gives you iSights feedback (basically like a QTCaptureView) Everything displays fine The button simply takes a snapshot using the following simple ...

Taking picture with QTCaptureView

Is it possible to simply take a picture and save it somewhere using a QTCaptureView and Apple s built-in iSight? I ve seen lots of tutorials on recording video but none on simply taking a picture. Any ...

Transform rectangular image into trapezoid

In .NET how can I transform an image into a trapezoid. The Matrix class supports rotation, shear, etc, but I can t see a trapezoidal transformation. I m using the usual System.Drawing.* API, but I m ...

Rotate image through Y-axis on web page

What are my options for rotating an image on a web page through the Y-axis? I m not sure if I even have the terminology right. Is this called a rotate or a transform. Most of the searches that I ve ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

Convert from 32-BPP to 8-BPP Indexed (C#)

I need to take a full color JPG Image and remap it s colors to a Indexed palette. The palette will consist of specific colors populated from a database. I need to map each color of the image to it s "...

热门标签