-2

Is it possible to compress jpg and/or png images more in a loss-less manner? I've found websites that do this, but I assumed that these files were all compressed the same way.

By compress, I mean it is still a jpg or png file that any image program can read, but the file size is smaller.

And if so, what is the best way in both Java and C# to get the additional compression?

David Thielen
  • 28,723
  • 34
  • 119
  • 193
  • 1
    I don't know what you can possibly mean by "best way" but https://stackoverflow.com/questions/14777800/gzip-compression-to-a-byte-array will help you to do compression (of any arbitrary data stream) in Java. – Ruzihm Oct 02 '18 at 22:21
  • @Ruzihm - gzip is general compression. I want to create a smaller valid jpg or png file. thanks - dave – David Thielen Oct 02 '18 at 22:23

2 Answers2

0

There are several compression algorithms out there some achieve better results than others in different images. I am not sure exactly what your question wants to know as you mix Java and C# with image compression.

Recently enough google developers have released a new format called WebP. They claim this format can deliver 26% smaller lossless images when compared with PNG's and lossy even better when compared to JPEG however the support for WebP is still quite limited.

What you can do is make use of the picture HTML element and deliver an alternative WebP with a fallback to a PNG or JPEF file.

here's an example:

<picture>
  <source srcset="img/awesomeWebPImage.webp" type="image/webp">
  <source srcset="img/creakyOldJPEG.jpg" type="image/jpeg"> 
  <img src="img/creakyOldJPEG.jpg" alt="Alt Text!">
</picture>

Here's a good article on using WebP: https://css-tricks.com/using-webp-images/

rodcunha
  • 387
  • 1
  • 16
  • Thank you for this but we have to provide JPG & PNG as the final destination of these images is DOCX & PDF files and so we're restricted to the formats they understand. – David Thielen Oct 03 '18 at 15:05
  • Welcome, yes the support for webp isn't great yet. Maybe an idea is to try grunt imagemin and do bulk conversions then do size comparisons with your current minification, this automation will accept both JPG and PNG. See more here: https://scriptverse.academy/tutorials/grunt-task-image-minification.html – rodcunha Oct 03 '18 at 16:17
0

All JPEG compression is inherently lossy. The RGB<->YCrCb conversion often requires modifying values because the gamuts of the color spaces are different. JPEG compression involves floating point values rounded to integers.

There are a lot of JPEG setting available to trade off loss against size. Whether you have access to them or not depends upon your encoder.

In the case of PNG, the only real size setting is how far the encoder will search the LZ window for matches. In PNG, it's a tradeoff between compression speed and size.

user3344003
  • 20,574
  • 3
  • 26
  • 62
  • Unless I'm not understanding something, there is lossless JPEG - https://en.wikipedia.org/wiki/Lossless_JPEG – David Thielen Oct 03 '18 at 15:04
  • For PNG, if we don't care about speed, any suggestions for a library that can give us the smallest size? We need this in both Java and .NET. thanks - dave – David Thielen Oct 03 '18 at 15:06
  • In the medical industry, 12 bpp lossless jpeg is used. Outside of that it is rarely supported. I don't know of any libraries offhand for PNG on Java and .NET. My guess is the PNG reference implementation does this. – user3344003 Oct 03 '18 at 17:46