Rublacava
Rublacava

Reputation: 519

Image format calculator and converter with GNU/Linux

Wouldn't it be great if there with a command for GNU/Linux that would do the following:

Open -Recursive *.png -Not-Case-Sensitive if exported-to-jpg@100%quality=less bytes than the original png then write jpg and delete the png

It would also be able to do the inverse of that command:

if png=less bytes than jpg then delete jpg

Upvotes: 0

Views: 177

Answers (3)

Mark Setchell
Mark Setchell

Reputation: 207670

Err, in answer to your question - "No, it probably wouldn't".

Firstly, PNG files can support transparency and JPEGs cannot, so if this was scripted to your specification, you could lose countless hours of work that went into creating transparent masks for thousands of images.

Secondly, PNG files do not support EXIF/IPTC data, so you would also lose all your Copyright, camera and lens settings, GPS data, dates, and oodles of other metadata.

Thirdly, your PNG file may contain 16 bits per channel, whereas JPEG can only store 8 bits per channel so you could potentially lose an awful lot of fine colour gradations by moving from PNG to JPEG.

Fourthly, you could potentially lose compatibility with older Web browsers which had spotty support for PNGs.

Upvotes: 0

fabincarmo
fabincarmo

Reputation: 81

Some time ago, I wrote a script to convert my photos. The script reduces the dimensions of all JPG file in current folder if any width or height is greater than MAX (default = 1024), keeping aspect ratio, and put them in a different folder (created). I hope this help you.

#!/bin/bash

if [ ! -d reduced ]
then
    mkdir reduced
fi

if [ $# -lt 1 ]
then
    MAX=1024
else
    MAX=$1
fi

for obj in *.jpg
do
    echo "------> File: $obj"
    tam=$(expr `identify -format "%b" "$obj" | tr -d "B"` / 1024)
    width=$(identify -format "%w" "$obj")
    height=$(identify -format "%h" "$obj")
    echo -e "\tDimensions: $width x $height px"
    echo -e "\tFile size: $tam kB"
    if [ $width -gt $height ] && [ $width -gt $MAX ]
    then
        convert "$obj" -resize $MAX "reduced/$obj.jpg"
        cd reduced
        mv "$obj".jpg "${obj%.jpg}".jpg;
        tam=$(expr `identify -format "%b" "$obj" | tr -d "B"` / 1024)
        width=$(identify -format "%w" "$obj")
        height=$(identify -format "%h" "$obj")
        echo -e "\tNew dimensions: $width x $height px"
        echo -e "\tNew file size: $tam kB"
        cd ..
        echo -e "\tOk!"
    elif [ $height -gt $MAX ]
    then
        convert "$obj" -resize x$MAX "reduced/$obj.jpg"
        cd reduced
        mv "$obj".jpg "${obj%.jpg}".jpg;
        tam=$(expr `identify -format "%b" "$obj" | tr -d "B"` / 1024)
        width=$(identify -format "%w" "$obj")
        height=$(identify -format "%h" "$obj")
        echo -e "\tNew dimensions: $width x $height px"
        echo -e "\tNew file size: $tam kB"
        cd ..
        echo -e "\tOk!"
    else
        cp "$obj" reduced/
        echo -e "\tDo not modify!"
    fi  
done

Upvotes: 0

AlwaysLearning
AlwaysLearning

Reputation: 796

Looking for the One True Command is not going to help: if it existed, it would only be useful for you and the (presumably) small set of people who had exactly your needs in the future.

The UNIX Way is to link together several commands to do what you want. For example:

"open-recursive": feed files into the hopper using "find", eg find /path -type f -name '*.png' -print and then send the list out through a pipe.

"not case-sensitive": either increase the scope of the find (-o) or get find to dump out all the files and then use grep to look for what you want, eg find . -print | grep -i '.png'

"if-exported-to-jpg": this is slightly tricky because I believe that the only way to check if the conversion saves bytes is to actually convert it and see. You can use the convert tool from the ImageMagick package to do this. ImageMagick has been standard in the big name distros for years so should be easy to find.

"if less bytes than": straightforward to do in the shell or your favorite scripting language - Perl, python etc.

The net is that you build up what you want from these smaller pieces and you should be able to do what you want now and have something that you can modify in the future or share with others for their unique needs. That is the UNIX Way. Ommmm :)

Upvotes: 1

Related Questions