magick icon indicating copy to clipboard operation
magick copied to clipboard

magick::image_animate closes dev in Shiny

Open moldach opened this issue 3 years ago • 0 comments

I'm relatively new to Shiny so what I'm trying to achieve is the following:

Have an observeEvent where I

  • Take a picture with {shinySense}
  • Save a variable ggplot2 object as a jpeg
  • Also display this variable ggplot2 object
    observeEvent(myCamera(), {
      photo <- myCamera()
      # A temp file to save the output of the camera.
      # It will be deleted after renderPlot
      # outfile <- tempfile(fileext = '.jpeg')
      
      ##### Above/Below may need to be modified for DPED
      
      jpeg(filename = "DPED/dped/iphone/test_data/full_size_test_images/cam.jpeg")
      plot(as.raster(photo))
      dev.off()
      tmp <- image_read("DPED/dped/iphone/test_data/full_size_test_images/cam.jpeg")
      tmp <- image_trim(tmp)
      image_write(tmp, "DPED/dped/iphone/test_data/full_size_test_images/cam2.jpeg")
      
      output$snapshot <- renderPlot(
        {
          # plot the image into tab1
          plot(as.raster(photo))
        },
        bg = "transparent",
        execOnResize = TRUE
      )

      output$filter <- renderPlot(
        {
          if (input$filterPicker == "Gameboy") {
            photo <- image_read("DPED/dped/iphone/test_data/full_size_test_images/cam2.jpeg")
            gg <- ggboy::ggboy(photo, graphic = T)
            frames <- image_graph(width = 400, height = 400)
            print(gg)
            tmpggboy <- magick::image_animate(frames, 1) %>%
              image_write(tempfile(fileext = "jpg"), format = "jpg")
            #dev.new()
            gg
          } else if(input$filterPicker == "Lego Mosaic") {
            filter_from_cam("Lego Mosaic")
            dev.off()
            brickr_img
          }
        },
        bg = "transparent",
        execOnResize = TRUE
      )
      
      
      output$image_magick <- renderImage({
        if (input$filterPicker == "Gameboy") {
          image <- image_read(tmpggboy)
          tmpfile <- image %>%
            image_implode(input$implode) %>%
            image_blur(input$blur, input$blur) %>%
            image_rotate(input$rotation) %>%
            # image_resize(input$size) %>%
            image_write(tempfile(fileext = "jpg"), format = "jpg")
          # Return a list
          list(src = tmpfile, contentType = "photo/jpeg")
        } 
      })
    })

Inside of renderPlot() no-matter the order or the following it saves the tmpggboy but doesn't display gg.

output$filter <- renderPlot(
        {
          if (input$filterPicker == "Gameboy") {
            photo <- image_read("DPED/dped/iphone/test_data/full_size_test_images/cam2.jpeg")

            gg <- ggboy::ggboy(photo, graphic = T)
            frames <- image_graph(width = 400, height = 400)
            print(gg)
            tmpggboy <- magick::image_animate(frames, 1) %>%
              image_write(tempfile(fileext = "jpg"), format = "jpg")
            #dev.new()

            gg
          } else if(input$filterPicker == "Lego Mosaic") {
            filter_from_cam("Lego Mosaic")
            dev.off()
            brickr_img
          }
        },
        bg = "transparent",
        execOnResize = TRUE
      )

dev.new() doesn't work - not sure what to do

moldach avatar Mar 04 '21 20:03 moldach