Carl
Carl

Reputation: 5779

R equivalent of Python's open("my.png").read()

In Python it is easy to read a png file:

f = open("my.png", "rb")
f.read()

I thought the equivalent in R would be:

paste0(readLines("my.png"), collapse = "\n")

But this yields a different result than the python code does for the same png file. How would I recreate open("my.png","rb").read() in R?

I am not interested in getting an array of RGB or greyscale like the png package offers.

Upvotes: 1

Views: 508

Answers (1)

Ajay Ohri
Ajay Ohri

Reputation: 3492

From Preview a saved PNG in an R device window

library(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))
grid::grid.raster(img)

from https://cran.r-project.org/doc/manuals/r-release/R-data.html#Image-files

Package pixmap has a function read.pnm to read ‘portable anymap’ images in PBM (black/white), PGM (grey) and PPM (RGB colour) formats. These are also known as ‘netpbm’ formats.

Packages bmp, jpeg and png read the formats after which they are named. See also packages biOps and Momocs, and Bioconductor package EBImage.

TIFF is more a meta-format, a wrapper within which a very large variety of image formats can be embedded. Packages rtiff and tiff can read some of the sub-formats (depending on the external libtiff software against which they are compiled). There some facilities for specialized sub-formats, for example in Bioconductor package beadarray.

Raster files are common in the geographical sciences, and package rgdal provides an interface to GDAL which provides some facilities of its own to read raster files and links to many others.

But

also see

imager package

https://cran.r-project.org/web/packages/imager/vignettes/gettingstarted.html

also see

https://www.bioconductor.org/packages/devel/bioc/manuals/EBImage/man/EBImage.pdf

another package is

https://github.com/leeroybrun/Bin2PNG

I am sorry but I am limited into a package search to help you

Upvotes: 2

Related Questions