开发者

fail reducing Image size using google app engine images java API

开发者 https://www.devze.com 2023-02-21 15:44 出处:网络
I want to reduce image size (in KB) when its size is larger than 1MB. when I apply the resize transformation with smaller width and smaller height the size of the transforme开发者_C百科d image (in b

I want to reduce image size (in KB) when its size is larger than 1MB.

when I apply the resize transformation with smaller width and smaller height the size of the transforme开发者_C百科d image (in bytes) is larger than the orig image.

The funny (or sad) part is even when I invoke the resize with the same width and height as the orig (i.e. dimensions are not changed) the size "transformed" image is larger than the orig

    final byte[] origData = .....;
    final ImagesService imagesService = ImagesServiceFactory.getImagesService();

    final Image origImage = ImagesServiceFactory.makeImage(oldDate);
    System.out.println("orig dimensions is " + origImage.getWidth() + " X " + origImage.getHeight());

    final Transform resize = ImagesServiceFactory.makeResize(origImage.getWidth(), origImage.getHeight());
    final Image newImage = imagesService.applyTransform(resize, origImage);
    final byte[] newImageData = newImage.getImageData();
    //newImageData.length > origData.length :-(


Image coding has some special characteristics that you are observing the results from. As you decode a image from its (file) representation, you generate a lot of pixels. The subsequent encoding only sees the pixels and does not know anything about the size of your original file. Therefore the encoding step is crusial to get right.

The common JPEG format, and also the PNG format, have different compression levels, i.e a quality setting. They can have this because they do lossy compressions. In general, images with a lot of details (sharp edges) should be compressed with high quality and blurry images with low quality; as you probably have seen, small images usually are more blurry and large images usually more sharp.

Without going into the techical details, this means that you should set the quality level accoring to the nature of your image, which also is determined by the size of the input image. In other words, if you encode a blurry image as a big file, you are wasting space, since you would get about the same result using less bytes. But the encoder does not have this information, so you have to configure it using the correct quality setting

Edit: In your case manually set a low quality for encoding if you started with a small file (compared to number of pixels) and then of course a high quality if the opposite is true. Do some experimentations, probably a single quality setting for all photos will be acceptable.


A pitfall I fell in was, that I requested PNG output ... and the image size didn't change either. The image service silently ignored quality parameter. According to a comment in implementation the quality parameter is considered only for JPEG.

0

精彩评论

暂无评论...
验证码 换一张
取 消