grayscale option

This commit is contained in:
Ali Gator 2022-05-01 17:49:51 +02:00
parent bffd1d542b
commit bd7c32b526
3 changed files with 9 additions and 3 deletions

View File

@ -118,10 +118,10 @@ func encodeImage(imgData *image.RGBA) {
log.Println("Done.") log.Println("Done.")
} }
// convert RGBA pixel to grayscale // convert RGBA pixel to grayscale. BT.709 luminosity formula
func grayscale(pixel color.Color) color.Color { func grayscale(pixel color.Color) color.Color {
c := color.RGBAModel.Convert(pixel).(color.RGBA) c := color.RGBAModel.Convert(pixel).(color.RGBA)
gray := uint8((c.R + c.G + c.B) / 3) gray := uint8(0.2126*float64(c.R) + 0.7152*float64(c.G) + 0.0722*float64(c.B))
return color.RGBA{ return color.RGBA{
R: gray, R: gray,
G: gray, G: gray,
@ -167,9 +167,13 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color {
g := uint8(blend(oc1.G, oc2.G, oc1.A, oc2.A)) g := uint8(blend(oc1.G, oc2.G, oc1.A, oc2.A))
b := uint8(blend(oc1.B, oc2.B, oc1.A, oc2.A)) b := uint8(blend(oc1.B, oc2.B, oc1.A, oc2.A))
a := oc1.A + (1-oc1.A)*oc2.A a := oc1.A + (1-oc1.A)*oc2.A
return color.RGBA{ new := color.RGBA{
R: r, G: g, B: b, A: a, R: r, G: g, B: b, A: a,
} }
if ConfigRegister.Grayscale == true {
return grayscale(new)
}
return new
} }
// creates a new rectangle with the min height and width from both images // creates a new rectangle with the min height and width from both images

View File

@ -16,6 +16,7 @@ type Config struct {
Dimensions string `json:dimensions` Dimensions string `json:dimensions`
OutputWidth int `json:output_width` OutputWidth int `json:output_width`
OutputHeight int `json:output_height` OutputHeight int `json:output_height`
Grayscale bool
} }
// loads configuration values from json file // loads configuration values from json file

View File

@ -14,6 +14,7 @@ func main() {
flag.StringVar(&ConfigRegister.InputDir, "input", "/home/gator/Photos", "Input directory. Where to look the images from") flag.StringVar(&ConfigRegister.InputDir, "input", "/home/gator/Photos", "Input directory. Where to look the images from")
flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set") flag.StringVar(&ConfigRegister.BaseImage, "base-img", "", "Path to the base image to work with. Random image if not set")
flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "800x600", "Out image dimensions. <width>x<height>") flag.StringVar(&ConfigRegister.Dimensions, "dimensions", "800x600", "Out image dimensions. <width>x<height>")
flag.BoolVar(&ConfigRegister.Grayscale, "grayscale", false, "Output image in shade of gray (around 50)")
flag.Parse() flag.Parse()
// set output's width and height // set output's width and height