Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
needs edge cases, i.e. if the pixel_size doesn't divide the width/height
  • Loading branch information
OscarDalby committed Jul 25, 2024
1 parent 3e9e93b commit c9b90a5
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module imgToPix
module ImgToPix

go 1.22.5
57 changes: 43 additions & 14 deletions img_to_pix.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import (
)

type ProcessConfig struct {
pixelSize int
scaling int
pixel_size int
scaling int
}

func main() {
base_img, err := get_base_image()
base_img, err := get_base_image("./input", "selfie")
if err != nil {
log.Fatalf("Failed to get base image: %v", err)
}
config := ProcessConfig{4, 2}
config := ProcessConfig{pixel_size: 64, scaling: 1}
processed_img, err := process_png(base_img, config)
if err != nil {
log.Fatalf("Failed to process PNG: %v", err)
Expand All @@ -28,8 +28,9 @@ func main() {
create_png(processed_img, "./output", "output_image")
}

func get_base_image() (image.Image, error) {
file, err := os.Open("./input/example.png")
func get_base_image(base_path string, file_name string) (image.Image, error) {
var path = fmt.Sprintf("%s/%s.png", base_path, file_name)
file, err := os.Open(path)
if err != nil {
fmt.Println("Error opening file:", err)
return nil, err
Expand Down Expand Up @@ -66,7 +67,7 @@ func get_base_image() (image.Image, error) {
return base_img, nil
}

func average_color(colors ...color.RGBA) color.RGBA {
func average_color(colors []color.RGBA) color.RGBA {
total_r := 0
total_g := 0
total_b := 0
Expand All @@ -85,23 +86,51 @@ func average_color(colors ...color.RGBA) color.RGBA {
average_g := total_g / num_colors
average_b := total_b / num_colors
average_a := total_a / num_colors
var average_color = color.RGBA{uint8(average_r), uint8(average_g), uint8(average_b), uint8(average_a)}
return average_color
return color.RGBA{uint8(average_r), uint8(average_g), uint8(average_b), uint8(average_a)}
}

func process_png(base_img image.Image, config ProcessConfig) (image.Image, error) {
// using config.pixelSize == 4
// using config.scaling == 2 // this is 1/scaling essentially
fmt.Printf("%v", config)
var width = base_img.Bounds().Dx()
var height = base_img.Bounds().Dy()
img := image.NewRGBA(image.Rect(0, 0, width, height))

for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
img.Set(x, y, color.RGBA{255, 0, 0, 120})
// for y := 0; y < height; y++ {
// for x := 0; x < width; x++ {
// img.Set(x, y, color.RGBA{255, 255, 255, 255})
// }
// }

for y := 0; y < height; y += config.pixel_size {
for x := 0; x < width; x += config.pixel_size {
var color_list []color.RGBA
var avg_color color.RGBA
count := 0
for dy := 0; dy < config.pixel_size; dy++ {
// if dy >= height {
// break
// }
for dx := 0; dx < config.pixel_size; dx++ {
// if dx >= width {
// break
// }
pixel := base_img.At(x+dx, y+dy)
r, g, b, a := pixel.RGBA()
color_list = append(color_list, color.RGBA{uint8(r), uint8(g), uint8(b), uint8(a)})
avg_color = average_color(color_list)
// iterate on x from x -> x + config.pixel_size
for j := 0; j < config.pixel_size; j++ {
for i := 0; i < config.pixel_size; i++ {
img.Set(x+i, y+j, avg_color)
}
}
count++
}
}

}
}

return img, nil
}

Expand Down
15 changes: 8 additions & 7 deletions img_to_pix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
)

func TestAverageColor(t *testing.T) {
result := average_color(
color.RGBA{1, 1, 1, 1},
color.RGBA{2, 2, 2, 2},
color.RGBA{3, 3, 3, 3},
color.RGBA{4, 4, 4, 4},
color.RGBA{5, 5, 5, 5},
)
color_list := []color.RGBA{
{1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4},
{5, 5, 5, 5},
}
result := average_color(color_list)
expected := color.RGBA{3, 3, 3, 3}
if result != expected {
t.Errorf("average_color(1,2,3,4,5) = %d; want %v", result, expected)
Expand Down
Binary file modified input/example.aseprite
Binary file not shown.
Binary file modified input/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added input/selfie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed output/output.png
Binary file not shown.
Binary file added output/output_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c9b90a5

Please sign in to comment.