Skip to content

Commit

Permalink
last fixes before submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Hegghammer committed Nov 12, 2024
1 parent 021aeb0 commit b189cf2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
2 changes: 1 addition & 1 deletion R/send_to_dai.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ dai_sync <- function(file,
"skipHumanReview" = skip_rev,
"rawDocument" = list(
"content" = encoded_file,
"mimeType" = "image/tiff"
"mimeType" = "image/png"
)
)

Expand Down
50 changes: 16 additions & 34 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,25 @@
#' # A vector of image files:
#' image_to_pdf(images)
#' }

image_to_pdf <- function(files, pdf_name) {

# checks
if (!(is.vector(files))) {
stop("'files' input not a vector.")
}
}

if (!(is.character(files))) {
stop("'files' input not a character vector.")
}
}

if (!(grepl("\\.pdf$", pdf_name))) {
stop("Destination filename not .pdf")
}
}

# convert
magick::image_write(magick::image_read(files),
format = "pdf",
pdf_name
)
format = "pdf",
pdf_name
)
}

#' Check that a file is PDF
Expand All @@ -54,9 +52,7 @@ image_to_pdf <- function(files, pdf_name) {
#' \dontrun{
#' is_pdf("document.pdf")
#' }

is_pdf <- function(file) {

result <- suppressMessages(try(pdftools::pdf_info(file), silent = TRUE))

return(!(inherits(result, "try-error")))
Expand All @@ -73,13 +69,10 @@ is_pdf <- function(file) {
#' \dontrun{
#' is_json("file.json")
#' }

is_json <- function(file) {

result <- suppressMessages(try(jsonlite::fromJSON(file), silent = TRUE))

return(!(inherits(result, "try-error")))

}

#' Check that a string is a valid colour representation
Expand All @@ -94,13 +87,10 @@ is_json <- function(file) {
#' is_colour("red")
#' is_colour("#12345")
#' }

is_colour <- function(x) {

result <- suppressMessages(try(grDevices::col2rgb(x), silent = TRUE))

return(!(inherits(result, "try-error")))

}

#' PDF to base64 tiff
Expand All @@ -113,27 +103,23 @@ is_colour <- function(x) {
#' \dontrun{
#' doc_encoded <- pdf_to_binbase("document.pdf")
#' }

pdf_to_binbase <- function(file) {

if (!(is_pdf(file))) {
stop("Input file not a pdf.")
}
}

img <- magick::image_read_pdf(file)

img_gray <- magick::image_convert(img, colorspace = "Gray")

filepath <- file.path(tempdir(), "dai_temp.tiff")

magick::image_write(img_gray,
filepath,
format = "tiff"#,
#compression = "JPEG"
)
magick::image_write(img_gray,
filepath,
format = "png"
)

base64enc::base64encode(filepath)

}

#' Image to base64 tiff
Expand All @@ -146,25 +132,21 @@ pdf_to_binbase <- function(file) {
#' \dontrun{
#' img_encoded <- img_to_binbase("image.png")
#' }

img_to_binbase <- function(file) {

if (is_pdf(file)) {
stop("Input file is .pdf.")
}
}

img <- magick::image_read(file)

img_gray <- magick::image_convert(img, colorspace = "Gray")

filepath <- file.path(tempdir(), "dai_temp.tiff")

magick::image_write(img_gray,
filepath,
format = "tiff"#,
#compression = "JPEG"
)
magick::image_write(img_gray,
filepath,
format = "png"
)

base64enc::base64encode(filepath)

}

0 comments on commit b189cf2

Please sign in to comment.