spywiree-priv / translateimage

Google Translate Images for free in Go
MIT License
2 stars 1 forks source link

Translate image from URL #6

Closed Avey777 closed 5 months ago

Avey777 commented 5 months ago

main.go

package main

import (
    "image/png"
    "io"
    "log"
    "net/http"
    "os"
    "path/filepath"

    languagecodes "github.com/spywiree/langcodes"
    "github.com/spywiree/translateimage"
)

// DownloadImageFromURL 下载图片并保存到指定目录
// DownloadImageFromURL downloads an image from a given URL and saves it to a specified directory.
func DownloadImageFromURL(imageURL, outputDir, outputFilename string) {
    // 发送HTTP GET请求获取图片数据
    // Send an HTTP GET request to retrieve image data
    response, err := http.Get(imageURL)
    if err != nil {
        log.Fatalf("Error downloading image: %v", err)
    }
    defer response.Body.Close()

    if response.StatusCode != http.StatusOK {
        log.Fatalf("Error downloading image: HTTP status %d", response.StatusCode)
    }

    // 构建输出图片文件的完整路径
    // Build the complete path for the output image file
    outputPath := filepath.Join(outputDir, outputFilename)

    // 创建输出目录,如果它不存在的话
    // Create the output directory if it doesn't exist
    err = os.MkdirAll(filepath.Dir(outputPath), 0755)
    if err != nil {
        log.Fatalf("Error creating output directory: %v", err)
    }

    // 创建输出文件
    // Create the output file
    outputFile, err := os.Create(outputPath)
    if err != nil {
        log.Fatalf("Error creating output image file: %v", err)
    }
    defer outputFile.Close()

    // 将图片数据写入输出文件
    // Write the image data to the output file
    _, err = io.Copy(outputFile, response.Body)
    if err != nil {
        log.Fatalf("Error saving image to file: %v", err)
    }

    log.Printf("Image downloaded and saved to %s", outputPath)
}

// TranslateAndSaveImage 翻译并保存指定目录中的图片
// TranslateAndSaveImage translates and saves the image in the specified directory
func TranslateAndSaveImage(inputDir, inputFilename, outputDir, outputFilename string) {
    // 构建输入图片文件的完整路径
    // Build the complete path for the input image file
    inputPath := filepath.Join(inputDir, inputFilename)
    inputPathAbs, err := filepath.Abs(inputPath)
    if err != nil {
        log.Fatalf("Error getting absolute path for input image: %v", err)
    }

    // 翻译图片
    // Translate the image
    img, err := translateimage.TranslateFile(
        inputPathAbs, languagecodes.DETECT_LANGUAGE, languagecodes.ENGLISH,
    )
    if err != nil {
        log.Fatalf("Error translating image: %v", err)
    }

    // 构建输出图片文件的完整路径
    // Build the complete path for the output image file
    outputPath := filepath.Join(outputDir, outputFilename)

    // 创建输出目录,如果它不存在的话
    // Create the output directory if it doesn't exist
    err = os.MkdirAll(filepath.Dir(outputPath), 0755)
    if err != nil {
        log.Fatalf("Error creating output directory: %v", err)
    }

    // 创建输出文件
    // Create the output file
    f, err := os.Create(outputPath)
    if err != nil {
        log.Fatalf("Error creating output image file: %v", err)
    }
    defer f.Close()

    // 将翻译后的图片编码为PNG格式并写入文件
    // Encode the translated image in PNG format and write it to the file
    err = png.Encode(f, img)
    if err != nil {
        log.Fatalf("Error encoding and saving output image: %v", err)
    }

    log.Printf("Image translated and saved to %s", outputPath)
}

func main() {

    inputDir := "inputDir"
    downInputImagesName := "image0" + ".png"

    // 调用DownloadImageFromURL函数,传入图片的URL、输出目录和输出文件名
    // Call the DownloadImageFromURL function, passing the image URL, output directory, and output file name as parameters
    imageURL := "https://cbu01.alicdn.com/img/ibank/O1CN01LdZDpx1qMLl44TLGV_!!2215685915481-0-cib.jpg"
    DownloadImageFromURL(imageURL, inputDir, downInputImagesName)

    //调用TranslateAndSaveImage函数,传入图片所在的目录、文件名、输出目录和输出文件名
    // Call the TranslateAndSaveImage function, passing the image directory, filename, output directory, and output filename as parameters
    TranslateAndSaveImage(inputDir, downInputImagesName, "outputDir/images", "output1.png")
}
Avey777 commented 5 months ago

The code cannot be contributed, but is placed here for easy retrieval later.