# INSTRUCTIONS # Requires ImageMagick 7+ (to confirm your version, type magick -version into terminal, or convert -version for older versions). # Requires Exiftool (mine works with Exiftool 11.88. To confirm your version, type exiftool -ver into terminal. If metadata is not important to you, delete the exiftool commands and it will no longer be required). # Operating System: Tested on Linux Mint 20.3 (Ubuntu 20.04). Should work on most Linux OS. # Purpose: Downsize your image and output to jpg - merging both a blurry (corse detail) and a sharp (fine detail) output. # Ideal input is 16 bit tiff with an embedded linear colour space (eg. linear rec 2020, linear sRGB, etc...). To confirm the colour space embedded in your image, open terminal, run identify -verbose /path/'image name.tif' and find the category icc:description # In file browser, rename this file Magick-Resize-Composite-Width.sh, right click > properties > permissions, tick box 'is executionable', ok. # To edit: right click > open with text editor # To run: Open terminal and enter /path/Magick-Resize-Composite-Width.sh # FILES # INFILE: Replace $IN with /path/'your image name.tif' # OUTFILE: Replace $OUT with /path/'your image name.jpg' # ICCPROFILE: Replace $ICC with /path/'profile.icc' # outpx: Width in pixels of your desired output # OUTFILETIFF: Replace $T1 and $T2 with /path/'your image name.tif' (Ensure both have different file names, or they will overwrite the same file) # PNGMASK1: Replace $P with /path/'your image name.png' # Note: If your files/folders have spaces in their name, ensure they are surrounded by quotation marks. Eg. /'Folder Name'/ not /Folder Name/ # SCRIPT INFILE=$IN OUTFILE=$OUT ICCPROFILE=$ICC outpx=1600 OUTFILETIFF1=$T1 OUTFILETIFF2=$T2 PNGMASK=$P echo INFILE="${INFILE}" OUTFILE="${OUTFILE}" OUTFILETIFF1="${OUTFILETIFF1}" OUTFILETIFF2="${OUTFILETIFF2}" PNGMASK="${PNGMASK}" ICCPROFILE="${ICCPROFILE}" inpx=$(magick -quiet "${INFILE}" -format %w info:) echo inpx=${inpx} outpx=${outpx} # OUTFILE: # PT1: OUTPUT SHARPER magick -quiet "${INFILE}" \ -filter Catrom \ -define filter:blur=0.6 -define filter:widow=Spline -define filter:lobes=4 \ -distort resize ${outpx} \ -intent Perceptual -black-point-compensation -profile "${ICCPROFILE}" \ -depth 16 \ -quality 9 \ "${OUTFILETIFF1}" # if result is oversharp, change -define filter:blur= to a higher value. <1 sharpens, >1 blurs. # PT2: OUTPUT BLURRIER magick -quiet "${INFILE}" \ -filter Robidoux \ -distort resize ${outpx} \ -intent Perceptual -black-point-compensation -profile "${ICCPROFILE}" \ -depth 16 \ -quality 9 \ "${OUTFILETIFF2}" # PT3: COMPOSITE magick -quiet "${OUTFILETIFF1}" \ \( -clone 0 -colorspace gray -auto-level -edge 1 -morphology close disk:1 \) \ -compose CopyOpacity -composite \ "${OUTFILETIFF2}" \ -compose DstOver -composite \ -intent Perceptual -black-point-compensation -profile "${ICCPROFILE}" \ -quality 92 \ "${OUTFILE}" exiftool -overwrite_original -TagsFromFile "${INFILE}" "-all>exif:all" "${OUTFILE}" rm "${OUTFILETIFF1}" rm "${OUTFILETIFF2}" rm "${INFILE}" # VARIABLES # -quiet: Suppresses all warning messages. Helpful if your tif has tags that libTIFF (used by ImageMagick) doesn't recognise. Remove if unnecessary. # -filter: Different artefacts will present depending on the filter chosen, from blur to haloing, and blockiness to aliasing (moire). We try to find a sweet spot giving greatest perceived sharpness with least artefacts. # -define filter: For a comprehensive look at filters, please visit: https://www.imagemagick.org/Usage/filter/ # -intent: Determines how out of gamut colours are mapped back in when converting from a larger colour space to a smaller one. Perceptual will shift hue slightly to retain smooth gradients (best for detail). Perceptual will maintain hue at the risk of unsmooth gradients, aka posterisation (best for hue accuracy). # -black-point-compensation: Ensures your darkest shadows do not get crushed to black during colour space conversion. Remove if undesired. # -profile: converts your colour space to that chosen. sRGB is standard for web display. A large selection of profiles are available here: https://github.com/ellelstone/elles_icc_profiles/tree/master/profiles (From that list, sRGB-elle-V2-srgbtrc.icc is the standard for web display. Note that they are matrix profiles, thus do not support Perceptual intent) # -quality: When output is jpg, it accepts values from 1-100, where smaller values increase compression, thus reducing both file size and quality. When quality is >90 the chroma channels are not downsampled, making this optimal. When output is png or tiff, it accepts values from 1-9, where 9 is highest quality and file size. # -depth: Bit depth. Applies to png and tiff. When working in linear space 16 or 32 is necessary to avoid banding in the shadows. # exiftool -TagsFromFile copies metadata from INFILE, while "all>exif:all" pastes only exif metadata to OUTFILE, preventing your xmp metadata from being overwritten. If you wish to paste all metadata, replace it with "-all>all". -overwrite_original prevents a duplicate from being created. If you don't wish to copy/paste any metadata, put a # in front of the line. (Reason to include this line is that ImageMagick cannot read/write all tiff metadata, so we use Exiftool to recover that which ImageMagick doesn't handle) # rm will delete the file listed. If you do not wish for this to be deleted, put a # in front of the line. # The above are suggested variables. For a complete list, please visit: https://imagemagick.org/script/command-line-options.php and https://exiftool.org/ # PROCESS # OUTFILETIFF1 is catrom-spline, and is top layer [sharper, for fine detail] # OUTFILETIFF2 is robidoux, and is bottom layer [blurrier, for broad detail] # Duplicate OUTFILETIFF1, make it greyscale, auto levels, edge detection, convert to transparency mask which is applied to OUTFILETIFF1, so only its edges are displayed. # duplicate = -clone (when you use clone you need to put it all in brackets) # greyscale = can be -colorspace gray or lineargray (results are the same) # auto levels = -auto-level # edge detection = can be -edge (radius) or -canny (radius, or more complex variations of radius. # if you want to smooth edges of your mask (good for halos) and close small gaps, use -morphology close disk # if you want to thicken edges of your mask (better sharpness but more halos), use -morphology dilate disk. # if you want to thin edges of your mask, use -morphology erode # if you're happy with your edge mask as is, delete -morphology # +write "${PNGMASK}" can be placed after -morphology (or -edge if you don't use -morphology) to output a display of your edge mask in black/white. # apply to alpha channel = -channel Alpha (but I have found this unnecessary) # to use the clone as transparency mask, use -compose CopyOpacity and to join the two use -composite # to combine your two input images, use -compose DstOver and -composite