Image Editing Program with R (GUI)

R is not only for statistics. There is a big progress in packaging creation and there is a vast amount of libraries that let you do many things other than only statistics and data analysis. In this project it is demonstrated a GUI that lets the user make simple edits on photos. The edits user can do is to switch between GRB color mode and Grayscale, edit the Brightness of the image, the Contrast and the Gamma.  I wouldn’t recommend using this script on large pictures that are several megabytes because it makes the system crash.

Libraries

The program uses two libraries. The first is the gWidgets2 [1a,1b] library. It is a really simple one package that lets you create GUIs and it is written by John Verzani [2].

The second library is the EBImage [3a, 3b] written by Gregoire Pau, Andrzej Oles, Mike Smith, Oleg Sklyar, Wolfgang Huber. It is one of many packages that let you import images in R, but I preferred this because it lets you edit the images and it has a big support to may photo formats [4].

Problems

As it can be seen below, the code of the program uses the photo that user imports and writes the edited image as imageout.jpeg. The problem is that if the user decides to edit a big file, then because every editing re-writes the  imageout.jpeg file, makes the system’s RAM so much loaded that even a simple task cannot be done. So, I suggest if someone wants to use this script, then do it wisely and with small sized files.

Another problem that raised when I was writing this script was that I couldn’t find an easy way to reload/refresh the image displayed when edited. With a little help from stackoverflow.com and the author of gWidgets himself and other user too, this problem was solved [5].

Examples

In the first picture it is shown a photo of the football team Red Star Belgrade [6] from 1991 when the team won the European Cup (now Champions League) [7].

imgedit1

The second picture show the image switched to Gray mode and the third picture shows the image edited (brightness, contrast and gamma edited).

imgedit2grey

img3

The code

[code language=”r”]
imgedit<-function(){

require("EBImage")

require("gWidgets2")

print("Choose the folder that contains the photo")

setwd(choose.dir())

print("Choose the photo you want to edit")

imageinput<-file.choose()

image<-readImage(imageinput)

##defininig the color mode

colorimage<-c(RGBmode="rgb",Greymode="gray")

updateImage <-function (h,…) {

image1<-((svalue(brightness)+image*svalue(contrast))^(svalue(gamma)))

image2<-channel(image1,colorimage[svalue(colormode)])

imageout<-writeImage(image2,"imageout.jpeg")

svalue(img)<-"imageout.jpeg"

}

colormode <- gradio(names(colorimage), horizontal=FALSE,handler=updateImage)

brightness<-gslider(from=-1,to=1,by=.01, value=0,handler=updateImage)

contrast <- gslider(from=0,to=10,by=.01, value=1,handler=updateImage)

gamma <- gslider(from=0,to=3,by=0.01, value=1,handler=updateImage)

window <- gwindow("Image Editing")

BigGroup <- ggroup(cont=window)

group <- ggroup(horizontal=FALSE, container=BigGroup)

tmp <- gframe("Colormode", container=group)

add(tmp, colormode)

tmp <- gframe("Brightness", container=group)

add(tmp, brightness, expand=TRUE)

tmp <- gframe("Contrast", container=group)

add(tmp, contrast, expand=TRUE)

tmp <- gframe("Gamma", container=group)

add(tmp, gamma, expand=TRUE)

img <- gimage(imageinput,container=BigGroup)

obj <- glabel("Your edited photo is automaticaly \nsaved as ‘imageout.jpeg’ in the folder", container = group)

}
[/code]

References:

1a           http://cran.r-project.org/web/packages/gWidgets/gWidgets.pdf

1b           http://cran.r-project.org/web/packages/gWidgets/vignettes/gWidgets.pdf

2              http://wiener.math.csi.cuny.edu/verzani/

3a           http://www.bioconductor.org/packages/2.13/bioc/html/EBImage.html

3b      http://bioconductor.org/packages/release/bioc/vignettes/EBImage/inst/doc/EBImage-introduction.pdf

4              http://rwiki.sciviews.org/doku.php?id=tips:graphics-misc:display-images

5              http://stackoverflow.com/questions/20362464/display-and-manipulate-images-with-gwidgets

6              http://en.wikipedia.org/wiki/Red_Star_Belgrade

7              http://www.crvenazvezdafk.com/

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *