├── LICENSE ├── README.md └── ec.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 nero 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # EncodeIMAGE 3 | 4 | Encode an image to Base64 using Python 5 | 6 | --- 7 | 8 | ## Overview 9 | 10 | EncodeIMAGE is a simple Python utility to encode images into Base64 strings. This is useful for embedding images directly into text-based formats such as JSON, HTML, or sending images via APIs that accept Base64 encoded data. 11 | 12 | --- 13 | 14 | ## Features 15 | 16 | - Encode any image file to a Base64 string. 17 | - Simple, minimal Python script. 18 | - Supports common image formats (JPEG, PNG, etc.). 19 | - Easy to integrate into other projects. 20 | 21 | --- 22 | 23 | ## Requirements 24 | 25 | - Python 3.x 26 | 27 | --- 28 | 29 | ## Installation 30 | 31 | Clone the repository: 32 | 33 | ``` 34 | git clone https://github.com/Julianhornero/EncodeIMAGE.git 35 | cd EncodeIMAGE 36 | ``` 37 | 38 | No external dependencies are required. 39 | 40 | --- 41 | 42 | ## Usage 43 | 44 | Run the script `ec.py` with the path to your image file: 45 | 46 | ``` 47 | python ec.py path/to/your/image.jpg 48 | ``` 49 | 50 | Example: 51 | 52 | ``` 53 | python ec.py sample.jpg 54 | ``` 55 | 56 | The script will output the Base64 encoded string of the image to the console. 57 | 58 | --- 59 | 60 | ## Example Code Snippet 61 | 62 | Here is an example of how the encoding is done in `ec.py`: 63 | 64 | ``` 65 | import base64 66 | import sys 67 | 68 | def encode_image_to_base64(image_path): 69 | with open(image_path, "rb") as image_file: 70 | encoded_string = base64.b64encode(image_file.read()).decode('utf-8') 71 | return encoded_string 72 | 73 | if __name__ == "__main__": 74 | if len(sys.argv) ") 75 | sys.exit(1) 76 | 77 | image_path = sys.argv[1] 78 | base64_str = encode_image_to_base64(image_path) 79 | print(base64_str) 80 | ``` 81 | 82 | --- 83 | 84 | ## License 85 | 86 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 87 | 88 | -------------------------------------------------------------------------------- /ec.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import requests 3 | from google import genai 4 | from google.genai import types 5 | 6 | # Initialize Gemini client with your API key 7 | client = genai.Client(api_key="YOUR_GOOGLE_API_KEY") 8 | 9 | def encode_image_to_base64(image_path): 10 | with open(image_path, "rb") as f: 11 | image_bytes = f.read() 12 | return base64.b64encode(image_bytes).decode('utf-8') 13 | 14 | def decode_base64_to_image(base64_str, output_path): 15 | image_bytes = base64.b64decode(base64_str) 16 | with open(output_path, "wb") as f: 17 | f.write(image_bytes) 18 | 19 | def generate_content_with_image(image_path, prompt_text): 20 | # Read image bytes 21 | with open(image_path, "rb") as f: 22 | image_bytes = f.read() 23 | 24 | # Create image part for Gemini API 25 | image_part = types.Part.from_bytes(data=image_bytes, mime_type="image/jpeg") 26 | 27 | # Prepare prompt contents: image + text prompt 28 | contents = [ 29 | image_part, 30 | prompt_text 31 | ] 32 | 33 | # Call Gemini generate_content API 34 | response = client.models.generate_content( 35 | model="gemini-2.0-flash", 36 | contents=contents 37 | ) 38 | 39 | return response.text 40 | 41 | if __name__ == "__main__": 42 | input_image_path = "input.jpg" # Path to your input image 43 | output_image_path = "output.jpg" # Path to save any decoded image if needed 44 | prompt = "Describe this image." 45 | 46 | # Generate text content describing the image 47 | result_text = generate_content_with_image(input_image_path, prompt) 48 | print("Response from Gemini API:") 49 | print(result_text) 50 | 51 | # If the response contains base64 image data you want to decode, do it like this: 52 | # (Example: suppose you get base64 image string in `base64_image_str`) 53 | # decode_base64_to_image(base64_image_str, output_image_path) 54 | --------------------------------------------------------------------------------