Created
January 18, 2013 17:19
-
-
Save hawx/4566266 to your computer and use it in GitHub Desktop.
Extends img with a lomo command. An attempt to replicate the shell script given in the README in pure Go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"code.google.com/p/graphics-go/graphics" | |
"github.com/nfnt/resize" | |
"github.com/hawx/img/blend" | |
"github.com/hawx/img/contrast" | |
"github.com/hawx/img/hsla" | |
"github.com/hawx/img/utils" | |
"flag" | |
"fmt" | |
"image" | |
"image/color" | |
) | |
func maskFor(in image.Image) image.Image { | |
b := in.Bounds() | |
// thumb size, corresponds to first convert command | |
thumbWidth := int(b.Dx() / 5) | |
thumbHeight := int(b.Dy() / 5) | |
thumb := image.NewRGBA(image.Rect(0, 0, thumbWidth, thumbHeight)) | |
final := image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy())) | |
// fill black (xc:black) | |
// draw white rectangle (1,1 : thumbWidth-1,thumbHeight-1) | |
for y := 0; y < thumbHeight; y++ { | |
for x := 0; x < thumbWidth; x++ { | |
if (y > 0 && y < thumbHeight-1) && (x > 0 && x < thumbWidth-1) { | |
thumb.Set(x, y, color.White) | |
} else { | |
thumb.Set(x, y, color.Black) | |
} | |
} | |
} | |
// apply Gaussian blur with radius=7, sigma=15 | |
graphics.Blur(thumb, thumb, &graphics.BlurOptions{2, 7}) | |
// now resize to original image size | |
resized := resize.Resize(uint(b.Dx()), uint(b.Dy()), thumb, resize.Bilinear) | |
// with gaussian blur radius=0, sigma=5 | |
graphics.Blur(final, resized, &graphics.BlurOptions{5, 0}) | |
return final | |
} | |
func main() { | |
var long = flag.Bool("long", false, "") | |
var short = flag.Bool("short", false, "") | |
var usage = flag.Bool("usage", false, "") | |
flag.Parse() | |
if *long { | |
fmt.Println( | |
" Applies a simple lomo effect to the image, boosting its saturation and\n" + | |
" composing with a black edged mask.", | |
) | |
} else if *short { | |
fmt.Println("applies a simple lomo effect to the image") | |
} else if *usage { | |
fmt.Println("lomo [options]") | |
} else { | |
img := utils.ReadStdin() | |
img = contrast.Adjust(img, 1.2) | |
img = hsla.Saturation(img, utils.Multiplier(1.2)) | |
img = blend.Multiply(img, maskFor(img)) | |
utils.WriteStdout(img) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment