Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a good quality thumbnail
    primarykey
    data
    text
    <p>I am trying to create a <strong>high quality thumbnail</strong> of this image, with Java and <a href="http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/" rel="noreferrer">Scalr 3.2</a></p> <p><img src="https://i.stack.imgur.com/ajedh.jpg" alt="full scale image"></p> <p>This is the relevant source code, where <code>THUMB_WIDTH = 77</code> and <code>THUMB_HEIGHT = 57</code></p> <pre><code>BufferedImage srcImg = ImageIO.read(new File(sourceFile)); BufferedImage dstImg = Scalr.resize(srcImg, Scalr.Method.QUALITY, THUMB_WIDTH, THUMB_HEIGHT); ImageIO.write(dstImg, format, new File(destFile)); </code></pre> <p>If I use <code>format = "png"</code>, here is the result:</p> <p><img src="https://i.stack.imgur.com/x4HAz.png" alt="png thumbnail"></p> <p>If I use <code>format = "jpg"</code>, here is the result:</p> <p><img src="https://i.stack.imgur.com/xw7iD.jpg" alt="jpg thumbnail"></p> <p>With <a href="http://www.imagemagick.org/script/identify.php" rel="noreferrer">imagemagick identify</a> I've found out that the JPEG is saved with a quality of 75 that is totally insufficient to create a good looking thumbnail. The PNG doesn't look good either to me.</p> <p>Here is the output of identify of the original file and the two thumbnails:</p> <pre><code>$ identify 42486_1.jpg 42486_s1.jpg 42486_s1.png 42486_1.jpg JPEG 580x435 580x435+0+0 8-bit DirectClass 50.6KB 0.000u 0:00.000 42486_s1.jpg[1] JPEG 77x58 77x58+0+0 8-bit DirectClass 2.22KB 0.000u 0:00.000 42486_s1.png[2] PNG 77x58 77x58+0+0 8-bit DirectClass 12.2KB 0.000u 0:00.000 </code></pre> <h2>Questions</h2> <ul> <li>How to improve the quality of the generated thumbnail? </li> <li>How to save a JPEG with a higher quality? I'd like to try with higher quality and compare the results. I couldn't find anything in the JavaDoc for ImageIO.write. </li> <li>Why I tell Scalr that my maximum dimensions are 77x57 and it output an image 77x58? I think that is to maintain the proportion, but those are my maximum width and maximum height. Width or height could be less but not more.</li> </ul> <p><strong>UPDATE</strong>: With a web search I found an article about how to <a href="http://www.universalwebservices.net/web-programming-resources/java/adjust-jpeg-image-compression-quality-when-saving-images-in-java" rel="noreferrer">adjust JPEG image compression quality</a>. I wrote my own method to save a BufferedImage setting the quality:</p> <pre><code>/** * Write a JPEG file setting the compression quality. * * @param image * a BufferedImage to be saved * @param destFile * destination file (absolute or relative path) * @param quality * a float between 0 and 1, where 1 means uncompressed. * @throws IOException * in case of problems writing the file */ private void writeJpeg(BufferedImage image, String destFile, float quality) throws IOException { ImageWriter writer = null; FileImageOutputStream output = null; try { writer = ImageIO.getImageWritersByFormatName("jpeg").next(); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(quality); output = new FileImageOutputStream(new File(destFile)); writer.setOutput(output); IIOImage iioImage = new IIOImage(image, null, null); writer.write(null, iioImage, param); } catch (IOException ex) { throw ex; } finally { if (writer != null) writer.dispose(); if (output != null) output.close(); } } </code></pre> <p>Here are the results. PNG:</p> <p><img src="https://i.stack.imgur.com/x4HAz.png" alt="png thumbnail"></p> <p>JPEG quality 75:</p> <p><img src="https://i.stack.imgur.com/xw7iD.jpg" alt="jpg thumbnail"></p> <p>JPEG quality 90 (the gravatars on stackoverflow are saved as JPEG quality 90):</p> <p><img src="https://i.stack.imgur.com/pLpxS.jpg" alt="jpg thumbnail quality 90"></p> <p>and the filesize:</p> <pre><code>thumb90.jpg JPEG 77x58 77x58+0+0 8-bit DirectClass 6.89KB 0.000u 0:00.000 </code></pre> <p><strong>UPDATE 2</strong>: test to compare Scalr with <a href="http://code.google.com/p/java-image-scaling/" rel="noreferrer">java-image-scaling</a>.</p> <pre><code>private void scaleAndSaveImageWithScalr(String sourceFile, String destFile, int width, int height) throws IOException { BufferedImage sourceImage = ImageIO.read(new File(sourceFile)); BufferedImage destImage = Scalr.resize(sourceImage, Scalr.Method.QUALITY, width, height); writeJpeg(destImage, destFile, JPEG_QUALITY); } private void scaleAndSaveImageWithJImage(String sourceFile, String destFile, int width, int height) throws IOException { BufferedImage sourceImage = ImageIO.read(new File(sourceFile)); ResampleOp resampleOp = new ResampleOp(width, height); resampleOp.setFilter(ResampleFilters.getLanczos3Filter()); resampleOp.setUnsharpenMask(AdvancedResizeOp.UnsharpenMask.Normal); BufferedImage destImage = resampleOp.filter(sourceImage, null); writeJpeg(destImage, destFile, JPEG_QUALITY); } </code></pre> <p>JPEG quality 90 generated with Scalr:</p> <p><img src="https://i.stack.imgur.com/pLpxS.jpg" alt="jpg thumb from scalr"></p> <p>JPEG quality 90 generated with java-image-scaling:</p> <p><img src="https://i.stack.imgur.com/WXVFZ.jpg" alt="jpg thumb from java-image-scaling"></p> <p>I didn't receive any further feedback, so my personal conclusion is that java-image-scaling provides superior quality, and so it's the library that I choose.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload