Golang : Simple image viewer with Go-GTK
This is a really simple command line image viewer program written in Golang. Instead of viewing images with Photoshop or web browser, I need this cross-platform command line image viewer to view the images generated by my image processor program.
To get it running.
First, install the gtk
library.
Windows :
Download at http://www.gtk.org/download/windows.php
Linux: (you can use higher version instead of 2.0)
apt-get install libgtk2.0-dev libglib2.0-dev libgtksourceview2.0-dev
or
yum install libgtk2.0-dev libglib2.0-dev libgtksourceview2.0-dev
MacOSX:
Get the latest Homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
then
brew install gtk
Next :
go get github.com/mattn/go-gtk/gdkpixbuf
go get github.com/mattn/go-gtk/glib
go get github.com/mattn/go-gtk/gtk
Once the gtk
library is installed. Build this code below and execute ./imageviewer <your image file>
- such as ./imageviewer helloworld.png
Here you go!
imageviewer.go
package main
import (
"fmt"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
"os"
)
func ErrorCheck(err error) {
if err != nil {
panic(err)
}
}
func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage : %s <image file>\n", os.Args[0])
os.Exit(0)
}
imageFile := os.Args[1]
gtk.Init(nil)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle("Golang GTK Really Primitive Image Viewer")
window.SetIconName("gtk-dialog-info")
window.Connect("destroy", func(ctx *glib.CallbackContext) {
gtk.MainQuit()
}, "Happy coding!")
hbox := gtk.NewHBox(false, 1)
hpaned := gtk.NewHPaned()
hbox.Add(hpaned)
frame1 := gtk.NewFrame(imageFile)
framebox1 := gtk.NewHBox(false, 1)
frame1.Add(framebox1)
hpaned.Pack1(frame1, false, false)
image := gtk.NewImageFromFile(imageFile)
framebox1.Add(image)
window.Add(hbox)
imagePixBuffer := image.GetPixbuf()
horizontalSize := imagePixBuffer.GetWidth()
verticalSize := imagePixBuffer.GetHeight()
window.SetSizeRequest(horizontalSize, verticalSize)
window.ShowAll()
gtk.Main()
}
Sample output:
Reference:
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+11.8k Golang : Perform sanity checks on filename example
+5.7k Golang : Use NLP to get sentences for each paragraph example
+7.1k Golang : How to convert strange string to JSON with json.MarshalIndent
+7.5k Golang : How to execute code at certain day, hour and minute?
+6.8k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+15.3k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+6.1k CodeIgniter : form input set_value cause " to become & quot
+10k Golang : Detect number of faces or vehicles in a photo
+14.7k Golang : package is not in GOROOT during compilation
+14.9k JavaScript/JQuery : Detect or intercept enter key pressed example
+17.3k Golang : Clone with pointer and modify value
+27.8k Golang : Connect to database (MySQL/MariaDB) server