|
| 1 | +#' Compare Images |
| 2 | +#' |
| 3 | +#' \code{juxtapose} compares the original image with the colorized version obtained with |
| 4 | +#' \code{colorize}. |
| 5 | +#' |
| 6 | +#' @param response a response object of a \code{colorize} function call. |
| 7 | +#' @param pane defines in which pane the image is displayed: \code{Plots} or \code{Viewer}. |
| 8 | +#' @param type defines the type of juxtaposition: \itemize{ |
| 9 | +#' \item \code{side-by-side} original image left, colorized right. |
| 10 | +#' \item \code{stacked} original above colorized. |
| 11 | +#' \item \code{c-focus} colorized center with original image border. |
| 12 | +#' \item \code{h-focus} above and below strips of original image. |
| 13 | +#' \item \code{v-focus} left and right strips of original image. |
| 14 | +#' \item \code{h-split} horizontally halved. |
| 15 | +#' \item \code{v-split} vertically halved. |
| 16 | +#' \item \code{d-split} diagonally halved. |
| 17 | +#' \item \code{u-animate} animated, colorized to the top (upwards). |
| 18 | +#' \item \code{s-animate} animated, colorized from left to right (sideways). |
| 19 | +#' } |
| 20 | +#' |
| 21 | +#' @return The function adds the comparison to the response object and returns it invisibly. |
| 22 | +#' |
| 23 | +#' @examples |
| 24 | +#' \dontrun{ |
| 25 | +#' # Compare images |
| 26 | +#' res <- colorize(img = "https://upload.wikimedia.org/wikipedia/commons/9/9e/Breadfruit.jpg") |
| 27 | +#' juxtapose(res) |
| 28 | +#' } |
| 29 | +#' @export |
| 30 | +#' @importFrom dplyr filter bind_cols |
| 31 | +#' @importFrom stringr str_detect |
| 32 | +#' @importFrom purrr map2_dfr |
| 33 | +juxtapose <- function(response, type = c("side-by-side", "stacked", "c-focus", "h-focus", "v-focus", "h-split", "v-split", "d-split", "u-animate", "s-animate"), |
| 34 | + pane = c("plot", "view", "none")) { |
| 35 | + |
| 36 | + # Remove Non-Responses |
| 37 | + response <- response %>% dplyr::filter(stringr::str_detect(response, "https://api.deepai.org/job-view-file/")) |
| 38 | + if (nrow(response) == 0) stop ("No URLs of colorized images found in response.") |
| 39 | + |
| 40 | + # Juxtapose |
| 41 | + jp <- purrr::map2_dfr( |
| 42 | + response$request, |
| 43 | + response$response, |
| 44 | + juxtapose_wh, |
| 45 | + pane = pane[1], |
| 46 | + type = type |
| 47 | + ) |
| 48 | + |
| 49 | + # Return respons |
| 50 | + response <- dplyr::bind_cols(response, jp) |
| 51 | + return(invisible(response)) |
| 52 | + |
| 53 | +} |
0 commit comments