Skip to main content

Compress, resize, trim and convert images

Project description

🪗 Kompressor

Compress, convert and resize images.

Features

Rename — Modify the image name in multiple ways. Add a string to the source image, the compressed image, or both and put them in the current dir or a subdir.

Compress — Compress the images on a 1-100 scale, with a default of 80.

Resize — Set a max width and height for the image. The image will be resized to fit within the bounds while maintaining the aspect ratio.

Trim — Trim pixels from the sides of the image, by specifiying a number of pixels from each site. This is particularly usefull for removing bad lines from the edges of images.

Convert — Convert the image to a different format. Supported formats are png, jpeg and webp.

Strip metadata — Remove all metadata from the image using exiftool.

Requirements

Three command line tools are used for the actual compression, pngquant, jpegoptim and cwebp, and exiftool is used to remove metadata. Kompressor assumes they exist and are on the PATH.

They can be installed with the following commands.

Linux:
apt install pngquant jpegoptim webp exiftool

Macos:
brew install pngquant jpegoptim webp exiftool

Also, UV is required to build the tool. It can be installed with pipx or curl. See the UV install docs for more info.

Installation

Kompressor can be installed from pypi. Its recomended you use pipx to install python applications.

pip install kompressor
# or better yet,
pipx install kompressor

Installing from the git repo:

git clone https://github.com/8cylinder/kompressor.git
cd kompressor
uv sync
uv build
uv tool install ./dist/kompressor-XXXXX-py3-none-any.whl
# kompressor is now installed

Output dir

If the --output option is not used, the compressed image will be put in a dir called kompressor in the same dir as the source image. If the dir doesn't exist, it will be created. That means that if multiple images are specified in a dir tree, then there will be multiple kompressor dirs created.

When using the --output option, the compressed dir will use that name. If . is used, then the compressed images won't be put in a subdir but will be in the same dir as the source image. In which case you will need to use the --destination-rename or the --source-rename option to avoid an error.

Piping

Files can be piped into kompressor using the find command.

find . -type f -name "*.png" | kompressor
# or with options
find . -type f -name "*.png" | kompressor --quality 50 --output . --source-rename "-ORIGINAL"

Renaming

Files can optionally have a string added to the end of the filename using the --source-rename / -s and --destination-rename / -d options. The -d option renames the compressed image and the -s option renames the source image. These are applied whether it's put into a subdir or not.

Eg, with an argument of -ORIGINAL, this file image.jpg would become image-ORIGINAL.jpg.

Usage

By default, if the --output option is not used, the compressed file will be put in a subdir called "kompressor" in the same dir as the source file. It will be created if it doesn't exist.

Supported formats: png, jpeg, webp.

Basic usage — compress single or multiple images. This will create the kompressor dir and put the compressed image in it.

# single image
kompressor file.png

# multiple images
kompressor file1.png file2.png file3.png
# or
kompressor *.png

Renaming files — add a string to the end of the filename.

kompressor --destination-rename "-COMPRESSED" image.png
# new compressed image: ./kompressor/image-COMPRESSED.png

kompressor --source-rename "-ORIGINAL" image.png
# new compressed image: ./kompressor/image.png
# original image:       ./image-ORIGINAL.png

kompressor --source-rename "-ORIGINAL" --destination-rename "-COMPRESSED" image.png
# new compressed image: ./kompressor/image-COMPRESSED.png
# original image:       ./image-ORIGINAL.png

Setting the --output option to . will put the compressed image in the same dir as the source image. If you don't specify a rename flag, you will get an error about the file already existing.

kompressor --output . --source-rename "-ORIGINAL" image.png
# new compressed image: ./image.png
# original image:       ./image-ORIGINAL.png

kompressor --output . --source-rename "-ORIGINAL" --destination-rename "-COMPRESSED" image.png
# new compressed image: ./image-COMPRESSED.png
# original image:       ./image-ORIGINAL.png

To generate multiple compressed images with different quality settings, use a range. The following example generates 3 compressed images with different quality settings and puts them in the 'kompressor' directory.

for QUALITY in 10 50 80; do
  kompressor --quality=$QUALITY --destination-rename "-$QUALITY" *.png;
done

# ./kompressor/image-10.png
# ./kompressor/image-50.png
# ./kompressor/image-80.png

Trim — Cromp the image by removing a number of pixels from each side. The format for trimmng is t10,r10,b10,l10. This trims 10 pixes from the top, right, bottom and left sides. Use a comma to separate the values. Not all fields are required.

# trim 10 pixels from the top, right, bottom and left sides
kompressor --trim t10,r10,b10,l10 image.png
# trim 1 pixel from the top and 3 from the right side
kompressor --trim t1,r3 image.png

Resize — Scale the image to fit within the bounds of the width and height. The aspect ratio is maintained.

kompressor --resize 1000x1000 image.png

Convert — Convert the image to a different format. Supported formats are png, jpeg and webp.

# convert image.png to image.webp
kompressor --convert webp image.png
# new compressed image: ./kompressor/image.webp
# original image:       ./image.png

kompressor --convert png image.jpg -o .
# new compressed image: ./image.png
# original image:       ./image.jpg

Output

Output can be human readable or json using the --human (default) or --json flag.

kompressor image-*.png -d '-NEW' -x 1000 1000
image-01.png -> kompressor/image-01-NEW.png |  48K ->  25K | 52% | 1538x 985 -> 1000x 640
image-03.png -> kompressor/image-03-NEW.png | 6.3M -> 428K |  7% | 2080x1880 -> 1000x 904
image-04.png -> kompressor/image-04-NEW.png | 213K ->  38K | 18% | 1538x 985 -> 1000x 640
image-05.png -> kompressor/image-05-NEW.png | 6.5M -> 472K |  7% | 2080x1880 -> 1000x 904
image-06.png -> kompressor/image-06-NEW.png | 205K ->  35K | 17% | 1538x 985 -> 1000x 640
image-07.png -> kompressor/image-07-NEW.png | 6.1M -> 385K |  6% | 2080x1880 -> 1000x 904
image-08.png -> kompressor/image-08-NEW.png |  12K ->   4K | 32% |  392x 146 ->  392x 146
image-09.png -> kompressor/image-09-NEW.png |  17K ->   5K | 30% |  430x 146 ->  430x 146
image-10.png -> kompressor/image-10-NEW.png |  18K ->   5K | 30% |  420x 146 ->  420x 146
image-11.png -> kompressor/image-11-NEW.png |   3K ->   3K | 95% |  380x 146 ->  380x 146
image-12.png -> kompressor/image-12-NEW.png |   9K ->   3K | 31% |  276x 146 ->  276x 146
image-13.png -> kompressor/image-13-NEW.png |   4K ->   1K | 24% | 1000x1000 -> 1000x1000

Using the --json flag, output will look like this. Note I'm using jq to format the output.

kompressor image-{3,4}.png -x 100 100 --json | jq
[
  {
    "files": {
      "source": "/path/to/images/image-4.png",
      "compressed": "/path/to/images/kompressor/image-4.png"
    },
    "bytes": {
      "original_size": 217785,
      "compressed_size": 1561
    },
    "human": {
      "original_size": "213K",
      "compressed_size": "2K"
    },
    "original_dimensions": [
      1538,
      985
    ],
    "compressed_dimensions": [
      100,
      64
    ]
  },
  {
    "files": {
      "source": "/path/to/images/image-3.png",
      "compressed": "/path/to/images/kompressor/image-3.png"
    },
    "bytes": {
      "original_size": 6644905,
      "compressed_size": 7233
    },
    "human": {
      "original_size": "6.3M",
      "compressed_size": "7K"
    },
    "original_dimensions": [
      2080,
      1880
    ],
    "compressed_dimensions": [
      100,
      90
    ]
  }
]

Development

Run

uv run kompressor --help

Build

uv build

Install

pipx install ./dist/kompressor-XXXXX-py3-none-any.whl

uv tool install ./dist/kompressor-XXXXX-py3-none-any.whl

Install editable

uv tool install --editable .

Upload to pypi

twine upload --repository testpypi dist/* twine upload dist/*

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

kompressor-0.2.3.tar.gz (39.5 kB view details)

Uploaded Source

Built Distribution

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

kompressor-0.2.3-py3-none-any.whl (23.8 kB view details)

Uploaded Python 3

File details

Details for the file kompressor-0.2.3.tar.gz.

File metadata

  • Download URL: kompressor-0.2.3.tar.gz
  • Upload date:
  • Size: 39.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for kompressor-0.2.3.tar.gz
Algorithm Hash digest
SHA256 4e56788474d7adbc2a88d15682ab5b7c0cac20b111e656b458ea2205c021dbd2
MD5 522bff1d8354f88ff2abbd7a3f8053eb
BLAKE2b-256 bc46a25a19f62b8efa368b9f83be9926134ea6980f99b43a261605448e773359

See more details on using hashes here.

File details

Details for the file kompressor-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: kompressor-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 23.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for kompressor-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0a266e831c2d1ab7979cbd32650fdcb439b9be382178b458aab58193da9d735c
MD5 ce68ea6ea812fc8bffd256ddab5d64bb
BLAKE2b-256 a73e1e3f4002dd66e2a3dd96acda746569012b3064e6627dc45dab777bd0b9ad

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