# Import the base64 encoding library.importbase64# Pass the image data to an encoding function.defencode_image(image):image_content=image.read()returnbase64.b64encode(image_content)
Node.js
Node.js에서 이미지 파일을 base64로 인코딩하는 방법은 다음과 같습니다.
// Read the file into memory.varfs=require('fs');varimageFile=fs.readFileSync('/path/to/file');// Convert the image data to a Buffer and base64 encode it.varencoded=Buffer.from(imageFile).toString('base64');
자바
자바에서 이미지 파일을 base64로 인코딩하는 방법은 다음과 같습니다.
// Import the Base64 encoding library.importorg.apache.commons.codec.binary.Base64;// Encode the image.byte[]imageData=Base64.encodeBase64(imageFile.getBytes());
Go
Go에서 이미지 파일을 base64로 인코딩하는 방법은 다음과 같습니다.
import("bufio""encoding/base64""io""os")// Open image file.f,_:=os.Open("image.jpg")// Read entire image into byte slice.reader:=bufio.NewReader(f)content,_:=io.ReadAll(reader)// Encode image as base64.base64.StdEncoding.EncodeToString(content)
[[["이해하기 쉬움","easyToUnderstand","thumb-up"],["문제가 해결됨","solvedMyProblem","thumb-up"],["기타","otherUp","thumb-up"]],[["이해하기 어려움","hardToUnderstand","thumb-down"],["잘못된 정보 또는 샘플 코드","incorrectInformationOrSampleCode","thumb-down"],["필요한 정보/샘플이 없음","missingTheInformationSamplesINeed","thumb-down"],["번역 문제","translationIssue","thumb-down"],["기타","otherDown","thumb-down"]],["최종 업데이트: 2025-09-04(UTC)"],[[["\u003cp\u003eBase64 encoding allows you to send image data as text, which is useful for transmitting images in contexts where only text is supported.\u003c/p\u003e\n"],["\u003cp\u003eVarious operating systems like Linux, Mac OSX, and Windows, as well as their respective command lines, have native utilities or commands to perform base64 encoding on image files.\u003c/p\u003e\n"],["\u003cp\u003eMultiple programming languages including Python, Node.js, Java, and Go provide built-in libraries or functions for base64 encoding image data, each with its unique implementation.\u003c/p\u003e\n"],["\u003cp\u003eBase64-encoded image data can be directly embedded within a JSON request, specifically in the "content" field of an "image" object, for processing by applications requiring image input.\u003c/p\u003e\n"]]],[],null,["# Vertex AI services\n\nBase64 encoding\n---------------\n\nYou can provide image data for optical character recognition by specifying the URI path to the\nimage, or by sending the image data as base64-encoded text.\n\nMost development environments contain a native `base64` utility to\nencode a binary image into ASCII text data. To encode an image file: \n\n### Linux\n\n```\n base64 input.jpg \u003e output.txt\n```\n\n### Mac OSX\n\n```\n base64 -i input.jpg -o output.txt\n```\n\n### Windows\n\n```bash\n C:\u003e Base64.exe -e input.jpg \u003e output.txt\n```\n\n### PowerShell\n\n```bash\n [Convert]::ToBase64String([IO.File]::ReadAllBytes(\"./\u003cvar translate=\"no\"\u003einput.jpg\u003c/var\u003e\")) \u003e output.txt\n```\n\nYou can then use this output image data natively within the JSON request: \n\n```\n{\n \"requests\":[\n {\n \"image\":{\n \"content\": \"BASE64_ENCODED_DATA\"\n },\n \"features\": [\n {\n \"type\":\"TEXT_DETECTION\",\n \"maxResults\":1\n }\n ]\n }\n ]\n}\n```\n\nEach programming language has its own way of base64 encoding image files: \n\n### Python\n\nIn Python, you can base64-encode image files as follows: \n\n # Import the base64 encoding library.\n import base64\n\n # Pass the image data to an encoding function.\n def encode_image(image):\n image_content = image.read()\n return base64.b64encode(image_content)\n\n### Node.js\n\nIn Node.js, you can base64-encode image files as follows: \n\n // Read the file into memory.\n var fs = require('fs');\n var imageFile = fs.readFileSync('/path/to/file');\n\n // Convert the image data to a Buffer and base64 encode it.\n var encoded = Buffer.from(imageFile).toString('base64');\n\n### Java\n\nIn Java, you can base64-encode image files as follows: \n\n // Import the Base64 encoding library.\n import org.apache.commons.codec.binary.Base64;\n\n // Encode the image.\n byte[] imageData = Base64.encodeBase64(imageFile.getBytes());\n\n### Go\n\nIn Go, you can base64-encode image files as follows: \n\n```go\n import (\n \"bufio\"\n \"encoding/base64\"\n \"io\"\n \"os\"\n )\n\n // Open image file.\n f, _ := os.Open(\"image.jpg\")\n\n // Read entire image into byte slice.\n reader := bufio.NewReader(f)\n content, _ := io.ReadAll(reader)\n\n // Encode image as base64.\n base64.StdEncoding.EncodeToString(content)\n```"]]