Giter Site home page Giter Site logo

imageprocessing-electronicpublications / jpeg-recompress Goto Github PK

View Code? Open in Web Editor NEW
15.0 2.0 1.0 109 KB

Utilities for lossy recompress JPEGs

Home Page: https://github.com/danielgtaylor/jpeg-archive

License: MIT License

Makefile 1.39% C 96.25% Shell 2.35%
jpeg compress iga smallfry metrics lossy webp

jpeg-recompress's Introduction

GitHub release (latest by date) GitHub Release Date GitHub repo size GitHub all releases GitHub

JPEG Recompress

Utilities for archiving photos for saving to long term storage or serving over the web. The goals are:

  • Use a common, well supported format (JPEG)
  • Minimize storage space and cost
  • Identify duplicates / similar photos

Approach:

  • Command line utilities and scripts
  • Simple options and useful help
  • Good quality output via sane defaults

Contributions to this project are very welcome.

Download

You can download the latest source and binary releases from the JPEG Recompress releases page.

Utilities

The following utilities are part of this project. All of them accept a --help parameter to see the available options.

jpeg-recompress

Compress JPEGs by re-encoding to the smallest JPEG quality while keeping perceived visual quality the same and by making sure huffman tables are optimized. This is a lossy operation, but the images are visually identical and it usually saves 30-70% of the size for JPEGs coming from a digital camera, particularly DSLRs. By default all EXIF/IPTC/XMP and color profile metadata is copied over, but this can be disabled to save more space if desired.

There is no need for the input file to be a JPEG. In fact, you can use jpeg-recompress as a replacement for cjpeg by using PPM input and the --ppm option.

The better the quality of the input image is, the better the output will be.

Some basic photo-related editing options are available, such as removing fisheye lens distortion.

Demo

Below are two 100% crops of Nikon's D3x Sample Image 2. The left shows the original image from the camera, while the others show the output of jpeg-recompress with the medium quality setting and various comparison methods. By default SSIM is used, which lowers the file size by 88%. The recompression algorithm chooses a JPEG quality of 80. By comparison the veryhigh quality setting chooses a JPEG quality of 93 and saves 70% of the file size.

JPEG recompression comparison

Why are they different sizes? The default quality settings are set to average out to similar visual quality over large data sets. They may differ on individual photos (like above) because each metric considers different parts of the image to be more or less important for compression.

Image Comparison Metrics

The following metrics are available when using jpeg-recompress. SUM is the default.

Name Option Description
MPE -m mpe Mean pixel error (as used by imgmin)
PSNR -m psnr Peak signal-to-noise ratio
MSE -m mse Mean squared error
MSEF -m msef sqrt(MSE/Variance)
Correlation -m cor Correlation
SSIM -m ssim Structural similarity
MS-SSIM* -m ms-ssim Multi-scale structural similarity (slow!) (2008 paper)
VIFP1 -m vifp1 The visual information fidelity (VIF) 1 layer.
SmallFry -m smallfry Linear-weighted BBCQ-like (original project, 2011 BBCQ paper -> LibSmallFry)
SharpenBad -m shbad Sharpen discrepancies (LibSmallFry)
NHW -m nhw NHW convolutional metric (original project -> LibSmallFry)
1 pair -m ssimfry (ssim + smallfry) / 2
2 pair -m ssimshb (ssim + shbad) / 2
SUMMARY -m sum (ssim + vipf1 + smallfry + shbad + nhw) / 5 DEFAULT

Note: The SmallFry algorithm may be patented so use with caution.

"Universal Scale" of metrics (UM):

UM example

  0.0
  ... (DIRTY) ...
  0.5
  ... (LOW) ...
  0.75
  ... (MEDIUM) ...
  0.875
  ... (SUBHIGH) ...
  0.9375
  ... (HIGH) ...
  0.96875
  ... (VERYHIGH) ...
  1.0

Trends:

  UM = 0.29 * (sqrt(sqrt(255.0 / MPE)) - 1.0)
  UM = 0.557 * (sqrt(PNSR) - 5.0)
  UM = 0.5 * (sqrt(sqrt(1.0 / MSEF)) - 1.0)
  UM = 1.0 * cor_sigma(cor_sigma(COR))
  UM = 1.57 * cor_sigma(cor_sigma(cor_sigma(SSIM)))
  UM = 1.59 * cor_sigma(cor_sigma(MS_SSIM))
  UM = 1.10 * cor_sigma(cor_sigma(VIFP1))
  UM = 3.0 * (SMALLFRY * 0.01 - 0.8)
  UM = 1.46 * cor_sigma(SHARPENBAD)
  UM = 0.342 * (sqrt(sqrt(1.0 / NHW)) - 1.0)

    cor_sigma(M) = 1.0 - sqrt(1.0 - M * M)

Subsampling

The JPEG format allows for subsampling of the color channels to save space. For each 2x2 block of pixels per color channel (four pixels total) it can store four pixels (all of them), two pixels or a single pixel. By default, the JPEG encoder subsamples the non-luma channels to two pixels (often referred to as 4:2:0 subsampling). Most digital cameras do the same because of limitations in the human eye. This may lead to unintended behavior for specific use cases (see #12 for an example), so you can use --subsample disable to disable this subsampling.

Example Commands

# Default settings
jpeg-recompress image.jpg compressed.jpg

# High quality example settings
jpeg-recompress --quality high --min 60 image.jpg compressed.jpg

# Slow high quality settings (3-4x slower than above, slightly more accurate)
jpeg-recompress --accurate --quality high --min 60 image.jpg compressed.jpg

# Use SmallFry instead of SSIM
jpeg-recompress --method smallfry image.jpg compressed.jpg

# Use 4:4:4 sampling (disables subsampling).
jpeg-recompress --subsample disable image.jpg compressed.jpg

# Remove fisheye distortion (Tokina 10-17mm on APS-C @ 10mm)
jpeg-recompress --defish 2.6 --zoom 1.2 image.jpg defished.jpg

# Read from stdin and write to stdout with '-' as the filename
jpeg-recompress - - <image.jpg >compressed.jpg

# Convert RAW to JPEG via PPM from stdin
dcraw -w -q 3 -c IMG_1234.CR2 | jpeg-recompress --ppm - compressed.jpg

# Disable progressive mode (not recommended)
jpeg-recompress --no-progressive image.jpg compressed.jpg

# Disable all output except for errors
jpeg-recompress --quiet image.jpg compressed.jpg

jpeg-compare

Compare two JPEG photos to judge how similar they are. The fast comparison method returns an integer from 0 to 99, where 0 is identical. PSNR, SSIM, and MS-SSIM return floats but require images to be the same dimensions.

# Do a fast compare of two images
jpeg-compare image1.jpg image2.jpg

# Calculate PSNR
jpeg-compare --method psnr image1.jpg image2.jpg

# Calculate SSIM
jpeg-compare --method ssim image1.jpg image2.jpg

jpeg-hash

Create a hash of an image that can be used to compare it to other images quickly.

jpeg-hash image.jpg

jpeg-zfpoint

Compress JPEG files by re-encoding them to the lowest JPEG quality using the peculiarity jpeg (zero point) quantization feature.

webp-compress

Compress JPEGs by re-encoding to the smallest WEBP quality while keeping perceived visual quality the same.

This is a lossy operation, but the images are visually identical and it usually saves >50% of the size for JPEGs coming from a digital camera, particularly DSLRs.

All EXIF/IPTC/XMP and color profile metadata are not preserved!

Some basic photo-related editing options are available, such as removing fisheye lens distortion.

Building

Dependencies

Debian

Debian users can install via apt-get:

sudo apt-get install build-essential autoconf pkg-config nasm libtool libjpeg8-dev
git clone https://github.com/ImageProcessing-ElectronicPublications/libiqa.git
cd libiqa
make
sudo make install
cd ..
git clone https://github.com/ImageProcessing-ElectronicPublications/libsmallfry.git
cd libsmallfry
make
sudo make install
cd ..

Compiling (Linux and Mac OS X)

The Makefile should work as-is on Ubuntu and Mac OS X. Other platforms may need to set the location of libjpeg.a or make other tweaks.

make

Installation

Install the binaries into /usr/local/bin:

sudo make install

Links / Alternatives

License

All are released under an MIT license.

http://dgt.mit-license.org/

jpeg-recompress's People

Contributors

xiota avatar zvezdochiot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

yushansuger

jpeg-recompress's Issues

[bug] jpeg-recompress 2.5.5 ignores subsampling option

Hi,

just want to report, that at least version 2.5.5 ignores the subsampling option and produce always YUV420 jpeg's.

examples:

jpeg-recompress.exe --subsample disable input-yuv444.jpg output.jpg (outputs a YUV420 jpeg)
jpeg-zfpoint.exe    --subsample disable input-yuv444.jpg output.jpg (outputs a YUV420 jpeg)

I tried this with other options in combination (accurate, no-proggressive, methods),
but the results are always YUV420 jpeg's.

If i use a wrong option arg, like --subsample 123, jpeg-recompress outputs an error.
So the given option --subsample disable (or -S disable) was correctly detected.

Regards.

ERROR: missing APP0 marker, aborting! Using jpegli

I tried to use jpeg-recompress with mozjpeg and jpegli to compare the achievable file sizes.

#export LD_PRELOAD=/usr/lib/libjpeg.moz.so
export LD_PRELOAD=/usr/lib/libjpeg.jpegli.so
jpeg-recompress -l 10 --min 10 -t 0.8 -a -m ms-ssim test.jpg test2.jpg

When using jpegli in that way I get the error

New size is 82% of original (saved 145 kb)
ERROR: missing APP0 marker, aborting!

when the file is going to be written to disk.

I used libjpeg62-turbo-dev instead of libjpeg8-dev as the latter is not available for Debian anymore. Could this be an issue?
On the other hand, mozjpeg is working and was also compiled with libjpeg62-turbo-dev. The cjpeg tool is also working fine with jpegli.

Any ideas?

Poor performance

jpeg-recompress performs poorly. The reason appears to be that it uses libraries to do most of its processing. While the Makefile for the jpeg-recompress does use -O3 optimizations, the libraries do not. The following PRs address this issue:

Before (~31s):

$ time jpeg-recompress input.jpg output.jpg
Metadata size is 17kb
SUM at q=50 (1 - 99): UM 0.506895
SUM at q=75 (50 - 99): UM 0.648848
SUM at q=87 (75 - 99): UM 0.755086
SUM at q=81 (75 - 87): UM 0.695131
SUM at q=84 (81 - 87): UM 0.723682
SUM at q=86 (84 - 87): UM 0.752241
SUM at q=85 (84 - 86): UM 0.727213
Final optimized SUM at q=86: UM 0.752241
New size is 17% of original (saved 7360 kb)

cmd: jpeg-recompress input.jpg output.jpg
cpu: 99%
mem: 235
usr: 30.362
sys: 0.474
tot: 30.942

After (~18s):

$ time jpeg-recompress input.jpg output.jpg
Metadata size is 17kb
SUM at q=50 (1 - 99): UM 0.506900
SUM at q=75 (50 - 99): UM 0.648844
SUM at q=87 (75 - 99): UM 0.755127
SUM at q=81 (75 - 87): UM 0.695130
SUM at q=84 (81 - 87): UM 0.723682
SUM at q=86 (84 - 87): UM 0.752233
SUM at q=85 (84 - 86): UM 0.727271
Final optimized SUM at q=86: UM 0.752233
New size is 17% of original (saved 7360 kb)

cmd: jpeg-recompress input.jpg output.jpg
cpu: 98%
mem: 235
usr: 17.369
sys: 0.491
tot: 18.155

Since 2.6.2 there is a bug with -m ms-ssim

jpeg-recompress2.6.4.exe -m ms-ssim in.jpg out.jpg

MS-SSIM at q=26 (1 - 50): UM inf
MS-SSIM at q=14 (1 - 26): UM inf
MS-SSIM at q=8 (1 - 14): UM inf
MS-SSIM at q=5 (1 - 8): UM inf
MS-SSIM at q=3 (1 - 5): UM inf
MS-SSIM at q=2 (1 - 3): UM inf
Final optimized MS-SSIM at q=2: UM inf
New size is 1% of original (saved 34 kb)
The in.jpg file is 35 987o
The out.jpg file is 743o and buggy.
The files can be found here : https://file.io/65nXepUudssP Its nsfw images the password for the zip is nsfw

Its working with 2.6.1 version. The bug happen since 2.6.2 (linux or windows version)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.