Skip to main content

Compress JPEG, PNG and GIF files losslessly in batch and parallel mode

Project description

Smally

Smally is a command line tool which can help you compress JPEG, PNG and GIF files losslessly, by invoking the famous 3rd party tools such as jpegtran, optipng and gifsicle, in batch and parallel mode, inplace and keep mtime unchanged. It is written in Python, combined with SQLite, and heavily relied on shell.

Actually, the code structure and logic of smally might be a bit strange. It repeated invoke itself throught a shell command which is triggered by a subprocess initiated by itself as well. I just want to keep everything as simple and straight forward as possible. In some scenarios it indeed does a little bit more unnecessary work, but t's definitely also very funny! :)

How to Compress

JPEG

  1. Use jpegtran to remove all metadata, create a baseline version and a progressive version.
  2. Compare among the original file, baseline and progressive files, choose the smallest one in size.
  3. Whenever possible, choose progressive version.

PNG

Call optipng to compress PNG, in the most crazy -o7 -zm1-9 level. (Caveat: time-consuming, but better compression ratio)

GIF

Call gifsicle to compress GIF, by using -O3 --color 256.

How to Install

You need to install all 3rd party tools and smally itself as well.

# install tools on Fedora
$ sudo dnf install libjpeg-turbo-utils optipng gifsicle
# install tools on Ubuntu
$ sudo apt install libjpeg-turbo-progs optipng gifsicle
# install smally
$ pip install smally

How to Use

# command line help
$ python -m smally -h
# compress a single file, if option is not presented,
# smally will use file command to get file type info.
$ python -m smally [-j|-p|-g] <pathname>
# to compress a directory
$ python -m smally -r -P<N> <pathname>
# to compress all png and gif file in a directory
$ python -m smally -r -P<N> -p -g <pathname>

-r, recursive, it's a command line convention and normally you should use it when deal with a directory.

-P<N>, number of parallel processes, if missing, the logical cpu number would be used by defalut. If the specific number is larger than the number of logical cpu, smally choose the smaller one, since compression is cpu-intensive job, it makes no sense that the process number is bigger than the number of cpu.

# delete database record recursively
$ python -m smally -r -d <pathname>
# delete database record recursively only for jpeg picture
$ python -m smally -P4 -r -d -j <pathname>
# delete all temporary files generated by smally recursively
$ python -m smally -r -c <pathname>

-d, delete database record for the specific pathname when you want to redo the compress job, since the purpose of database records is to prevent smally from redoing the same job.

-c, remove all temporary files generated by smally, which includes database file (SQLite) and database lock file (I use a file lock to realize mutex for multiple processes). If you want to redo everything, or store all your pictures to a place, use it.

APIs

There are 4 APIs offered by smally:

# import
from smally import (jpegtran,
                    optipng,
                    gifsicle,
                    is_jpeg_progressive)

# signature
def jpegtran(pathname: str) -> tuple[int,int]: ...
def optipng(pathname: str) -> tuple[int,int]: ...
def gifsicle(pathname: str) -> tuple[int,int]: ...
def is_jpeg_progressive(pathname: str) -> bool: ...

The first int in returned tuple is the saved byte number. It could be zero, which means no save, or negetive, which mean how many bytes have been saved. The second int is the original file size.

Showcase

smally.gif

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

smally-0.54.tar.gz (2.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

smally-0.54-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file smally-0.54.tar.gz.

File metadata

  • Download URL: smally-0.54.tar.gz
  • Upload date:
  • Size: 2.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for smally-0.54.tar.gz
Algorithm Hash digest
SHA256 78e90ee6ed609eb70c50e48974f29426d1051825c020822d70cda758afad1aed
MD5 5fa2a2beac19378908d19569464e3b41
BLAKE2b-256 a5f5870f71bb709ef1cceb72f9f23b9e929a586cdb6edebb19b1bb61c8ae98ff

See more details on using hashes here.

File details

Details for the file smally-0.54-py3-none-any.whl.

File metadata

  • Download URL: smally-0.54-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for smally-0.54-py3-none-any.whl
Algorithm Hash digest
SHA256 465a9269259dca8ef97a157b02462eb8fbc471785c4f5d14958f2aee2c85243e
MD5 a23c72c46637633263e3fa9c5fd26a47
BLAKE2b-256 232f197519d26f797b4bd0c43a3c0849c0746a97adee305d4f3a9d099df10da2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page