Convert pdf to png with animation package - r

I am trying to convert this example pdf to png using the animation package the same way it´s done here Convert pdf to png in R
library("animation")
ani.options(outdir = "//Usuarios/Facturas")
pdf("Factura.pdf")
plot(1:10)
dev.off()
im.convert("Factura.pdf",
output = "Factura.png")
But I have this error:
Same error than here: Error using magick R to import PDF but that solution makes reference to an external software that I don´t find in the original post (and I haven´t installed). I red that Ghostscript do what I want (pdf to png), but isn´t what animation package suppose to do?
Or I messed up my brain?
Thanks

The animation package uses ImageMagick for some of its features (which is why ImageMagick is listed as a system requirement in the manual). ImageMagick in turn apparently uses Ghostscript for PDF rendering. So yeah: the Ghostscript issues discussed here are certainly worth looking into.

Finally I found the answer with another package, I am gonna explain if someone need it.
library(pdftools)
library(magick)
fichero="Factura.pdf"
png= pdftools::pdf_convert(fichero, dpi = 600)
# And if someone want to read it
png2 = image_read(png)
This doesn´t save any file .png, which is a perfect solution for my situation.

Related

How to get a screenshot directly into a variable when using R

I use R and the Magick package to process screenshots! Currently, I'm going through an external tool to get my screenshots. This tool gives me a file (png or other) that I have to reload to use it with Magick. Is there a package that would allow me to take screenshots (in whole or only partially) and to retrieve the image directly into a variable without going through an intermediate file? I found plenty of stuff for web-snapshots but nothing for a capture of my screen...
Thanks in advance

Can't read GeoTIFFs with Magick

I'm attempting to use the magick R package to do some image editing. However, I am unable to read in GeoTIFF files. When I try this:
magick::image_read(RGBFile)
I get the following error message:
Error in magick_image_readpath(path, density, depth, strip) :
Magick: Unknown field with tag 34737 (0x87b1) encountered.
`TIFFReadDirectory' # warning/tiff.c/TIFFWarnings/912
After doing some research, I've concluded that the GeoTIFF tags are the issue.
I've written to the package maintainer to see if an update to the package might allow GeoTIFFs to be read, but in the meantime, is anyone aware of a way of an way of removing GeoTIFF tags without re-copying the entire file? (Or any other efficient workaround for dealing with large >1 GB images)
Thanks!
I have contacted the package maintainer and this bug has been fixed! It hasn't made it onto CRAN as of this posting but in the meantime installing magick with devtools::install_github("ropensci/magick") should do the trick.

Error using magick R to import PDF

I have hundreds of PDFs that I want to crop. For each PDF, I have a unique set of coordinates around which to crop. I am trying to use the R's magick package (version ImageMagick 6.9.9.14), but I receive an error when importing a PDF.
This example from the magick documentation throws an error:
library(magick)
manual <- image_read('https://cran.r-project.org/web/packages/magick/magick.pdf', density = "72x72")
The error I receive is "Error in magick_image_readpath(path, density, depth, strip) : Magick: PDFDelegateFailed `The system cannot find the file specified.
' # error/pdf.c/ReadPDFImage/809"
When I check the config settings:
magick_config
I find that ghostscript is true. I am not sure if there are other settings required for reading in a PDF.
Has anyone else encountered a similar problem with magick? I am open to alternative packages with the ability to crop PDFs, if there are any.
I had the same problem on Windows. It was no R problem. In my case, I used ImageMagick 64-bit but had GhostScript 32-bit installed. After installing the 64-bit version of GhostScript it worked without any issues.
You could use tabulizer package.
library(tabulizer)
manual_url <- "https://cran.r-project.org/web/packages/magick/magick.pdf"
manual <- extract_text(manual_url)
For installing tabulizer follow exactly these steps.

devEMF export and powerpoint issue

Had some recent issues with my workflow. I normally print Rplots as .emf files using the devEMF package, and then edit them in powerpoint. [Import the emf file to powerpoint, right click on image, and select ungroup.] However, recently powerpoint isn't recognizing the vector format when I "ungroup."
I'm using a windows machine and powerpoint 2016.
Any thoughts would be VERY helpful.
Nate
Just to leave a definitive answer here -- Powerpoint does not yet allow editing ("ungrouping") of EMF+ graphics. Thus the solution is to tell devEMF to not use emfPlus features (the downside being that features such as transparency will not be available):
emf(file = "test.emf", emfPlus = FALSE)
plot(1:10)
dev.off()
The original poster's comment worked because the older version of devEMF did not use emfPlus by default.

Why doesn't savePlot("file.pdf", type="pdf") work by default?

Does anyone know why savePlot can't save to pdf in linux by default?
> savePlot("rv-3.pdf", type="pdf")
Error in match.arg(type) :
'arg' should be one of “png”, “jpeg”, “tiff”, “bmp”
lizard:~images$ R --version
R version 2.14.1 (2011-12-22)
...
?savePlot is pretty clear about this:
This works by copying the image surface to a file.
Hence you start with a raster representation and therefore can only go to a raster representation. It would be somewhat perverse to pipe a raster version of the plot in a PDF, which is a vector format (yes I know you can have rasters inside PDFs).
The functionality is limited to cario-based X11 devices and the documentation refers to copying the "on screen" representation hence the restrictions.
I suppose the other Answer to your question is: that functionality has not been implemented yet.
dev.copy2pdf does what you want:
plot(1:10)
dev.copy2pdf(file="~/test.pdf")
From reading the help files, I take it this will effectively replot your figure as a vector image in the file, which will usually be preferable to exporting your vector image into a raster format, as savePlot appears to do.
Try this:
pdf(file="rv-3.pdf")
plot(x,y)
dev.off()
you can also change the size by by adding height= or width= to the pdf function.

Resources