Paper Camera

File

A faux-sketch style of image, popularised by smartphone apps such as "Paper Camera", are actually a very simple set of image processing steps. By adding dark edges based on an edge detection, posterising the image brightness and subtly desaturating the colours the hand-sketched image style can be made.

Paper_Camera.ijm

//Preset values
desatfac=0.7;
edgepreblur=1;
edgefac=0.8;
posterblur=2;
postersteps=5;
posterdepth=0.9;

//Quit if not RGB
if (bitDepth()!=24) {
    exit("Error: RGB image required");
}

//Generate user input dialog
Dialog.create("Paper Camera");
Dialog.addNumber("Desaturation:", desatfac, 1, 5, "(0.0-1.0)");
Dialog.addNumber("Edge smoothing:", edgepreblur, 0, 5, "(px)");
Dialog.addNumber("Edge strength:", edgefac, 1, 5, "(0.0-1.0)");
Dialog.addNumber("Overall smoothing:", posterblur, 0, 5, "(px)");
Dialog.addNumber("Posterization steps:", postersteps, 0, 5, "");
Dialog.addNumber("Posterization flattening:", posterdepth, 1, 5, "(0.0-1.0)");

//Show dialog and tidy variables
Dialog.show();
desatfac=maxOf(0, minOf(1, Dialog.getNumber()));
edgepreblur=maxOf(0, Dialog.getNumber());
edgefac=maxOf(0, minOf(1, Dialog.getNumber()));
posterblur=maxOf(0, Dialog.getNumber());
postersteps=maxOf(0, round(Dialog.getNumber()));
posterdepth=maxOf(0, minOf(1, Dialog.getNumber()));

//Convert to HSB
//setBatchMode(true);
run("HSB Stack");

//Duplicate brightness channel and generate image edges
setSlice(3);
run("Duplicate...", "title=tmp");
run("Gaussian Blur...", "sigma="+edgepreblur);
run("Find Edges");
run("Maximum...", "radius="+edgepreblur);
run("Enhance Contrast", "saturated=0.5");
run("Macro...", "code=v=v*"+edgefac);
run("Select All");
run("Copy");
close();

//Posterise brightness channel
setSlice(3);
run("Gaussian Blur...", "sigma="+posterblur);
run("Macro...", "code=v=floor(v/(256/"+postersteps+"))*256/"+postersteps+"+(v%(256/"+postersteps+"))*"+(1-posterdepth)+"+128/"+postersteps+" slice");

//Subtract edges from value image to generate black outline
setPasteMode("Subtract");
run("Paste");
setPasteMode("Copy");

//Desaturate
setSlice(2);
run("Macro...", "code=v=v*"+desatfac+" slice");

//Restore source
run("RGB Color");
run("Select None");

ImageJ Macros Gallery