Title: | Take Screenshots (Screen Capture) from R Command |
---|---|
Description: | Take screenshots from R command and locate an image position. |
Authors: | Toshikazu Matsumura [aut, cre]
|
Maintainer: | Toshikazu Matsumura <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.9.1 |
Built: | 2025-01-25 02:48:26 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_sample
clipboard_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)
This function returns the coordinates of the specified corner of the display. This function works only on windows.
display_corner(corner = "bottom_left", width = 600, height = 600)
display_corner(corner = "bottom_left", width = 600, height = 600)
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: display_corner("top_left", 800, 800) ## End(Not run)
## Not run: display_corner("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()
Cut off a part of image from a whole image.
hay2needle(haystack_image, pos_x, pos_y, w = 50, h = 20)
hay2needle(haystack_image, pos_x, pos_y, w = 50, h = 20)
haystack_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.
haystack_image <- imager::load.example("parrots") needle_image <- hay2needle(haystack_image, 200, 250, 100, 50) layout(c(1:2)) plot(haystack_image) plot(needle_image)
haystack_image <- imager::load.example("parrots") needle_image <- hay2needle(haystack_image, 200, 250, 100, 50) layout(c(1:2)) plot(haystack_image) plot(needle_image)
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)
is_all_same(ndl_mt, hay_mt, base_xy)
ndl_mt , hay_mt
|
A matrix |
base_xy |
A numeric pair of xy location. |
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, bin_dir = "" )
locate_image( needle_image, center = TRUE, exact = TRUE, timeout = 5, corner = NULL, width = 600, height = 300, bin_dir = "" )
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. |
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. |
bin_dir |
A string for directory name of screenshot.exe on Win. |
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 <- hay2needle(sc_image, pos_x, pos_y, w, h) (locate_image(needle)) # center location pos <- locate_image(needle, center = FALSE) found <- hay2needle(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 <- hay2needle(sc_image, pos_x, pos_y, w, h) (locate_image(needle)) # center location pos <- locate_image(needle, center = FALSE) found <- hay2needle(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()
.Locate needle image matrix position in a haystack_image matrix.
Helper function for locate_image()
.
locate_ndl_in_hay(ndl_mt, hay_mt, exact = TRUE, timeout = 5)
locate_ndl_in_hay(ndl_mt, hay_mt, exact = TRUE, timeout = 5)
ndl_mt , hay_mt
|
A matrix |
exact |
A logical. Check matching exactly or not. |
timeout |
A numeric for timeout seconds. |
A numeric pair of xy location for needle image.
haystack_image <- imager::load.example("parrots") w <- 100 h <- 50 needle_image <- hay2needle(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 <- hay2needle(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 <- hay2needle(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 <- hay2needle(haystack_image, pos[1], pos[2], w, h) layout(c(1:3)) plot(haystack_image) plot(needle_image) plot(found)
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 = "")
save_clipboard_image(path = "")
path |
Optional path to save the image to. If not specified, a temporary file will be created. |
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(bin_dir = "", file = "")
screenshot(bin_dir = "", file = "")
bin_dir |
A string for directory name of screenshot.exe on Win. |
file |
A string for file name of screenshot. |
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)