Compare commits

...

5 Commits

Author SHA1 Message Date
bffd1d542b resize picture based on specified dimensions 2022-05-01 15:33:55 +02:00
6c29d534d5 configurable input directory 2022-05-01 09:48:37 +02:00
11983d971b base image + skeleton for loading config file 2022-05-01 09:36:52 +02:00
fc1144cb1d simplify random img function 2022-05-01 09:15:49 +02:00
b7053c68aa logging each steps 2022-05-01 09:07:46 +02:00
6 changed files with 109 additions and 24 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*.jpg
*.png

View File

@ -5,6 +5,7 @@ import (
"image"
"image/color"
"image/jpeg"
"log"
"math"
"os"
"runtime"
@ -13,10 +14,10 @@ import (
// job passed to goroutines. blend color from img1 and img2 at position (x, y)
type blendColorJob struct {
X int
Y int
Img1 image.Image
Img2 image.Image
X int
Y int
Img1 image.Image
Img2 image.Image
}
// new color after blend, to apply at position (x, y)
@ -82,12 +83,21 @@ func (p *blendWorkerPool) SetImageWorker(newImage *image.RGBA) {
}
func (p *blendWorkerPool) BlendImages(img1 image.Image, img2 image.Image) {
dimensions := getDimensions(img1, img2)
log.Println("Blending the images...")
// dimensions := getMaxDimensions(img1, img2)
// output image, ready to receive pixel values
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
outImg := image.NewRGBA(dimensions)
// TODO: use a worker pool for those operations ?
// resize image
img1Resized := resize(img1)
img2Resized := resize(img2)
p.RunWorkers(outImg)
p.SendBlendJobs(dimensions, img1, img2)
p.SendBlendJobs(dimensions, img1Resized, img2Resized)
// first waitgroup to wait for the results to be ready before closing the channel
p.WorkerWG.Wait()
@ -103,9 +113,9 @@ func encodeImage(imgData *image.RGBA) {
outputFile := fmt.Sprintf("%s/%s", ConfigRegister.OutputDir, "output.jpg")
out, _ := os.Create(outputFile)
defer out.Close()
fmt.Print("Encoding the image...")
log.Println("Encoding the new image...")
jpeg.Encode(out, imgData, nil)
fmt.Println(" Done.")
log.Println("Done.")
}
// convert RGBA pixel to grayscale
@ -163,7 +173,7 @@ func blendColor(color1 color.Color, color2 color.Color) color.Color {
}
// creates a new rectangle with the min height and width from both images
func getDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
func getMaxDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
// get dimensions for both images
size1 := img1.Bounds().Size()
size2 := img2.Bounds().Size()
@ -178,3 +188,30 @@ func getDimensions(img1 image.Image, img2 image.Image) image.Rectangle {
return image.Rectangle{upLeft, lowRight}
}
// resizes image using dimensions from config register (nearest neighbour interpolation)
func resize(img image.Image) (resized *image.RGBA) {
imgSize := img.Bounds().Size()
dimensions := dimensionsToRectangle(ConfigRegister.OutputWidth, ConfigRegister.OutputHeight)
xscale := float64(imgSize.X) / float64(dimensions.Max.X)
yscale := float64(imgSize.Y) / float64(dimensions.Max.Y)
// creates new rescaled image based on a given image dimensions
resized = image.NewRGBA(dimensions)
// get pixels from the original image
for x := 0; x < dimensions.Max.X; x++ {
for y := 0; y < dimensions.Max.Y; y++ {
xp := int(math.Floor(float64(x) * xscale))
yp := int(math.Floor(float64(y) * yscale))
pixel := img.At(xp, yp)
resized.Set(x, y, pixel)
}
}
return
}
// returns a Rectangle of dimensions <width>x<height>
func dimensionsToRectangle(width int, height int) image.Rectangle {
upLeft := image.Point{0, 0}
lowRight := image.Point{width, height}
return image.Rectangle{upLeft, lowRight}
}

View File

@ -1,8 +1,45 @@
package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
type Config struct {
Method string
OutputDir string
Method string `json:method`
OutputDir string `json:outputdir`
InputDir string `json:inputdir`
BaseImage string `json:baseimage`
Dimensions string `json:dimensions`
OutputWidth int `json:output_width`
OutputHeight int `json:output_height`
}
// loads configuration values from json file
func (c *Config) LoadConfigFromFile(filePath string) {
file, _ := os.Open("conf.json")
defer file.Close()
decoder := json.NewDecoder(file)
configuration := Config{}
err := decoder.Decode(&configuration)
if err != nil {
fmt.Println("error:", err)
}
fmt.Println(configuration.Method)
fmt.Println(configuration.OutputDir)
}
// Set output file dimensions using string symbol (<width>x<height>)
func (c *Config) SetOutputDimensions() {
split := strings.Split(c.Dimensions, "x")
width, _ := strconv.Atoi(split[0])
height, _ := strconv.Atoi(split[1])
c.OutputWidth = width
c.OutputHeight = height
}
// global register to acccess configuration values
var ConfigRegister Config

15
fs.go
View File

@ -23,13 +23,14 @@ func loadImage(filename string) image.Image {
log.Fatal(err)
}
log.Println("Loading :", filename)
return imgData
}
// Walk through a folder recursively and returns a list of image paths
func getImagesList(path string) []string {
var imgs []string
err := filepath.Walk(path,
func(path string, info os.FileInfo, err error) error {
ext := strings.ToLower(filepath.Ext(path))
@ -50,13 +51,9 @@ func getImagesList(path string) []string {
}
// Randomly choose x number of image from a given folder
func getRandomImages(number int) []string {
func getRandomImage() string {
rand.Seed(time.Now().UnixNano())
var images []string
dir := getImagesList("/home/gator/Photos/")
for i := 0; i < number; i++ {
index := rand.Intn(len(dir))
images = append(images, dir[index])
}
return images
dir := getImagesList(ConfigRegister.InputDir)
index := rand.Intn(len(dir))
return dir[index]
}

18
main.go
View File

@ -2,6 +2,7 @@ package main
import (
"flag"
"image"
_ "image/jpeg"
_ "image/png"
)
@ -10,12 +11,23 @@ func main() {
// command line arguments
flag.StringVar(&ConfigRegister.Method, "blending", "darken", "Blending methods : darken, lighten, fuckyfun")
flag.StringVar(&ConfigRegister.OutputDir, "output", "./", "Output directory")
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.Dimensions, "dimensions", "800x600", "Out image dimensions. <width>x<height>")
flag.Parse()
// set output's width and height
ConfigRegister.SetOutputDimensions()
// get two random images and load them
imgs := getRandomImages(2)
img1 := loadImage(imgs[0])
img2 := loadImage(imgs[1])
var img1 image.Image
if ConfigRegister.BaseImage != "" {
img1 = loadImage(ConfigRegister.BaseImage)
} else {
img1 = loadImage(getRandomImage())
}
img2 := loadImage(getRandomImage())
// pool of workers unionizing, ready to blend a new picture using the power of friendship
var pool blendWorkerPool

View File

@ -1,7 +1,7 @@
package main
import (
"testing"
"testing"
)
func TestMain(t *testing.T) {