4

How to use .ICO image files in plots programmatically with/without conversion?

My question is a follow-up of How to convert .ICO to .PNG?, targeted to R, and with the following specific details:

  • I am not asking about methods involving transcluding the icons within an HTML, e.g. inside a shiny app (author: RStudio). That is of course possible.

  • Can the icons .ICO files be used directly in a base-R plot, a ggplot object (author: Hadley Wickham) , a lattice plot (author: Deepayan Sarkar)? And will this approach be robust to printing the output to PDF?

  • How to convert the ICO icons to PNG with or without a dedicated R library. This method would be careful to handle the icon's image size, depth, transparency, and other potentially important properties.

The motivation for this is simply that there are many open-source libraries of icons that one could use inside plot objects, e.g. Inserting an image to ggplot2, Display custom image as geom_point, Inserting an image to ggplot outside the chart area.

In the question I cite at the top, How to convert .ICO to .PNG?, there are examples of code in C# and in Python, but none in R.

Community
  • 1
  • 1
PatrickT
  • 10,037
  • 9
  • 76
  • 111

2 Answers2

2

Install ImageMagick on your system (your system - its not an R package). Its a set of open source image manipulation and conversion tools.

From the command line you can then convert icons with convert foo.ico foo.png. If the ICO file contains multiple icons you get foo-1.ico to foo-N.ico. If convert doesn't work then you've not installed ImageMagick properly.

If you want to do this from R then use the system function:

 name = "foo"
 system(paste0("convert ",name,".ico ",name,".png"))

Then use the generated PNG files. They will preserve all the properties (transparency etc), and will be usable as much as any other PNG files are usable in those packages, which is outside the scope of this question. You now have your PNG files.

One could write an R package to read ICO files natively (the spec is on Wikipedia) but it seems a bit pointless when ImageMagick exists and does such a brilliant job. Use it.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • Not sure I understand. The conversion is done by the ImageMagick "convert" command. Should work from R console or R Studio *if* ImageMagick is correctly installed. Have you tried it??? – Spacedman May 10 '16 at 09:37
  • No I haven't tried it, as I only have access to a browser at present (!) I think I get it: the ``system()`` function acts like a command-line call, right? I could do something like this ``system("ls -la")``, could I? EDIT: yep, just tested on https://www.rollapp.com/. – PatrickT May 10 '16 at 10:18
  • I'll wait a little to see if anyone comes up with something else and if no-one does I will accept your answer. Thanks again! – PatrickT May 11 '16 at 05:35
  • fyi there's now a `magick` package on `CRAN`, see answer below – moodymudskipper Dec 04 '17 at 02:36
2

There is now a magick package and you don't need to use system anymore:

library(magick)
path  <- "magic.png" # wherever you want to save
image <- image_read("https://rpubs.com/favicon.ico") # works with local path as well

In R studio, this will print our image in the plot window, for ico files it shows the different icons available in the file.

image 

Then you can save in the desired format :

image_write(image,path,"png")
file.exists(path)
# [1] TRUE

There's a really cool vignette:

moodymudskipper
  • 46,417
  • 11
  • 121
  • 167