Skip to content

Commit 04a22b6

Browse files
committed
Scale image to fit within the terminal
1 parent 027845f commit 04a22b6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.23.4
44

55
require (
66
github.com/BourgeoisBear/rasterm v1.1.2-0.20250103192908-b3632033a926
7+
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
78
github.com/qeesung/image2ascii v1.0.1
89
github.com/strukturag/libheif v1.19.5
910
golang.org/x/image v0.23.0
@@ -13,7 +14,6 @@ require (
1314
require (
1415
github.com/aybabtme/rgbterm v0.0.0-20170906152045-cc83f3b3ce59 // indirect
1516
github.com/mattn/go-isatty v0.0.20 // indirect
16-
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
1717
github.com/stretchr/testify v1.10.0 // indirect
1818
github.com/wayneashleyberry/terminal-dimensions v1.1.0 // indirect
1919
golang.org/x/term v0.27.0 // indirect

print_image.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/BourgeoisBear/rasterm"
1313
"github.com/hilli/icat/util"
14+
"github.com/nfnt/resize"
1415

1516
ascii "github.com/qeesung/image2ascii/convert"
1617

@@ -76,11 +77,12 @@ func PrintImageURL(imageURL string) error {
7677
}
7778

7879
func PrintImage(img *image.Image, imageConfig *image.Config, filename string, imageSize int64) error {
80+
var img2 image.Image
7981
sixelCapable, _ := rasterm.IsSixelCapable()
8082

8183
_, _, pw, ph := TermSize() // Get terminal height and width in pixels
8284

83-
kittyOpts := rasterm.KittyImgOpts{SrcWidth: uint32(imageConfig.Width), SrcHeight: uint32(imageConfig.Height)}
85+
kittyOpts := rasterm.KittyImgOpts{SrcWidth: uint32(pw), SrcHeight: uint32(ph)}
8486

8587
if pw < uint16(imageConfig.Width) {
8688
kittyOpts.SrcWidth = uint32(pw)
@@ -89,14 +91,22 @@ func PrintImage(img *image.Image, imageConfig *image.Config, filename string, im
8991
kittyOpts.SrcHeight = uint32(ph)
9092
}
9193

92-
// resizedImage := resizeImage(*img, kittyOpts.SrcHeight)
94+
if pw < uint16(imageConfig.Width) {
95+
img2 = resize.Resize(uint(pw), 0, *img, resize.NearestNeighbor)
96+
}
97+
98+
img2Height := img2.Bounds().Max.Y
99+
100+
if ph < uint16(img2Height) {
101+
img2 = resize.Resize(0, uint(ph), img2, resize.NearestNeighbor)
102+
}
93103

94104
switch {
95105
case rasterm.IsKittyCapable():
96-
return rasterm.KittyWriteImage(os.Stdout, *img, kittyOpts)
106+
return rasterm.KittyWriteImage(os.Stdout, img2, kittyOpts)
97107

98108
case rasterm.IsItermCapable():
99-
return rasterm.ItermWriteImage(os.Stdout, *img)
109+
return rasterm.ItermWriteImage(os.Stdout, img2)
100110

101111
case sixelCapable:
102112
// TODO: Convert image to a paletted format
@@ -111,7 +121,7 @@ func PrintImage(img *image.Image, imageConfig *image.Config, filename string, im
111121
// Ascii art fallback
112122
converter := ascii.NewImageConverter()
113123
convertOptions := ascii.DefaultOptions
114-
fmt.Print("\n", converter.Image2ASCIIString(*img, &convertOptions)) // Align image at the initial position instead of \n first?
124+
fmt.Print("\n", converter.Image2ASCIIString(img2, &convertOptions)) // Align image at the initial position instead of \n first?
115125
}
116126
return nil
117127
}

0 commit comments

Comments
 (0)