Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

CoreImageTool - process images with Apple's CoreImage framework

In addition to command line tools such as man sips, ImageMagick or GIMP (batch mode) there's CoreImageTool to edit images from the command line using Apple's CoreImage framework (Mac OS X 10.4 and Mac OS X 10.5).

# download & install CoreImageTool

# create /usr/local/bin
/usr/bin/sudo /bin/mkdir -p /usr/local/bin
/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local /usr/local/bin
/usr/bin/sudo /bin/chmod 0755 /usr/local /usr/local/bin

cd ~/Desktop
/usr/bin/curl -L -O http://www2.entropy.ch/download/CoreImageTool
/usr/bin/sudo /bin/mv -i ~/Desktop/CoreImageTool /usr/local/bin

/usr/bin/sudo /usr/sbin/chown root:wheel /usr/local/bin/CoreImageTool
/usr/bin/sudo /bin/chmod 755 /usr/local/bin/CoreImageTool

ls -l /usr/local/bin/CoreImageTool


/bin/mkdir -p ~/Desktop/CoreImageDir
/bin/cp /Library/Desktop\ Pictures/Nature/Ladybug.jpg ~/Desktop/CoreImageDir

chmod 0777 ~/Desktop/CoreImageDir/Ladybug.jpg
ls -l ~/Desktop/CoreImageDir/Ladybug.jpg

cd ~/Desktop/CoreImageDir


# process Ladybug.jpg

# convert .jpg to .png
CoreImageTool load pic Ladybug.jpg store pic Ladybug.png public.png

CoreImageTool \
   load pic Ladybug.jpg \
   store pic Ladybug.png public.png

open -a Preview Ladybug.jpg
open -a Preview Ladybug.png

CoreImageTool load pic Ladybug.jpg filter pic CILanczosScaleTransform scale=0.5 store pic Ladybug2.jpg public.jpeg
open -a Preview Ladybug2.jpg

CoreImageTool load pic Ladybug.jpg filter pic CILanczosScaleTransform scale=0.1 store pic Ladybug3.jpg public.jpeg
open -a Preview Ladybug3.jpg

CoreImageTool load pic Ladybug.jpg filter pic CICrystallize radius=6.0 store pic Ladybug4.jpg public.jpeg
CoreImageTool load pic Ladybug.jpg filter pic CICrystallize radius=12.0 store pic Ladybug4.jpg public.jpeg
open -a Preview Ladybug4.jpg

CoreImageTool load pic Ladybug.jpg filter pic CICrop rectangle=250,250,800,800 store pic Ladybug5.jpg public.jpeg
open -a Preview Ladybug5.jpg

# sharpen image
CoreImageTool load pic Ladybug.jpg filter pic CISharpenLuminance sharpness=3 store pic Ladybug6.jpg public.jpeg
open -a Preview Ladybug6.jpg

CoreImageTool load pic Ladybug.jpg filter pic CIUnsharpMask radius=1.0:intensity=2.0 store pic Ladybug7.jpg public.jpeg
open -a Preview Ladybug7.jpg


Further information:

- CoreImageTool
- Apple's CoreImage power in the Terminal
- Apple Core Image Filter Reference
- Developing with Core Image

Automatically resize Terminal window

For more information see Terminal window commands, Discover tput and Apple Shell Scripting Primer -> ANSI Escape Sequence Tables.

tput lines
tput cols

echo $LINES
echo $COLUMNS

stty size
stty size | awk '{print $1}'    # lines
stty size | awk '{print $NF}'   # columns

stty size | cut -d" " -f1   # lines
stty size | cut -d" " -f2   # columns 

stty -a | awk '/rows/ {print $4}'      # lines
stty -a | awk '/columns/ {print $6}'   # columns

stty -a | sed -E -n -e 's/^.*[^[:digit:]]([[:digit:]]+)[[:space:]]+rows;.*$/\1/p;q;'
stty -a | sed -E -n -e 's/^.*[^[:digit:]]([[:digit:]]+)[[:space:]]+columns;.*$/\1/p;q;'



# automatically resize the Terminal window if it gets smaller than the default size

# positive integer test (including zero)
function positive_int() { return $(test "$@" -eq "$@" > /dev/null 2>&1 && test "$@" -ge 0 > /dev/null 2>&1); }


# resize the Terminal window
function sizetw() { 
   if [[ $# -eq 2 ]] && $(positive_int "$1") && $(positive_int "$2"); then 
      printf "\e[8;${1};${2};t"
      return 0
   fi
   return 1
}


# the default Terminal window size: 26 lines and 107 columns
sizetw 26 107


# automatically adjust Terminal window size
function defaultwindow() {

   DEFAULTLINES=26
   DEFAULTCOLUMNS=107

   if [[ $(/usr/bin/tput lines) -lt $DEFAULTLINES ]] && [[ $(/usr/bin/tput cols) -lt $DEFAULTCOLUMNS ]]; then
      sizetw $DEFAULTLINES $DEFAULTCOLUMNS
   elif [[ $(/usr/bin/tput lines) -lt $DEFAULTLINES ]]; then
      sizetw $DEFAULTLINES $(/usr/bin/tput cols)
   elif [[ $(/usr/bin/tput cols) -lt $DEFAULTCOLUMNS ]]; then
      sizetw $(/usr/bin/tput lines) $DEFAULTCOLUMNS
   fi

   return 0
}


# SIGWINCH is the window change signal
trap defaultwindow SIGWINCH    


sizetw 26 70
sizetw 10 107
sizetw 4 15

Resize image to fit container

Resizes an image to fit into its container, depending on whether the width or height is longer, yet keeping the aspect ratio. Copied from a comment on sitepoint.com.

ffunction resizeImgOO(el) 
{ 
	function imgRatio()
	{ 
		return (el.height / el.width); 
	} 
	function holderRatio()
	{ 
		return (el.offsetParent.offsetHeight / el.offsetParent.offsetWidth); 
	} 
	function fitToContainer()
	{ 
		if(imgRatio>holderRatio) 
		{ 
			el.height = el.offsetParent.offsetHeight; 
		} 
		else 
		{ 
			el.width = el.offsetParent.offsetWidth;	
		} 
	}
	
	this.imgRatio = imgRatio; 
	this.holderRatio = holderRatio; 
	this.resize = fitToContainer; 
}
var img = new resizeImgOO(document.getElementById('yourImgId'));
img.resize();

Image resize

Resize an image, keeping proportions to a new width or a new height. oh, and it caches.
You'll need to setup a writable directory $_SERVER[ 'DOCUMENT_ROOT' ].'/images/cache/ to use it.
<?php

if (isset($_GET[ 'image' ]))
{
    $image = realpath($_SERVER[ 'DOCUMENT_ROOT' ].urldecode($_GET[ 'image' ]));
    
    if (!file_exists($image))
    {
        die ( 'no such image!' );
    }
} else {
    die ( 'Missing argument!' );
}

function between($x, $y, $z)
{
    return $x >= $y && $x <= $z;
}

function cachefilename( $o, $w, $h)
{
    return $_SERVER[ 'DOCUMENT_ROOT' ].'/images/cache/'.base64_encode( $o.$w.$h ).'jpeg';
}

$imageinfo = getimagesize($image);
$w = $imageinfo[0];
$h = $imageinfo[1];
// the 64 and 1024 are minimum and maximum, change to please
if (isset ($_GET[ 'width' ]) && between($_GET[ 'width' ], 64, 128))
{
    $resizedwidth = $_GET[ 'width' ];
    $wf = $w / $resizedwidth;
    $resizedheight = $h / $wf;

} else {
    // same here
    if (isset ($_GET[ 'height' ]) && between($_GET[ 'height' ], 64, 1024))
    {
        $resizedheight = $_GET[ 'height' ];
        $hf = $h / $resizedheight;
        $resizedwidth = $w / $hf;

    } else {
        $resizedwidth = 600;
        $wf = $w / $resizedwidth;
        $resizedheight = $h / $wf;
    }
}

if (file_exists( cachefilename($image, $resizedwidth, $resizedheight) ))
{
    header ("Content-type: image/jpeg");
    readfile(cachefilename($image, $resizedwidth, $resizedheight) );
} else {
    $img = imagecreatefromstring(file_get_contents( $image ));
    $thumb = imagecreatetruecolor($resizedwidth, $resizedheight);
    imagecopyresampled($thumb, $img, 0, 0, 0, 0, $resizedwidth, $resizedheight, $w, $h);
    header ("Content-type: image/jpeg");
    imagejpeg($thumb, cachefilename($image, $resizedwidth, $resizedheight), 70);
    readfile(cachefilename($image, $resizedwidth, $resizedheight) );
    imagedestroy($img);
    imagedestroy($thumb);
}

?>