Search This Blog

Friday, March 26, 2021

Convert large TIFF file to JPG from to reduce space

I need convert large TIFF files, each size between 200MB to 2GB. Total size about 700TB.

  • First install the latest version imagemagick on the server.
  • Use convert command loop throught all the TIFF files, convert to JPG file at same folder.

    Set /etc/ImageMagick-7/policy.xml before run:
    <policymap>

      <!-- <policy domain="resource" name="temporary-path" value="/tmp"/> -->

      <policy domain="resource" name="memory" value="5GiB"/>

      <policy domain="resource" name="map" value="5GiB"/>

      <policy domain="resource" name="width" value="16KP"/>

      <policy domain="resource" name="height" value="16KP"/>

      <policy domain="resource" name="area" value="5GiB"/>

      <policy domain="resource" name="disk" value="5GiB"/>

     Verify the changes by command: convert -list resource

    Resource limits:
      Width: 16KP
      Height: 16KP
      Area: 128MP
      Memory: 1.9573GiB
      Map: 512MiB
      Disk: 1GiB
      File: 768
      Thread: 2
      Throttle: 0
      Time: unlimited
    
  • My find loop will break on whitespace in FileName.
  • Check and fix any TIFF filename has whitespace in it:
    find . -type f -name *tif | grep ' '
    
    Only few, quickly replace whitespace to '_'

     

  • Then run my loop with convert:
  • for file in $(find . -type f -name \*.tif); do # Not recommended, will break on whitespace
    	convert "$file"[0] -flatten -quiet -verbose "$(dirname "$file")"/"$(basename "$file" .tif).jpg"
    done
    
    '[0] -flatten' only read the top layer from TIFF files.
    '-quiet -verbose' remove all tags from TIFF files.
    'convert INPUT.jpg -sampling-factor 4:2:0 -strip [-resize WxH] [-quality N] [-interlace JPEG] [-colorspace Gray/sRGB] OUTPUT.jpg' using the Google Pagespeed Insights image optimization guidelines
    Output Example:
    ./2020-01-01/_DSC9999-Touch.tif[0]=>./2020-01-01/_DSC9999-Touch.jpg TIFF 5304x7952 5304x7952+0+0 16-bit sRGB 6.878MB 5.330u 0:06.530
  • # Delete TIF file
  • find . -type f -name "*.tif" -exec rm -rf {} \;
    
    Happy Earth Day!

    No comments:

    Post a Comment