read.gif(filename, frame=0, flip=FALSE, verbose=FALSE)
write.gif(image, filename, col="gray", scale=c("smart", "never", "always"),
transparent=NULL, comment=NULL, delay=0, flip=FALSE, interlace=FALSE)
read.gif
URL's are also allowed.scale='always'
, numbers will frame=0
). Setting
frame=1
will ensure that output is always a 2D matrix containing the
first framerainbow
or
image
is an array of doubles in range [0, 1] thandelay
controls speed of the animation. It is number of hundredths
(1/100) of a second of delay between frames.print
function: row 1 is on top, image x-axis
corresponds to columns and y-axis corresponds to rows. However function
<image
will be set to transparent.interlace
d, or
reordered in such a way as to allow viewer to display image using 4 passes,
making image sharper with each pass. Irrelevant feature on fast computers.write.gif
does not return anything.
Function read.gif
returns a list with following fields:frame=0
only the first (usually global)
color-map (palette) is returned.image
to display transparent colors correctly one
should use y$col[y$transparent+1] = NA
.scale="never"
), in order to
prevent scaling.
If NA
or other infinite numbers are found in the image
by
write.gif
, they will be converted to numbers given by transparent
.
If transparent
color is not provided than it will be created, possibly
after reshretching.
There are some GIF files not fully supported by read.gif
function:
image
(part of R),
image.plot
and add.image
from
plot.im
from GDD
from package HTMLplot
from package jpeg
and png
.
Functions for directly reading and writing image files:
read.pnm
andwrite.pnm
fromread.ENVI
andwrite.ENVI
from this package
can process files in ENVI format. ENVI files can store 2D images and 3D data
(multi-frame images), and are supported by most GIS (Geographic Information
System) software including free "freelook".read.jpeg
fromrainbow
,topo.colors
,heat.colors
,terrain.colors.colors
,gray
tim.colors
in packagerich.colors
in packagebrewer.pal
fromcolorbrewer.palette
fromrgb
andhsv
creates palette from RGB or HSV 3-vectors.col2rgb
translates
palette colors to RGB 3-vectors.# visual comparison between image and plot
write.gif( volcano, "volcano.gif", col=terrain.colors, flip=TRUE,
scale="always", comment="Maunga Whau Volcano")
y = read.gif("volcano.gif", verbose=TRUE, flip=TRUE)
image(y$image, col=y$col, main=y$comment, asp=1)
# browseURL("file://volcano.gif") # inspect GIF file on your hard disk
# test reading & writing
col = heat.colors(256) # choose colormap
trn = 222 # set transparent color
com = "Hello World" # imbed comment in the file
write.gif( volcano, "volcano.gif", col=col, transparent=trn, comment=com)
y = read.gif("volcano.gif")
stopifnot(volcano==y$image, col==y$col, trn==y$transparent, com==y$comment)
# browseURL("file://volcano.gif") # inspect GIF file on your hard disk
# create simple animated GIF (using image function in a loop is very rough,
# but only way I know of displaying 'animation" in R)
x <- y <- seq(-4*pi, 4*pi, len=200)
r <- sqrt(outer(x^2, y^2, "+"))
image = array(0, c(200, 200, 10))
for(i in 1:10) image[,,i] = cos(r-(2*pi*i/10))/(r^.25)
write.gif(image, "wave.gif", col="rainbow")
y = read.gif("wave.gif")
for(i in 1:10) image(y$image[,,i], col=y$col, breaks=(0:256)-0.5, asp=1)
# browseURL("file://wave.gif") # inspect GIF file on your hard disk
# Another neat animation of Mandelbrot Set
jet.colors = colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan", "#7FFF7F",
"yellow", "#FF7F00", "red", "#7F0000")) # define "jet" palette
m = 400
C = complex( real=rep(seq(-1.8,0.6, length.out=m), each=m ),
imag=rep(seq(-1.2,1.2, length.out=m), m ) )
C = matrix(C,m,m)
Z = 0
X = array(0, c(m,m,20))
for (k in 1:20) {
Z = Z^2+C
X[,,k] = exp(-abs(Z))
}
image(X[,,k], col=jet.colors(256))
write.gif(X, "Mandelbrot.gif", col=jet.colors, delay=100)
# browseURL("file://Mandelbrot.gif") # inspect GIF file on your hard disk
file.remove("wave.gif", "volcano.gif", "Mandelbrot.gif")
# Display interesting images from the web
url = "http://www.ngdc.noaa.gov/seg/cdroms/ged_iib/datasets/b12/gifs/eccnv.gif"
y = read.gif(url, verbose=TRUE, flip=TRUE)
image(y$image, col=y$col, breaks=(0:length(y$col))-0.5, asp=1,
main="January Potential Evapotranspiration mm/mo")
url = "http://www.ngdc.noaa.gov/seg/cdroms/ged_iib/datasets/b01/gifs/fvvcode.gif"
y = read.gif(url, flip=TRUE)
y$col[y$transparent+1] = NA # mark transparent color in R way
image(y$image, col=y$col[1:87], breaks=(0:87)-0.5, asp=1,
main="Vegetation Types")
url = "http://talc.geo.umn.edu/people/grads/hasba002/erosion_vids/run2/r2_dems_5fps(8color).gif"
y = read.gif(url, verbose=TRUE, flip=TRUE)
for(i in 2:dim(y$image)[3])
image(y$image[,,i], col=y$col, breaks=(0:length(y$col))-0.5,
asp=1, main="Erosion in Drainage Basins")
Run the code above in your browser using DataLab