Package 'sketcher'

Title: Pencil Sketch Effect
Description: An implementation of image processing effects that convert a photo into a line drawing image. For details, please refer to Tsuda, H. (2020). sketcher: An R package for converting a photo into a sketch style image. <doi:10.31234/osf.io/svmw5>.
Authors: Hiroyuki Tsuda [aut, cre]
Maintainer: Hiroyuki Tsuda <[email protected]>
License: MIT + file LICENSE
Version: 0.1.3
Built: 2025-03-06 03:50:26 UTC
Source: https://github.com/tsuda16k/sketcher

Help Index


A face image.

Description

A photograph obtained from a free stock photos site. pexels.com/photo/man-about-to-touch-his-face-wearing-blue-suit-718261/

Usage

face

Format

An array with 600 x 460 * 3 dimensions. Each dimension represents y-coordinate, x-coordinate, and color channel.

Examples

plot(face)

Load image from file or URL

Description

Load image from file or URL

Usage

im_load(file, name)

Arguments

file

path to file or URL

name

a string for name attribute. if missing, inferred from the file argument.

Value

an array of image data

Examples

## Not run: 
# load an image from disk
im = im_load("path/to/your/image.jpg")
plot(im)
# load an image from URL
im = im_load("http://placehold.jp/150x150.png")

## End(Not run)

Save an image to disk

Description

Save an image to disk

Usage

im_save(im, name, path, format = "png", quality = 0.95)

Arguments

im

An image.

name

Name of the image file.

path

Path to file.

format

Image format. Either "jpg", "png", "tiff", or "bmp". Default is "png".

quality

(jpg only) default is 0.95. Higher quality means less compression.

Value

No return value, called for side effects.

Examples

## Not run: 
im = sketch(face)

# im.png is saved to the current working directory
im_save( im, name = "im", path = getwd() )

# myimage.jpg is saved to a specified directory
im_save( im, name = "myimage", path = "path/to/image", format = "jpg" )

## End(Not run)

Display an image

Description

Display an image

Usage

## S3 method for class 'nimg'
plot(x, rescale = FALSE, ...)

Arguments

x

an image

rescale

logical. if true, then pixel value is rescaled to range between 0 and 1.

...

other parameters to be passed to plot.default

Value

No return value, called for side effects.

Examples

plot(face)

Apply the sketch effect on an image

Description

Apply the sketch effect on an image

Usage

sketch(
  im,
  style = 1,
  lineweight = 1,
  smooth = ceiling(lineweight),
  gain = 0.02,
  contrast = NULL,
  shadow = 0,
  max.size = 2048
)

Arguments

im

an image (array).

style

a numeric (integer). Either 1 or 2.

lineweight

a numeric. Strength of lines.

smooth

a numeric (integer). Smoothness of image texture.

gain

a numeric between 0 and 1. Can be used to reduce noise in dim regions.

contrast

a numeric (integer). Adjusts the image contrast.

shadow

a numeric between 0 and 1

max.size

maximum image resolution (width or height) of the output image

Value

an image.

Examples

im = sketch(face)
plot(im)

## Not run: 
im = im_load("path/to/your/image.jpg")
plot(im)

## End(Not run)

Create multiple sketches at once and combine them into a single image

Description

It is often necessary to find optimal sketch style parameters for your task. With this function, you can easily compare the effects of different style parameters.

Usage

survey(
  im,
  style = 1,
  weight_levels = c(1, 2, 4),
  smooth_levels = c(1, 2, 4),
  gain = 0.02,
  contrast = NULL,
  shadow = 0,
  verbose = TRUE
)

Arguments

im

an image.

style

numeric (integer). Either 1 (edge-focused) or 2 (smooth gradient)

weight_levels

numeric (integer). a vector of lineweight values

smooth_levels

numeric (integer). a vector of smooth values

gain

a numeric between 0 and 1. Can be used to reduce noise in dim regions.

contrast

numeric (integer). Adjusts the image contrast.

shadow

a numeric between 0 and 1

verbose

If TRUE (default), progress information is displayed in the Console.

Value

an array of the sketched image.

Examples

im = survey(face, style = 1, weight_levels = c(1, 3), smooth_levels = c(1, 3), shadow = 0.3)
plot(im)