[R] PPM to BMP converter

Michael Sumner mdsumner at gmail.com
Wed May 9 11:38:25 CEST 2012


You could write it to BMP with rgdal, i.e. with dummy "world" coordinates:

x1 <- list(x = 1:5, y = 1:4, z = matrix(1:20, 5, 4))

require(sp)
x2 <- image2Grid(x1)

require(rgdal)
writeGDAL(x2, "file.bmp", driver = "BMP", type = "Byte", mvFlag = 255)

(I am not familiar with the details of what BMP can support, so I just
limit to Byte and stay safe. )

To show the round trip, convert back to "image" xyz list:

as.image.SpatialGridDataFrame(readGDAL("file.bmp"))$z
file.bmp has GDAL driver BMP
and has 4 rows and 5 columns
     [,1] [,2] [,3] [,4]
[1,]    1    6   11   16
[2,]    2    7   12   17
[3,]    3    8   13   18
[4,]    4    9   14   19
[5,]    5   10   15   20

I was a bit surprised to find that there seem to be no coercion
functions to convert to/from pixmap within the family of packages that
include maptools, sp, spatstat  and raster , they would be useful. I
would say that raster would be the best bet in the first instance
since it can store the different colour models directly. There is also
new "rasterImage" support in the graphics package that is worth
mentioning. All of these packages have different data structures, but
there is a growing set of coercion functions and it's not too hard
once you understand them to do it yourself.

Cheers, Mike.




On Wed, May 9, 2012 at 8:31 AM, ZHANG Yingqi <zhangyingqi at ivpp.ac.cn> wrote:
> Dear all,
>        Several days ago, I posted How to write a bmp file pixel by pixel. Instead of bmp, I succeeded in writing a PPM file by using the pixmap package. Thanks for the hint generously provided by Uwe Ligges.
>        Now I have a new question. How to convert a PPM file to BMP file in R? I know I can do this in photoshop or by some other softwares, but I think if I Can do this in R, that would be great! Would anyone please give me any hints? just hints, I will dig it out! Thanks a lot!
>
>
> Yingqi
> ********************
>
> Yingqi ZHANG
>
> Beijing P.O. Box 643, China 100044
> Institute of Vertebrate Paleontology and Paleoanthropology (IVPP)
> Chinese Academy of Sciences
> Tel: +86-10-88369378     Fax: +86-10-68337001
> Email: arvicolid at gmail.com
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
Michael Sumner
Institute for Marine and Antarctic Studies, University of Tasmania
Hobart, Australia
e-mail: mdsumner at gmail.com



More information about the R-help mailing list