You can save a picture in byte format inside a variable in your program. You can then convert the bytes back into a file-like object using the BytesIO function of the io module and plot that object using the Image module from the Pillow library.

import io
import PIL.Image

with open("filename.png", "rb") as file:
    img_binary = file.read()

img = PIL.Image.open(io.BytesIO(img_binary))
img.show()

To save the binary data inside your program without having to read from the source file you need to encode it with something like base64, use print() and then simply copy the output into a new variable and remove the file reading operation from your code.

That would look like this:

img_encoded = base64.encodebytes(img_binary)
print(img_binary)

img_encoded = " " # paste the output from the console into the variable

the output will be very long, especially if you are using a big image. I only used a very small png for testing.

This is how the program should look like at the end:

import io
import base64
import PIL.Image

# with open("filename.png", "rb") as file:
#    img_binary = file.read()
# img_encoded = base64.encodebytes(img_binary)

img_encoded = b'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABX[...]'
img = PIL.Image.open(io.BytesIO(base64.decodebytes(img_encoded)))
img.show()
Answer from CaptainException on Stack Overflow
Top answer
1 of 4
3

You can save a picture in byte format inside a variable in your program. You can then convert the bytes back into a file-like object using the BytesIO function of the io module and plot that object using the Image module from the Pillow library.

import io
import PIL.Image

with open("filename.png", "rb") as file:
    img_binary = file.read()

img = PIL.Image.open(io.BytesIO(img_binary))
img.show()

To save the binary data inside your program without having to read from the source file you need to encode it with something like base64, use print() and then simply copy the output into a new variable and remove the file reading operation from your code.

That would look like this:

img_encoded = base64.encodebytes(img_binary)
print(img_binary)

img_encoded = " " # paste the output from the console into the variable

the output will be very long, especially if you are using a big image. I only used a very small png for testing.

This is how the program should look like at the end:

import io
import base64
import PIL.Image

# with open("filename.png", "rb") as file:
#    img_binary = file.read()
# img_encoded = base64.encodebytes(img_binary)

img_encoded = b'iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABX[...]'
img = PIL.Image.open(io.BytesIO(base64.decodebytes(img_encoded)))
img.show()
2 of 4
1

You can base64-encode your JPEG/PNG image which will make it into a regular (non-binary string) like this:

base64 -w0 IMAGE.JPG

Then you want to get the result into a Python variable, so repeat the command but copy the output to your clipboard:

base64 -w0 IMAGE.JPG | xclip -selection clipboard    # Linux
base64 -w0 IMAGE.JPG | pbcopy                        # macOS

Now start Python and make a variable called img and paste the clipboard into it:

img = 'PASTE'

It will look like this:

img = '/9j/4AAQSk...'     # if your image was JPEG
img = 'iVBORw0KGg...'     # if your image was PNG

Now do some imports:

from PIL import Image
import base64
import io

# Make PIL Image from base64 string
pilImage = Image.open(io.BytesIO(base64.b64decode(img)))

Now you can do what you like with your image:

# Print its description and size
print(pilImage)
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=200x100>

# Save it to local disk
pilImage.save('result.jpg')
Discussions

I made python code that generates beautiful images of your source code
Nice. FYI, a similar thing exist for vscode as a plugin. But nice nonetheless More on reddit.com
๐ŸŒ r/Python
30
222
August 22, 2022
Is it possible to code images into a python script? - Stack Overflow
Instead of using directories to reference an image, is it possible to code an image into the program directly? More on stackoverflow.com
๐ŸŒ stackoverflow.com
How do I input a jpg file into my code and then display that photo?
Pillow (PIL) , maybe? More on reddit.com
๐ŸŒ r/learnpython
6
1
October 23, 2022
Embedding images in a Python script with base64
I have a script that has images required for the UI, and I was wondering if it was possible to embed the images in the script, instead of having to save them somewhere on the hard drive. So I googled it, and came across something called base64. I can extract a string of characters from an image, ... More on discourse.mcneel.com
๐ŸŒ discourse.mcneel.com
0
0
October 16, 2020
People also ask

Why convert image to TXT in Python?
Documents are encoded in many ways, and image files may be incompatible with some software. To open and read such files, just convert them to appropriate file format.
๐ŸŒ
products.aspose.com
products.aspose.com โ€บ aspose.words โ€บ python via .net โ€บ conversion โ€บ image to txt in python
Convert image to TXT in Python
What is the maximum file size for image to TXT conversion supported by Aspose.Words for Python via .NET?
There are no file size limits for converting image files with Aspose.Words for Python via .NET.
๐ŸŒ
products.aspose.com
products.aspose.com โ€บ aspose.words โ€บ python via .net โ€บ conversion โ€บ image to txt in python
Convert image to TXT in Python
How to convert image into TXT?
Open the site: image into TXT. Drag and drop image files to quickly convert them into TXT. Or just use a code sample in Python with a few lines of code.
  1. Install Aspose.Words for Python via .NET.
  2. Add a library reference (import the library) to your Python project.
  3. Open the source image file in Python.
  4. Call the save() method, passing an output filename with TXT extension.
  5. Get the result of image conversion as TXT.
๐ŸŒ
products.aspose.com
products.aspose.com โ€บ aspose.words โ€บ python via .net โ€บ conversion โ€บ image to txt in python
Convert image to TXT in Python
๐ŸŒ
Lex
theresanaiforthat.com โ€บ s โ€บ image+to+python+code
Image to python code - There's An AI For Thatยฎ
Browse 14 Image to python code AIs. Includes tasks such as Code Documentation, Python code optimization, Image to HTML, Images and Image editing.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ working-images-python
Working with Images in Python - GeeksforGeeks
June 4, 2024 - from PIL import Image def main(): try: #Relative Path img = Image.open("picture.jpg") #Angle given img = img.rotate(180) #Saved in the same relative location img.save("rotated_picture.jpg") except IOError: pass if __name__ == "__main__": main() Note: There is an optional expand flag available as one of the argument of the rotate method, which if set true, expands the output image to make it large enough to hold the full rotated image. As seen in the above code snippet, I have used a relative path where my image is located in the same directory as my python code file, an absolute path can be used as well.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-can-you-convert-an-image-into-code-in-Python
How to convert an image into code in Python - Quora
Answer (1 of 2): It seems that in web development there is some standard for coding images with base64. Most support for such things are probably in Java or Javascript but I would not be surprised if you can find it for Python as well. Perhaps something like this: https://stackoverflow.com/que...
๐ŸŒ
Carbon
carbon.now.sh
Carbon | Create and share beautiful images of your source code
Carbon is the easiest way to create and share beautiful images of your source code.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ code-to-image-converter
Code to Image Converter Online supports JPG, PNG and WEBP and AVIF
Code to image converter is a simple but powerful tool that can be used to generate images from the code.
๐ŸŒ
GS Plugins
gsplugins.com โ€บ tools โ€บ code-to-image-converter
Code to Image Converter - GS Plugins
January 5, 2026 - Most Codes to Image Converters support languages like: JavaScript, Python, PHP, Java, C++, HTML, CSS, and more.
๐ŸŒ
GitHub
github.com โ€บ achimoraites โ€บ Python-Image-Converter
GitHub - achimoraites/Python-Image-Converter: Simple Python Image Converter for quick batch conversions of images to jpg/png
Simple Python Image Converter for quick batch conversions of images to jpg/png - achimoraites/Python-Image-Converter
Starred by 34 users
Forked by 11 users
Languages ย  Python 100.0% | Python 100.0%
๐ŸŒ
McNeel Forum
discourse.mcneel.com โ€บ scripting
Embedding images in a Python script with base64 - Scripting - McNeel Forum
October 16, 2020 - I have a script that has images required for the UI, and I was wondering if it was possible to embed the images in the script, instead of having to save them somewhere on the hard drive. So I googled it, and came across something called base64. I can extract a string of characters from an image, ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2499517 โ€บ how-to-insert-image-in-python
How to insert image in Python | Sololearn: Learn to code for FREE!
Python mein image ki coding ka ek simple udaharan yeh hai: ``` from PIL import Image img = Image.open('image.jpg') img.show() ``` 8th Dec 2024, 12:12 PM ยท Mati Rehman ยท Answer ยท Learn more efficiently, for free: Introduction to Python ยท ...
๐ŸŒ
OpenAI
platform.openai.com โ€บ docs โ€บ guides โ€บ image-generation
Image generation | OpenAI API
The optional action parameter (supported on gpt-image-1.5 and chatgpt-image-latest) controls this behavior: keep action: "auto" to let the model decide (recommended), set action: "generate" to always create a new image, or set action: "edit" to force editing (requires an image in context). Force image creation with action ยท python ยท
๐ŸŒ
Real Python
realpython.com โ€บ python-code-image-generator
Build a Code Image Generator With Python โ€“ Real Python
August 18, 2023 - The code image generator is a Flask project that creates stylish screenshots of a code snippet with the help of Pygments and Playwright. The project will run locally in your browser, and you have full control over the look of the code that you want to share: Once you run the Flask server, you can visit the application in the browser, add your own Python code, select a style, and download an image of the code to your computer...
๐ŸŒ
10015
10015.io โ€บ tools โ€บ code-to-image-converter
Code to Image Converter: Code Screenshots Online | 10015 Tools
Online Code to Image Converter is a free tool for converting code into images and download code screenshots. You can convert JavaScript, TypeScript HTML, CSS, Java, Python, PHP, Kotlin, Swift, Dart, Ruby, Rust, C#, Go and many programming languages into images.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ deep learning โ€บ generate-images-from-text-in-python-stable-diffusion
Generate Images from Text in Python - Stable Diffusion - GeeksforGeeks
July 23, 2025 - The Stable Diffusion model is a huge framework that requires us to write very lengthy code to generate an image from a text prompt. However, HuggingFace has introduced Diffusers to overcome this challenge. With Diffusers, we can easily generate numerous images by simply writing a few lines of Python code, without the need to worry about the architecture behind it.
๐ŸŒ
Ray
ray.so
Create beautiful images of your code
Turn your code into beautiful images. Choose from a range of syntax colors, hide or show the background, and toggle between a dark and light window.