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 |
A photograph obtained from a free stock photos site. pexels.com/photo/man-about-to-touch-his-face-wearing-blue-suit-718261/
face
face
An array with 600 x 460 * 3 dimensions. Each dimension represents y-coordinate, x-coordinate, and color channel.
plot(face)
plot(face)
Load image from file or URL
im_load(file, name)
im_load(file, name)
file |
path to file or URL |
name |
a string for name attribute. if missing, inferred from the file argument. |
an array of image data
## 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)
## 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
im_save(im, name, path, format = "png", quality = 0.95)
im_save(im, name, path, format = "png", quality = 0.95)
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. |
No return value, called for side effects.
## 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)
## 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
## S3 method for class 'nimg' plot(x, rescale = FALSE, ...)
## S3 method for class 'nimg' plot(x, rescale = FALSE, ...)
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 |
No return value, called for side effects.
plot(face)
plot(face)
Apply the sketch effect on an image
sketch( im, style = 1, lineweight = 1, smooth = ceiling(lineweight), gain = 0.02, contrast = NULL, shadow = 0, max.size = 2048 )
sketch( im, style = 1, lineweight = 1, smooth = ceiling(lineweight), gain = 0.02, contrast = NULL, shadow = 0, max.size = 2048 )
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 |
an image.
im = sketch(face) plot(im) ## Not run: im = im_load("path/to/your/image.jpg") plot(im) ## End(Not run)
im = sketch(face) plot(im) ## Not run: im = im_load("path/to/your/image.jpg") plot(im) ## End(Not run)
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.
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 )
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 )
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. |
an array of the sketched image.
im = survey(face, style = 1, weight_levels = c(1, 3), smooth_levels = c(1, 3), shadow = 0.3) plot(im)
im = survey(face, style = 1, weight_levels = c(1, 3), smooth_levels = c(1, 3), shadow = 0.3) plot(im)