Every like gives me a bigger pumpkin head

code clean up

+39 -30
base_images/pumpkin_body.jpg

This is a binary file and will not be displayed.

+39 -30
main.go
··· 4 "fmt" 5 "image" 6 "image/draw" 7 "image/png" 8 "os" 9 "path/filepath" ··· 27 } 28 defer bodyFile.Close() 29 30 - bodyImg, err := png.Decode(bodyFile) 31 if err != nil { 32 return nil, fmt.Errorf("failed to decode body image: %w", err) 33 } ··· 53 bodyImg: bodyImg, 54 headImg: headImg, 55 callCount: 0, 56 - scaleStep: 0.1, // Grow by 10% each call 57 outputDir: outputDir, 58 }, nil 59 } 60 61 - func (p *PumpkinHead) GrowHead() error { 62 p.callCount++ 63 64 // Calculate new scale (starts at 1.0, grows by scaleStep each time) ··· 66 67 fmt.Printf("Call #%d: Growing head to %.1fx original size\n", p.callCount, scale) 68 69 - // Get dimensions 70 bodyBounds := p.bodyImg.Bounds() 71 headBounds := p.headImg.Bounds() 72 73 - // Calculate scaled head dimensions 74 newHeadWidth := int(float64(headBounds.Dx()) * scale) 75 newHeadHeight := int(float64(headBounds.Dy()) * scale) 76 77 - // Create a new image for the scaled head 78 scaledHead := image.NewRGBA(image.Rect(0, 0, newHeadWidth, newHeadHeight)) 79 80 // Scale the head using bilinear interpolation 81 xdraw.BiLinear.Scale(scaledHead, scaledHead.Bounds(), p.headImg, headBounds, draw.Over, nil) 82 83 - // Create output image (use body dimensions) 84 output := image.NewRGBA(bodyBounds) 85 86 - // Draw body first 87 draw.Draw(output, bodyBounds, p.bodyImg, image.Point{0, 0}, draw.Src) 88 89 // Calculate position to center the head on top of the body 90 // Place head in upper portion of the image 91 headX := (bodyBounds.Dx() - newHeadWidth) / 2 92 - headY := bodyBounds.Dy()/8 - newHeadHeight/2 + 1900 // Position in upper area (adjusted down by 1500px) 93 94 headPos := image.Rect(headX, headY, headX+newHeadWidth, headY+newHeadHeight) 95 96 // Draw scaled head on top 97 draw.Draw(output, headPos, scaledHead, image.Point{0, 0}, draw.Over) 98 99 - // Save to file 100 - filename := filepath.Join(p.outputDir, fmt.Sprintf("bighead_%03d.png", p.callCount)) 101 - outFile, err := os.Create(filename) 102 - if err != nil { 103 - return fmt.Errorf("failed to create output file: %w", err) 104 - } 105 - defer outFile.Close() 106 - 107 - if err := png.Encode(outFile, output); err != nil { 108 - return fmt.Errorf("failed to encode PNG: %w", err) 109 - } 110 - 111 - fmt.Printf("Saved to: %s\n", filename) 112 - return nil 113 } 114 115 func main() { 116 - pumpkin, err := NewPumpkinHead("base_images/pumpkin_body.png", "base_images/pumpkin_head.png", "bighead") 117 if err != nil { 118 - fmt.Fprintf(os.Stderr, "Error initializing: %v\n", err) 119 - os.Exit(1) 120 } 121 122 fmt.Println("Pumpkin Head Grower!") 123 fmt.Println("==================") 124 125 // Call the method 5 times to demonstrate 126 - for i := 0; i < 5; i++ { 127 - if err := pumpkin.GrowHead(); err != nil { 128 - fmt.Fprintf(os.Stderr, "Error growing head: %v\n", err) 129 - os.Exit(1) 130 } 131 } 132 133 fmt.Println("\nDone! Check the 'bighead' directory for results.") 134 }
··· 4 "fmt" 5 "image" 6 "image/draw" 7 + "image/jpeg" 8 "image/png" 9 "os" 10 "path/filepath" ··· 28 } 29 defer bodyFile.Close() 30 31 + bodyImg, err := jpeg.Decode(bodyFile) 32 if err != nil { 33 return nil, fmt.Errorf("failed to decode body image: %w", err) 34 } ··· 54 bodyImg: bodyImg, 55 headImg: headImg, 56 callCount: 0, 57 + scaleStep: 0.05, // How much my big head grows each call 58 outputDir: outputDir, 59 }, nil 60 } 61 62 + func (p *PumpkinHead) GrowHead() (image.Image, error) { 63 p.callCount++ 64 65 // Calculate new scale (starts at 1.0, grows by scaleStep each time) ··· 67 68 fmt.Printf("Call #%d: Growing head to %.1fx original size\n", p.callCount, scale) 69 70 bodyBounds := p.bodyImg.Bounds() 71 headBounds := p.headImg.Bounds() 72 73 newHeadWidth := int(float64(headBounds.Dx()) * scale) 74 newHeadHeight := int(float64(headBounds.Dy()) * scale) 75 76 scaledHead := image.NewRGBA(image.Rect(0, 0, newHeadWidth, newHeadHeight)) 77 78 // Scale the head using bilinear interpolation 79 xdraw.BiLinear.Scale(scaledHead, scaledHead.Bounds(), p.headImg, headBounds, draw.Over, nil) 80 81 output := image.NewRGBA(bodyBounds) 82 83 + // Drawing the base layer image 84 draw.Draw(output, bodyBounds, p.bodyImg, image.Point{0, 0}, draw.Src) 85 86 // Calculate position to center the head on top of the body 87 // Place head in upper portion of the image 88 headX := (bodyBounds.Dx() - newHeadWidth) / 2 89 + headY := bodyBounds.Dy()/8 - newHeadHeight/2 + 1900 // the 1900 is to line up with my head 90 91 headPos := image.Rect(headX, headY, headX+newHeadWidth, headY+newHeadHeight) 92 93 // Draw scaled head on top 94 draw.Draw(output, headPos, scaledHead, image.Point{0, 0}, draw.Over) 95 96 + return output, nil 97 } 98 99 func main() { 100 + pumpkin, err := NewPumpkinHead("base_images/pumpkin_body.jpg", "base_images/pumpkin_head.png", "bighead") 101 + 102 if err != nil { 103 + writeErrorAndExit(err) 104 } 105 106 fmt.Println("Pumpkin Head Grower!") 107 fmt.Println("==================") 108 109 // Call the method 5 times to demonstrate 110 + for i := 0; i < 25; i++ { 111 + newPic, growingPains := pumpkin.GrowHead() 112 + if growingPains != nil { 113 + writeErrorAndExit(err) 114 } 115 + 116 + saveToFiles(pumpkin, newPic) 117 } 118 119 fmt.Println("\nDone! Check the 'bighead' directory for results.") 120 } 121 + 122 + func saveToFiles(pumpkin *PumpkinHead, newPic image.Image) { 123 + // Save to file 124 + filename := filepath.Join(pumpkin.outputDir, fmt.Sprintf("bighead_%03d.png", pumpkin.callCount)) 125 + outFile, err := os.Create(filename) 126 + if err != nil { 127 + writeErrorAndExit(err) 128 + } 129 + 130 + defer outFile.Close() 131 + 132 + if err := png.Encode(outFile, newPic); err != nil { 133 + writeErrorAndExit(err) 134 + } 135 + } 136 + 137 + func writeErrorAndExit(err error) { 138 + _, err = fmt.Fprintf(os.Stderr, "Error: %v\n", err) 139 + if err != nil { 140 + panic(err) 141 + } 142 + os.Exit(1) 143 + }