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