Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

  1. #1
    Join Date
    Aug 2010
    Beans
    409
    Distro
    Ubuntu 10.04 Lucid Lynx

    How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    What is the best way to shrink JPG file size to a given number, e.g., ~100KB without significantly changing the pixel dimensions, color, or dpi resolution.

    On Windows, I had no problem (using Irfanview freeware with the right plugin) shrinking JPEG files to 'around' a given file size (e.g., 100KB) simply at the click of a checkbox. The results were 'around' the desired file size (usually plus or minus something like 10% to 20%), which was an attempt at the software to do the best it could using a variety of selectable filters. (Irfanview will even automatically 'ignore' files that are already below the file size set limit & with another checkbox, it will strip out the EXIF header & thumbnail information).

    However, on Ubuntu, I can't seem to figure out what's best to shrink JPG file size to around a given number.

    Of course I resize to the desired pixel dimensions first (e.g., using the RESIZE feature of Nautilus to resize to 640x480 pixels or "convert * -strip -auto-orient -resize 640x480 -quality 75 *.jpg); but, once the pixel dimensions are set, I no longer want to change dimensions to get the file size down ... but sometimes I still need to reduce the file size closer to a given desired file size for a large directory of photos.

    Certainly ImageMagick has a "resize" option, but that option resizes the pixel dimension, which is NOT what is needed here:

    Code:
    convert -resize 50% -quality 80 input.jpg output.jpg
    Of course, with trial and error on the "-quality" setting, one can 'approximate' what is needed:
    Code:
    mogrify -format jpg -quality 75 -resize "640x480>" -strip *
    But, without trial and error approaches for potentially hundreds of photos at a time, what's the best way to shrink a directory of photos to (around) a given file size in KB (plus or minus 10% or 20%) simply by specifying the desired target file size?
    Last edited by rocksockdoc; April 3rd, 2011 at 07:00 PM.

  2. #2
    Join Date
    Aug 2010
    Beans
    409
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Update: I gave up on trying to find any Ubuntu utility to resize digital pictures to a given file size (in kilobytes).

    For now, I'll just use the Ubuntu Software Center > nautilus extension to mass resize or rotate images" instead.

    Attached Images Attached Images

  3. #3
    Join Date
    Aug 2010
    Beans
    409
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Ever since I posted this (as yet unanswered question), I've been manually converting files to smaller sizes using the Nautilus GUI extension to resize photos:
    Code:
    sudo apt-get install nautilus-image-converter

    When batch operation is needed, I use ImageMagick for batch resizing operations:

    Code:
    sudo apt-get install imagemagick
    convert -resize 640x480 -quality 90 input.jpg output.jpg
    or
    Code:
    mogrify -resize 640×480! *.jpg
    Note: This will resize all to 640*480 pixels.
    The bang (!) tells it to force the aspect ratio.

    or
    Code:
    mogrify *.jpg -quality 75 -resize "640x480>" *
    Note: The ">" at the end makes images smaller but 'not' larger.
    However ...

    This still doesn't set the final file size (in kilobytes).
    Last edited by rocksockdoc; September 5th, 2011 at 07:17 AM.

  4. #4
    Join Date
    Jul 2010
    Location
    Bangalore, India
    Beans
    147
    Distro
    Ubuntu Development Release

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Irfan View has nice image conversion features with lot of customization too. Try installing it using wine and use batch resize option in Irfan View.
    A stopped clock shows right time twice a day

  5. #5
    Join Date
    Feb 2009
    Location
    Central MD
    Beans
    658

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    I don't believe nautilus-image-converter works in the most recent versions of nautilus/ubuntu.

  6. #6
    Join Date
    Aug 2010
    Beans
    409
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Quote Originally Posted by aura7 View Post
    Irfan View has nice image conversion features
    I agree that Irfanview, on Windows, is the golden standard.

    Not only can it batch shrink to a pre-determined file size (e.g., 100 KBytes), but, it does so in an intelligent manner that allows the file to be larger or smaller if it can't get there.

    There appears to be no similar equivalent functionality in Ubuntu, unfortunately.

  7. #7
    Join Date
    May 2008
    Location
    Sverige
    Beans
    1,133

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Quote Originally Posted by rocksockdoc View Post
    When batch operation is needed, I use ImageMagick for batch resizing operations:

    Code:
    sudo apt-get install imagemagick
    convert -resize 640x480 -quality 90 input.jpg output.jpg
    or
    Code:
    mogrify -resize 640×480! *.jpg
    Note: This will resize all to 640*480 pixels.
    The bang (!) tells it to force the aspect ratio.

    or
    Code:
    mogrify *.jpg -quality 75 -resize "640x480>" *
    Note: The ">" at the end makes images smaller but 'not' larger.
    However ...

    This still doesn't set the final file size (in kilobytes).
    Hello rocksockdoc,

    1. I don't understand why you want to set the final size in kilobytes. I am satisfied to set the quality and size in pixels (imagemagick options -quality and -resize), because that way the visual quality of the picture is controlled (allowing more kilobytes to a complicated picture). The average size of many pictures can be predicted well enough (to fit in a limited space).

    2. Irfanview works well for me with Wine in Ubuntu. I use it very seldom nowadays, but it is certainly a good option.

    3. By the way, I use jhead for lossless rotation of pictures.
    Code:
    jhead -autorot
    /olle

  8. #8
    Join Date
    Aug 2010
    Beans
    409
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Quote Originally Posted by Olle Wiklund View Post
    I don't understand why you want to set the final size in kilobytes.
    It's a good question.

    I'm 'spoiled' by the wonderful features of the Irfanview freeware ... so much so that I haven't really figured out if these so nice and so easy to use features are really necessary.

    For example, to set the file size to, say, 100KB (using a RIOT plugin, whatever that is), is as simple as checking a checkbox.

    So, in practice, since I email ten at a time and I wish to limit my email size to less than a GB, I usually set the following three items in batch Irfanview operations as my default ... and I leave it at that:

    • Shrink pixel size to 640x480 pixels
    • Shrink JPEG quality to something around 85% or so (& auto-apply sharpening)
    • Shrink file size to something around 100 to 200KB or so

    Now, your question of whether there is value in that third setting, is one I can't really answer. In effect, the first two get the file size down from 5 GB to something on the order of a hundred to two hundred KB anyway ... but I use the third for good measure.

    It seems to work - but - I can't answer the question of whether it's actually necessary.

    Maybe someone who knows the RIOT plugin can tell us more about the value of shrinking down to a specified file size (give or take)?


    Quote Originally Posted by Olle Wiklund View Post
    The average size of many pictures can be predicted well enough (to fit in a limited space).
    In practice, this is true (for the most part).

    Quote Originally Posted by Olle Wiklund View Post
    Irfanview works well for me with Wine
    As a general rule, I first try to REPLACE Windoze features with Ubuntu ... w/o resorting to Wine.

    IMHO, the day Wine becomes no longer necessary to easily do the things we need to do, is the first day that Ubuntu (and any other Linux) will finally become 'user friendly' functional.

    Quote Originally Posted by Olle Wiklund View Post
    I use jhead for lossless rotation of pictures
    Thanks. There are ten things I do (doesn't everyone?) with pictures that I am desperately trying to get Ubuntu to do easily ...

    Here, for the record, are my attempts to reproduce, on Ubuntu, what Irfanview seems to do with aplomb on Windoze:
    * Stringing commands together to manage typical digital photo operations

    Quote Originally Posted by rocksockdoc View Post
    Can those more knowledgeable than I help me improve the to-be-written script described below?

    My ten typical digital photo operations, in preparation for mailing to friends & family, which I presume are somewhat ubiquitous, are the following:

    1. I create an empty directory on the hard disk drive titled by date
      • e.g., mkdir /data/pic/20110904
      • Note: I should make this automatically figure out the date from the 'date' command.

    2. I copy the flash card JPG contents to the hard disk drive
      • e.g., mv /media/NIKON D5000/DCIM/100D5000 /data/pic/20110904/big
      • Note: I'm not sure if the Nikon flash card folder will always be the same name or not.

    3. I rotate the large JPG files according to EXIF orientation tags
      • e.g., exiftran -ai *.JPG
      • or, jhead -autorot *.JPG

    4. I rename the large JPG files based on EXIF date & time data
      • e.g., for i in $(ls *.JPG); do exiv2 -r '%Y%m%d-%H%M%S-:basename:' rename $i; done
      • Note: This creates file names of the format: 20110904-141904-DSC_1001.JPG
      • I should probably figure out how to get rid of the "DSC_" part as it adds no value.

    5. I create a directory to hold the shrunken JPG files (suitable for mailing out)
      • e.g., mkdir /data/pic/20110904/small

    6. I shrink the JPG files to 640x480 pixels at any desired % quality (and/or to about 100KB in size)
      • for f in *.JPG; do convert -resize 640x480 -quality 90 $f ../small/shrunk_$f; done
      • This creates file names of the format: shrunk_20110904-141904-DSC_1001.JPG
      • Note: I can't figure out how to ensure they will be smaller than a certain file size.

    7. I add a white caption plate of about 100 pixels wide for later captioning
      • e.g., for f in *.JPG; do convert $f -bordercolor white -gravity south -splice 0x100 $f; done
      • Note: This bottom captioning plate must be added only after auto-rotation.

    8. I create a common set of sub folders for mailing purposes
      • e.g., mkdir 20110904/small/{family,friends}

    9. I step through the small picture set selecting the best photos to copy to the to-be-emailed subfolders
      • e.g., feh *.JPG -F --cycle-once --action1 "cp %f ./family/%n" --action2 "cp %f ./friends/%n" --action3 "cp %f ./family/%n; cp %f ./friends/%n"
      • As I view each picture in turn:
        • Space bar advances to the next photo
        • Backspace repeats the previous photo
        • 1 copies the file to the 'family' subfolder
        • 2 copies the file to the 'friends' subfolder
        • 3 copies the file to both the friends and family subfolders
        • Control + delete will delete the current file


    10. I caption the pictures, ad hoc, for that personal touch in the results.
      • e.g., for f in *.JPG; do kolourpaint $f; done
      • Note: I caption and annotate each picture, as needed, & then save the new file & close kolourpaint (whereupon the next file pops up in kolourpaint, to repeat the process).


    I'm not a programmer so, before I string these ten commands into a single script, may I ask for advice from those of you more experienced than I in these scripting matters?
    So far, nobody has provided any advice on the above question ... but I hope someone does as the results will be published back here so that everyone can do these most-common ten steps just as easily on Ubuntu as we can already do on Windoze.
    Attached Images Attached Images

  9. #9
    Join Date
    May 2008
    Location
    Sverige
    Beans
    1,133

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    Thank you rocksockdoc,

    for your long and informative answer. I use digikam to manage and tag my pictures, gimp to edit them, imagemagick and jhead for batch processing. But I have not at all your high ambitions to manage my pictures. I hope that you find some friends here in the forum to exchange methods with you in order to migrate completely to linux.

    Best regards
    Olle

  10. #10
    Join Date
    May 2008
    Location
    Sverige
    Beans
    1,133

    Re: How best to batch shrink JPEG file size (not pixels) to a given number of KBs?

    I should probably figure out how to get rid of the "DSC_" part as it adds no value.
    In bash you can use ${i/string/} as in the following example
    Code:
    olle@April-2008:~$ typeset greeting='Hello DSC_World'
    olle@April-2008:~$ echo $greeting 
    Hello DSC_World
    olle@April-2008:~$ echo ${greeting/DSC_/} 
    Hello World
    olle@April-2008:~$

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •