Skip to content

Commit 64bef3b

Browse files
committed
Initial commit
0 parents  commit 64bef3b

15 files changed

+799
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: R
2+
sudo: false
3+
cache: packages
4+
r:
5+
- oldrel
6+
- release
7+
- devel

DESCRIPTION

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Package: colorizer
2+
Type: Package
3+
Title: Colorize and Restore Old Images Using the 'DeOldify' Image
4+
Colorization API on 'DeepAI'
5+
Version: 0.1.0
6+
Authors@R: person("David", "Zumbach", , "[email protected]", c("aut", "cre"))
7+
Description: Call the 'DeOldify' <https://github.com/jantic/DeOldify> image
8+
colorization API on 'DeepAI'<https://deepai.org/machine-learning-model/colorizer>
9+
to colorize black and white images.
10+
License: MIT + file LICENSE
11+
URL: https://github.com/zumbov2/colorizer
12+
BugReports: https://github.com/zumbov2/colorizer/issues
13+
SystemRequirements: ImageMagick++: ImageMagick-c++-devel (rpm) or
14+
libmagick++-dev (deb)
15+
Encoding: UTF-8
16+
LazyData: true
17+
RoxygenNote: 7.1.1
18+
Imports: httr, magick, purrr, dplyr, stringr, tibble, magrittr
19+
Suggests: gifski
20+
NeedsCompilation: no
21+
Packaged: 2020-10-22 10:52:56 UTC; david
22+
Author: David Zumbach [aut, cre]
23+
Maintainer: David Zumbach <[email protected]>

LICENSE

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
YEAR: 2020
2+
COPYRIGHT HOLDER: David Zumbach

NAMESPACE

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Generated by roxygen2: do not edit by hand
2+
3+
export(clsave)
4+
export(colorize)
5+
export(juxtapose)
6+
importFrom(dplyr,bind_cols)
7+
importFrom(dplyr,filter)
8+
importFrom(httr,POST)
9+
importFrom(httr,add_headers)
10+
importFrom(httr,content)
11+
importFrom(httr,status_code)
12+
importFrom(httr,upload_file)
13+
importFrom(magick,geometry_area)
14+
importFrom(magick,image_animate)
15+
importFrom(magick,image_append)
16+
importFrom(magick,image_crop)
17+
importFrom(magick,image_info)
18+
importFrom(magick,image_read)
19+
importFrom(magick,image_rotate)
20+
importFrom(magick,image_scale)
21+
importFrom(magick,image_write)
22+
importFrom(magrittr,"%>%")
23+
importFrom(purrr,map2_dfr)
24+
importFrom(purrr,map_dfr)
25+
importFrom(purrr,walk2)
26+
importFrom(stringr,str_detect)
27+
importFrom(stringr,str_remove_all)
28+
importFrom(stringr,str_replace_all)
29+
importFrom(tibble,tibble)

R/clsave.R

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#' Save Colorized and Juxtaposed Images
2+
#'
3+
#' \code{clsave} saves images that have been colorized using \code{colorize} or
4+
#' juxtaposed with \code{juxtapose}.
5+
#'
6+
#' @param response a response object of a \code{colorize} function call.
7+
#' @param destfile a character string or vector with the name where the images are saved.
8+
#'
9+
#' @return Besides saving, the function returns the response object invisibly.
10+
#'
11+
#' @examples
12+
#' \dontrun{
13+
#' # Save colorized images
14+
#' res <- colorize(img = "https://upload.wikimedia.org/wikipedia/commons/9/9e/Breadfruit.jpg")
15+
#' clsave(res, destfile = "colorized_version.jpg")
16+
#' }
17+
#' @export
18+
#' @importFrom dplyr filter
19+
#' @importFrom stringr str_detect str_remove_all str_replace_all
20+
#' @importFrom purrr walk2
21+
clsave <- function(response, destfile = "") {
22+
23+
# Remove Non-Responses
24+
response <- response %>% dplyr::filter(stringr::str_detect(response, "https://api.deepai.org/job-view-file/"))
25+
if (nrow(response) == 0) stop ("No URLs of colorized images found in response.")
26+
27+
# Save Colorized Images from URL
28+
if (ncol(response) == 2) {
29+
30+
i <- c(1:nrow(response))
31+
if (destfile == "") destfile <- rep("", nrow(response))
32+
purrr::pwalk(list(response$response, destfile, i), save_col_wh)
33+
34+
}
35+
36+
# Save Juxtaposed Images
37+
if (ncol(response) == 4) {
38+
39+
i <- c(1:nrow(response))
40+
if (destfile == "") destfile <- rep("", nrow(response))
41+
purrr::pwalk(list(response$jp_type, response$jp, destfile, i), save_jp_wh)
42+
43+
}
44+
45+
# Return response
46+
return(invisible(response))
47+
48+
}

R/colorize.R

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#' Call Image Colorization API
2+
#'
3+
#' \code{colorize} calls the DeOldify image colorization API on DeepAI.
4+
#'
5+
#' @param img path or url to the images to be colorized.
6+
#' @param key DeepAI api-key.
7+
#' @param pane defines in which pane the image is displayed: \code{plot}, \code{view} or \code{none}.
8+
#'
9+
#' @details With the standard api-key a few queries are possible. After registration on DeepAI
10+
#' \url{https://deepai.org/}, around 5000 free requests are currently possible.
11+
#'
12+
#' @return A tibble with the file locations of the original images and the response urls to
13+
#' the colorized images or status messages.
14+
#'
15+
#' @examples
16+
#' \dontrun{
17+
#' # Call image colorization API
18+
#' colorize(img = "https://upload.wikimedia.org/wikipedia/commons/9/9e/Breadfruit.jpg")
19+
#' }
20+
#' @export
21+
#' @importFrom purrr map_dfr
22+
colorize <- function(img, key = "quickstart-QUdJIGlzIGNvbWluZy4uLi4K", pane = c("plot", "view", "none")) {
23+
24+
# Colorize
25+
response <- purrr::map_dfr(
26+
img,
27+
colorize_wh,
28+
key = key,
29+
pane = pane[1]
30+
)
31+
32+
# Return response
33+
return(response)
34+
35+
}

R/colorizer.R

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#' \code{colorizer} package
2+
#'
3+
#' Colorize and Restore Old Images Using the 'DeOldify' Image Colorization API on 'DeepAI'
4+
#'
5+
#' See the README on
6+
#' \href{https://github.com/zumbov2/colorizer#readme}{GitHub}
7+
#'
8+
#' @docType package
9+
#' @name colorizer
10+
NULL
11+
12+
## quiets concerns of R CMD check re: the .'s that appear in pipelines
13+
if(getRversion() >= "2.15.1") {
14+
15+
utils::globalVariables(
16+
c("%>%")
17+
)
18+
}

R/juxtapose.R

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)