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

Ruby: Conform Sequence Files to Final Cut Pro XML (BETA)


#! /usr/bin/env ruby

# Tested with Apple XML Interchange Format Vr. 4
# The program infers the location of the seq. files by using the offline MOV's name.
# 1. It first looks for subfolders in the location of the MOV file. If a subfolder is found to have the same name as
# the file itself, then the whole folder is treated as a mirror of itself.
# 2. If no matching folders are found, it will use the first non-matching folder it comes across, and then assume
# that the files contained within it are prefixed with a name similar to its own.
# 3. Offline MOVs must run at the same framerate as your editing timebase in FCP! No putting a 24fps MOV into a 30fps timeline!

require 'rubygems'
require 'hpricot'

class Range

def length
  to_a.size
end

end

class Fixnum

def pad(n=8)
  x = to_s
  pad = n - x.length
  x.insert(0,"#{'0'*pad}")
  x
end

end

class Editor

attr_reader :edl

def initialize(file)

  @masters = {}
  @edits = []
  @edl = []
  @doc = Hpricot(open(file))

  find_masters()
  breakdown()

end

def find_masters

@doc.search("//file/pathurl").each do |elem| 
  
  base = File.basename(elem.inner_html).gsub!(/\....$/,'')
  path = abspath(elem.inner_html)

  folders = `ls #{path}`.split("\n").delete_if { |f| f =~ /\..../ }

  if folders.index(base).nil?
    @masters[elem.parent.attributes['id']] = path + "/" + folders.first + "/" + base + "."
  else
    @masters[elem.parent.attributes['id']] = path + "/" + base + "/"
  end

end

end

def breakdown

  @edits = @doc.search("//clipitem/file").each do |elem| 

    cut = []
    file_id = elem.attributes['id']
    seq_location = @masters[file_id]
    places = getpad(seq_location)

    cutin = (elem.parent/"in").inner_html.to_i.pad(places)
    cutout = (elem.parent/"out").inner_html.to_i.pad(places)

    cut.push(Range.new(cutin.succ!,cutout)) # Compensate for off-by-one count in IN-point from XML
    cut.push(seq_location)

    @edl.push(cut)

  end

end

def getpad(path)

  if path =~ /\.$/
    f = `ls #{path}*`.split("\n")[1]
  else
    f = `ls #{path}`.split("\n")[1]
  end
  
  return f.match(/(.+\.)?(.+)\..../).to_a.last.length

end

def abspath(path)

  path.gsub!(/file:\/\/localhost/,'')
  path.gsub!(/%20/,'\ ')

  return File.dirname(path)

end

def print_edl
  @edl.each_with_index do |cut,i|
    puts i.pad(4).succ + "|" + cut[0].first + " -- " + cut[0].last + " ++ " + cut[1]
  end
end

end

class Manager

def initialize

  @subroll = '00'
  @maxslots = 0
  @current_directory = ""
  @current_frame = "00000001"

  refresh_current

end

def refresh_current
  @subroll.succ!
  @maxslots = FRAMES_PER_ROLL
  @current_directory = "#{TARGET}/#{BIGROLL}_#{@subroll}"
  `mkdir -p #{@current_directory}`
  puts "NEW ROLL! #{@current_directory}"
  sleep 5
end

def assemble(cut)

  seq, source = cut[0], cut[1]

  if seq.length+1 > @maxslots
    refresh_current()
  end

  puts "Copying files to temporary folder..."

  seq.each do |frame|
    if source =~ /\/$/
      `cp #{source}/*.#{frame}.dpx #{TEMP}/`
    else
      `cp #{source}#{frame}.dpx #{TEMP}/`
    end
  end

  Dir.chdir(TEMP)
  
  list = Dir.entries(TEMP).delete_if { |x| x =~ /^\./ }
  
  list.map! { |f| File.rename(f,f + "x") ; f + "x" }

  puts "Moving files into roll #{@subroll}"

  list.each do |f|

    `mv -i #{f} #{@current_directory}/`

    Dir.chdir(@current_directory)
    
    `mv -i #{f} #{BIGROLL}_#{@subroll}.#{@current_frame}.dpx`

    puts "Moved #{f}"

    Dir.chdir(TEMP)
    
    @current_frame.succ!
    @maxslots -= 1
    
  end

end

end

## SETUP ##

FRAMES_PER_ROLL = 80
BIGROLL = '03'
TEMP = "/Volumes/Offline/Cache/Rubycut"
TARGET = "/Volumes/Offline/REEL3RC2"
FCP_XML = "/Users/anak/Desktop/edl.xml"

## MAIN ##

editor = Editor.new(FCP_XML)

editor.print_edl

puts "Press ENTER to continue..."

gets

manager = Manager.new

editor.edl.each_with_index do |cut,i|
  
  puts "Assembling #{(i+1).pad(4)} | #{cut[0]} > #{cut[1]}"
  
  manager.assemble(cut)
  
end

puts "CONFORMED: #{FCP_XML}"