Home > Net >  Is there a way of attaching an image on a python code in such a way that it becomes part of the sour
Is there a way of attaching an image on a python code in such a way that it becomes part of the sour

Time:01-26

I'm a beginner in python and I'm trying to send someone my small python program together with a picture that'll display when the code is run.

I tried to first convert the image to a binary file thinking that I'd be able to paste it in the source code but I'm not sure if that's even possible as I failed to successfully do it.

CodePudding user response:

You could perhaps have your Python program download the image from a site where you upload files such as Google Drive, Mega, or Imgur. That way, you can always access and view the image easily without the need of running the program or for example converting the binary back into the image in the method you mentioned.

Otherwise, you could always store the image as bytes in a variable and have your program read this variable. I'm assuming that you really wish to do it this way as it would be easier to distribute as there is only one file that needs to be downloaded and run.

Or you could take a look at pyinstaller which is made for python programs to be easily distributed across machines without the need to install Python by packaging it as an executable (.exe) file! That way you can include the image file together by embedding it into the program. There are plenty of tutorials for pyinstaller you could google up. Note: Include the '--onefile' in your parameters when running pyinstaller as this will package the executable into a single file that the person you're sending it to can easily open whoever it may be-- granted the executable file can run on the user's operating system. :)

CodePudding user response:

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')

CodePudding user response:

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()
  • Related