-
Notifications
You must be signed in to change notification settings - Fork 0
/
im2ascii.go
77 lines (63 loc) · 2.24 KB
/
im2ascii.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package im2ascii
import (
"fmt"
"image"
"image/color"
_ "image/jpeg"
_ "image/png"
"os"
"golang.org/x/image/draw"
)
func CreateASCIIImage(img image.Image, width, height int) ([]string, error) {
// Resize the image to half the height as we're going to use half-blocks to represent two vertical pixels.
resizedImg := resizeImage(img, width, height) // Adjust width and height as needed
asciiImage := make([]string, 0)
for y := resizedImg.Bounds().Min.Y; y < resizedImg.Bounds().Max.Y; y += 2 {
var line string
for x := resizedImg.Bounds().Min.X; x < resizedImg.Bounds().Max.X; x++ {
topPixelColor := resizedImg.At(x, y)
bottomPixelColor := color.RGBA{255, 255, 255, 255} // Default to white if bottom pixel exceeds image bounds
if y+1 < resizedImg.Bounds().Max.Y {
bottomPixelColor, _ = resizedImg.At(x, y+1).(color.RGBA)
}
// Extract RGBA components for top and bottom pixels
r1, g1, b1, _ := topPixelColor.RGBA()
r2, g2, b2, _ := bottomPixelColor.RGBA()
// Print the half-block with top color as foreground and bottom color as background
line += fmt.Sprintf("\x1b[38;2;%d;%d;%dm\x1b[48;2;%d;%d;%dm▀\x1b[0m", r1>>8, g1>>8, b1>>8, r2>>8, g2>>8, b2>>8)
}
asciiImage = append(asciiImage, line)
}
return asciiImage, nil
}
func LoadImage(imagePath string) (image.Image, error) {
file, err := os.Open(imagePath)
if err != nil {
return nil, err
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return img, nil
}
func resizeImage(src image.Image, newWidth, newHeight int) image.Image {
srcBounds := src.Bounds()
srcWidth := srcBounds.Dx()
srcHeight := srcBounds.Dy()
// Calculate the aspect ratio of the source image
srcAspectRatio := float64(srcWidth) / float64(srcHeight)
// Calculate the target width and height while maintaining the aspect ratio
var targetWidth, targetHeight int
if float64(newWidth)/float64(newHeight) > srcAspectRatio {
targetWidth = int(float64(newHeight) * srcAspectRatio)
targetHeight = newHeight
} else {
targetWidth = newWidth
targetHeight = int(float64(newWidth) / srcAspectRatio)
}
dst := image.NewRGBA(image.Rect(0, 0, targetWidth, targetHeight))
draw.CatmullRom.Scale(dst, dst.Rect, src, srcBounds, draw.Over, nil)
return dst
}