43 lines
919 B
Go
43 lines
919 B
Go
package main
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
"go-bot/utils"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
img := utils.Image2Base64("/home/liyp/aa.jpg")
|
|
// 保存Base64字符串到文件
|
|
err := os.WriteFile("output_base64.txt", []byte(img), 0644)
|
|
if err != nil {
|
|
log.Fatalf("failed to write base64 string to file: %s", err)
|
|
}
|
|
// 将base64转回图片
|
|
// 去掉格式标志
|
|
|
|
parts := strings.Split(img, ",")
|
|
if len(parts) != 2 {
|
|
log.Fatalf("invalid data URI")
|
|
}
|
|
base64String := parts[1]
|
|
|
|
// 解码Base64字符串
|
|
fileContent, err := base64.StdEncoding.DecodeString(base64String)
|
|
if err != nil {
|
|
log.Fatalf("failed to decode base64 string: %s", err)
|
|
}
|
|
|
|
// 将解码后的内容保存为图片文件
|
|
err = os.WriteFile("output_image.jpg", fileContent, 0644)
|
|
if err != nil {
|
|
log.Fatalf("failed to write image file: %s", err)
|
|
}
|
|
|
|
fmt.Println("Image saved as output_image.jpg")
|
|
|
|
// println(img)
|
|
}
|