| Title: | Take Screenshots (Screen Capture) from R Command |
|---|---|
| Description: | Take screenshots from R command and locate an image position. |
| Authors: | Toshikazu Matsumura [aut, cre] (ORCID: <https://orcid.org/0000-0003-2306-6355>) |
| Maintainer: | Toshikazu Matsumura <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.9.3 |
| Built: | 2026-09-21 10:05:05 UTC |
| Source: | https://github.com/matutosi/screenshot |
Converts a bitmap image to PNG using
bitmap2png(path)bitmap2png(path)
path |
Path to the bitmap image. |
The result of the system call.
## Not run: bitmap2png("path/to/image.bmp") ## End(Not run)## Not run: bitmap2png("path/to/image.bmp") ## End(Not run)
Sample data of clipboard bitmap array
clipboard_sampleclipboard_sample
raw data of bitmap array with 246292 length
data(clipboard_sample)data(clipboard_sample)
This function works only on windows. This function saves the image currently in the clipboard to a temporary BMP file.
clipboard2bitmap()clipboard2bitmap()
Path to the temporary BMP file.
## Not run: clipboard2bitmap() ## End(Not run)## Not run: clipboard2bitmap() ## End(Not run)
locate_ndl_in_hay().Compare values within tow arrays or matrices.
Helper function for locate_ndl_in_hay().
compare_table(ndl_mt, hay_mt)compare_table(ndl_mt, hay_mt)
ndl_mt, hay_mt
|
A matrix. |
A tibble.
val <- seq(from = 0, to = 1, by = 0.1) mt_1 <- matrix(sample(val, 20, replace = TRUE)) mt_2 <- matrix(sample(val, 100, replace = TRUE)) compare_table(mt_1, mt_2)val <- seq(from = 0, to = 1, by = 0.1) mt_1 <- matrix(sample(val, 20, replace = TRUE)) mt_2 <- matrix(sample(val, 100, replace = TRUE)) compare_table(mt_1, mt_2)
compare_table().Helper function for compare_table().
count_val_freq(mt, colname)count_val_freq(mt, colname)
mt |
A numeric matrix or array. |
colname |
A string of name for count. |
A dataframe.
mt <- sample(1:10, 30, replace = TRUE) count_val_freq(mt, "freq")mt <- sample(1:10, 30, replace = TRUE) count_val_freq(mt, "freq")
Create a BMP header
create_header(clipboard)create_header(clipboard)
clipboard |
A raw vector of the clipboard contents. |
A raw vector of the BMP header.
data(clipboard_sample) create_header(clipboard_sample)data(clipboard_sample) create_header(clipboard_sample)
Cut off a part of image from a whole image.
crop_image(image, pos_x, pos_y, w = 50, h = 20) hay2needle(image, pos_x, pos_y, w = 50, h = 20)crop_image(image, pos_x, pos_y, w = 50, h = 20) hay2needle(image, pos_x, pos_y, w = 50, h = 20)
image |
An image of cimg. |
pos_x, pos_y
|
A numeric to indicate the top left corner of cutting image. When NULL, position will be randomly sampled. |
w, h
|
A numeric for width or height of the cutting image. |
An image of cimg object.
image <- imager::load.example("parrots") croped_image <- crop_image(image, 200, 250, 100, 50) layout(c(1:2)) plot(image) plot(croped_image)image <- imager::load.example("parrots") croped_image <- crop_image(image, 200, 250, 100, 50) layout(c(1:2)) plot(image) plot(croped_image)
This function returns the coordinates of the specified corner of the display. This function works only on windows.
display_corner(size, corner = "bottom_left", width = 600, height = 600)display_corner(size, corner = "bottom_left", width = 600, height = 600)
size |
Integers to specify width or height of display size. |
corner |
A string to specify a corner of the display. "top_left", "top_right", "bottom_left", or "bottom_right". |
width, height
|
A integer to specify width or height of the corner. |
A numeric vector of length 4 representing the coordinates of the specified corner.
## Not run: size <- display_size() display_corner(size, "top_left", 800, 800) ## End(Not run)## Not run: size <- display_size() display_corner(size, "top_left", 800, 800) ## End(Not run)
This function works only on windows.
display_size()display_size()
A list with two elements, width and height, which are the width
and height of the display.
## Not run: display_size() ## End(Not run)## Not run: display_size() ## End(Not run)
This function works only on windows.
get_clipboard_image()get_clipboard_image()
A raw vector containing the image data.
## Not run: get_clipboard_image() ## End(Not run) data(clipboard_sample) head(clipboard_sample, 100) header <- create_header(clipboard_sample) image_data <- c(header, clipboard_sample) path <- fs::path_temp(ext = "bmp") save_bmp(image_data, path) # shell.exec(path) fs::file_delete(path)## Not run: get_clipboard_image() ## End(Not run) data(clipboard_sample) head(clipboard_sample, 100) header <- create_header(clipboard_sample) image_data <- c(header, clipboard_sample) path <- fs::path_temp(ext = "bmp") save_bmp(image_data, path) # shell.exec(path) fs::file_delete(path)
Get OS name
get_os()get_os()
A string of OS name
get_os()get_os()
Convert hexadecimal string to little-endian
hex2little_endian(x)hex2little_endian(x)
x |
Hexadecimal string |
Little-endian hexadecimal string
hex2little_endian("01234567") hex2little_endian("012345")hex2little_endian("01234567") hex2little_endian("012345")
locate_image().
Use grayscale to Speed up and to simplify code.Convert cimg class into grayscale xy matrix.
Helper function for locate_image().
Use grayscale to Speed up and to simplify code.
image2gray_matrix(img)image2gray_matrix(img)
img |
A cimg object. |
An xy dimensional matrix.
locate_ndl_in_hay().Convert array index into xy location in matrix.
Helper function for locate_ndl_in_hay().
index2xy(index, nrow)index2xy(index, nrow)
index, nrow
|
A numeric. |
A numeric pair of xy location.
nrow <- 4 matrix(1:12, nrow = nrow) purrr::map(1:12, index2xy, nrow = nrow)nrow <- 4 matrix(1:12, nrow = nrow) purrr::map(1:12, index2xy, nrow = nrow)
Codes are from URL shown below.
https://superuser.com/questions/75614/take-a-screen-shot-from-command-line-in-windows#answer-1751844
On Mac screencapture is usually available.
On Linux GNOME desktop use gnome-screenshot.
If not installed, run sudo apt install gnome-screenshot.
install_screenshot(bin_dir = "")install_screenshot(bin_dir = "")
bin_dir |
A string of directory to be installed. |
A string of installed directory.
if(interactive()){ # need only on Win if(get_os() == "win"){ bin_dir <- fs::path_package("screenshot") # if you want to install another directory # bin_dir <- "SET_YOUR DIRECTORY" install_screenshot(bin_dir) } }if(interactive()){ # need only on Win if(get_os() == "win"){ bin_dir <- fs::path_package("screenshot") # if you want to install another directory # bin_dir <- "SET_YOUR DIRECTORY" install_screenshot(bin_dir) } }
locate_ndl_in_hay().Helper function for locate_ndl_in_hay().
is_all_same(ndl_mt, hay_mt, base_xy, tol_val = 0)is_all_same(ndl_mt, hay_mt, base_xy, tol_val = 0)
ndl_mt, hay_mt
|
A matrix |
base_xy |
A numeric pair of xy location. |
tol_val |
A numeric of the tolerance of the comparison. |
A logical.
Locate needle image position on a screenshot image.
locate_image( needle_image, center = TRUE, exact = TRUE, timeout = 5, corner = NULL, width = 600, height = 300, size = NULL, scale = NULL, bin_dir = "", tol = 0 )locate_image( needle_image, center = TRUE, exact = TRUE, timeout = 5, corner = NULL, width = 600, height = 300, size = NULL, scale = NULL, bin_dir = "", tol = 0 )
needle_image |
A string of image file path or a cimg class object of imager library. |
center |
A logical. TRUE returns center position of needle_image. |
exact |
A logical. Check matching exactly or not. FALSE compares sampled pixels only. |
timeout |
A numeric for timeout seconds. |
corner |
A string to specify a corner of the display. "top_left", "top_right", "bottom_left", or "bottom_right". |
width, height
|
A integer to specify width or height of the corner. |
size |
Integers to specify width or height of display size. |
scale |
A numeric to specify display scale. |
bin_dir |
A string for directory name of screenshot.exe on Win. |
tol |
A numeric for the tolerance of the comparison, in steps of 255 grayscale levels. 0 needs an exact match, which is right for a screenshot saved as PNG. Use a positive value for an image that has been through a lossy format such as JPEG. |
A numeric pair of xy location.
## Not run: sc <- screenshot() if(sc != ""){ sc_image <- imager::load.image(sc) w <- 100 h <- 80 pos_x <- 1 pos_y <- imager::height(sc_image) - h needle <- crop_image(sc_image, pos_x, pos_y, w, h) (locate_image(needle)) # center location pos <- locate_image(needle, center = FALSE) found <- crop_image(sc_image, pos[1], pos[2], w, h) layout(c(1:3)) plot(sc_image) plot(needle) plot(found) # usse `coner` to limit searching field # `coner` can be used in Windows pos <- locate_image(needle, corner = "bottom_left", center = FALSE) } ## End(Not run)## Not run: sc <- screenshot() if(sc != ""){ sc_image <- imager::load.image(sc) w <- 100 h <- 80 pos_x <- 1 pos_y <- imager::height(sc_image) - h needle <- crop_image(sc_image, pos_x, pos_y, w, h) (locate_image(needle)) # center location pos <- locate_image(needle, center = FALSE) found <- crop_image(sc_image, pos[1], pos[2], w, h) layout(c(1:3)) plot(sc_image) plot(needle) plot(found) # usse `coner` to limit searching field # `coner` can be used in Windows pos <- locate_image(needle, corner = "bottom_left", center = FALSE) } ## End(Not run)
locate_image().Searches the value that the needle and the haystack have in common and that appears fewest times in the haystack, and uses one of its positions as an anchor. Every candidate position of the needle is then a difference between a position of the value in the haystack and the anchor, so only those candidates have to be compared.
locate_ndl_in_hay(ndl_mt, hay_mt, exact = TRUE, timeout = 5, tol = 0)locate_ndl_in_hay(ndl_mt, hay_mt, exact = TRUE, timeout = 5, tol = 0)
ndl_mt, hay_mt
|
A matrix |
exact |
A logical. Check matching exactly or not. FALSE compares sampled pixels only. |
timeout |
A numeric for timeout seconds. |
tol |
A numeric for the tolerance of the comparison, in steps of 255 grayscale levels. 0 needs an exact match. Use a positive value for an image that has been through a lossy format such as JPEG. |
A numeric pair of xy location for needle image.
haystack_image <- imager::load.example("parrots") w <- 100 h <- 50 needle_image <- crop_image(haystack_image, 129, 257, w, h) hay_mt <- image2gray_matrix(haystack_image) ndl_mt <- image2gray_matrix(needle_image) (pos <- locate_ndl_in_hay(ndl_mt, hay_mt)) found <- crop_image(haystack_image, pos[1], pos[2], w, h) layout(c(1:3)) plot(haystack_image) plot(needle_image) plot(found)haystack_image <- imager::load.example("parrots") w <- 100 h <- 50 needle_image <- crop_image(haystack_image, 129, 257, w, h) hay_mt <- image2gray_matrix(haystack_image) ndl_mt <- image2gray_matrix(needle_image) (pos <- locate_ndl_in_hay(ndl_mt, hay_mt)) found <- crop_image(haystack_image, pos[1], pos[2], w, h) layout(c(1:3)) plot(haystack_image) plot(needle_image) plot(found)
Reset transparent background of an image
reset_transparent(path)reset_transparent(path)
path |
A character string specifying the file path to the image to be processed. |
The file path to the processed image.
## Not run: path <- "directory/image.png" reset_transparent(path) ## End(Not run)## Not run: path <- "directory/image.png" reset_transparent(path) ## End(Not run)
Save an image as a BMP file
save_bmp(image_data, path)save_bmp(image_data, path)
image_data |
A raster image data object, such as an array of pixel values or an R object representing an image. |
path |
The path to the file to be saved. |
Saves the image as a BMP file at the specified path.
## Not run: # Create an image data object image_data <- matrix(rnorm(100), ncol = 10) # Save the image as a BMP file save_bmp(image_data, "image.bmp") ## End(Not run)## Not run: # Create an image data object image_data <- matrix(rnorm(100), ncol = 10) # Save the image as a BMP file save_bmp(image_data, "image.bmp") ## End(Not run)
This function works only on windows.
save_clipboard_image(path = "", reset_transparent = TRUE)save_clipboard_image(path = "", reset_transparent = TRUE)
path |
Optional path to save the image to. If not specified, a temporary file will be created. |
reset_transparent |
A logical. If true, the image of transparent color will be removed. |
The path to the saved image file.
## Not run: # Save the image from the clipboard to a file save_clipboard_image("clipboard_image.png") ## End(Not run)## Not run: # Save the image from the clipboard to a file save_clipboard_image("clipboard_image.png") ## End(Not run)
Need to install screenshot.exe on Win by install_screenshot().
screenshot(file = "", bin_dir = "", quote = FALSE)screenshot(file = "", bin_dir = "", quote = FALSE)
file |
A string for file name of screenshot. |
bin_dir |
A string for directory name of screenshot.exe on Win. |
quote |
Deprecated and ignored. The command is always quoted. |
The command is always quoted, so that a path with a space (such as "C:/Program Files/R/R-4.5.1/library/screenshot") works.
A file name of screenshot. When "", screenshot will be saved in a tempral directory.
install_screenshot()
if(interactive()){ sc <- screenshot() if(sc != ""){ sc_image <- imager::load.image(sc) plot(sc_image) } }if(interactive()){ sc <- screenshot() if(sc != ""){ sc_image <- imager::load.image(sc) plot(sc_image) } }
Find screenshot exec file.
screenshot_exists(bin_dir = "")screenshot_exists(bin_dir = "")
bin_dir |
A string for directory name screenshot.exe exec file. No need on Mac and Linux. |
A logical.
screenshot_exists()screenshot_exists()
locate_ndl_in_hay().Get xy position of a value in a matrix
Helper function for locate_ndl_in_hay().
xy_pos(mt, val)xy_pos(mt, val)
mt |
A matrix |
val |
A matrix |
A numeric pairs of xy location.
nrow <- 4 mt <- matrix(1:12, nrow = nrow) xy_pos(mt, 5)nrow <- 4 mt <- matrix(1:12, nrow = nrow) xy_pos(mt, 5)