-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.R
72 lines (55 loc) · 2.02 KB
/
app.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Minimal example of Shiny widget using 'magick' images
ui <- fluidPage(
titlePanel("Magick Shiny Demo"),
sidebarLayout(
sidebarPanel(
fileInput("upload", "Upload new image", accept = c('image/png', 'image/jpeg')),
textInput("size", "Size", value = "500x500!"),
sliderInput("rotation", "Rotation", 0, 360, 0),
sliderInput("blur", "Blur", 0, 20, 0),
sliderInput("implode", "Implode", -1, 1, 0, step = 0.01),
checkboxGroupInput("effects", "Effects",
choices = list("edge", "charcoal", "negate", "flip", "flop"))
),
mainPanel(
imageOutput("img")
)
)
)
server <- function(input, output, session) {
library(magick)
# Start with placeholder img
image <- image_read("https://raw.githubusercontent.com/ThinkR-open/collage/master/inst/tigrou/tigrou.jpg")
# When uploading new image
observeEvent(input$upload, {
if (length(input$upload$datapath))
image <<- image_convert(image_read(input$upload$datapath), "jpeg")
info <- image_info(image)
updateCheckboxGroupInput(session, "effects", selected = "")
updateTextInput(session, "size", value = paste0(info$width, "x", info$height, "!"))
})
# A plot of fixed size
output$img <- renderImage({
# Boolean operators
if("edge" %in% input$effects)
image <- image_edge(image)
if("charcoal" %in% input$effects)
image <- image_charcoal(image)
if("negate" %in% input$effects)
image <- image_negate(image)
if("flip" %in% input$effects)
image <- image_flip(image)
if("flop" %in% input$effects)
image <- image_flop(image)
# Numeric operators
tmpfile <- image %>%
image_resize(input$size) %>%
image_implode(input$implode) %>%
image_blur(input$blur, input$blur) %>%
image_rotate(input$rotation) %>%
image_write(tempfile(fileext='jpg'), format = 'jpg')
# Return a list
list(src = tmpfile, contentType = "image/jpeg")
})
}
shinyApp(ui, server)