Skip to content
/ py2img Public

The official GitHub repository for the Python source code to image converter.

License

Notifications You must be signed in to change notification settings

Itzsten/py2img

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

py2img

The official GitHub repository for the Python source code to image converter.

Installation

Installation is as simple as:

pip install py2img

Why use py2img?

py2img features stabile image en- or decoding and aims at converting Python source into pure images while also being able to safely execute them without writing any temporary files or other unneeded cache.

How does it work?

decoder

The decoder reads the image colors and converts them to bytes and base64 decodes them which achieves a stronger source code protection rather than storing the source code openly, and optionally executes the code stored in it.

encoder

The encoder is a bit more complicated compared to the decoder, it first of all calculates necessary height and width for a square, optionally removing the last row if empty to achieve a comfortably read image. It then converts the data inputted to bytes and base64 encodes it to not store source openly as described before, to then copy the char codes to the image. Due to the base64 encoding UTF8 characters may be safely stored without losing data. Notice that UTF16 characters are not recommended. After copying data it optionally saves the image or returns a PIL/numpy object based on the function called.

Examples

Converting a Python Source file into an image

foo.py
print("Hello world from the image side!")
main.py
import py2img
py2img.encode.from_file("foo.py", output="bar.png")

Run main.py, and you should have an image saved like this:
foo.py as an image
Congrats! You have now converted your Python Source code to an image.

Converting images to Python source code

So, now that we've converted our Python source code into an image, how do we make use of it?
It's very straight forward, simply call the decoder function as following:

>>> import py2img
>>> py2img.decode.file('bar.png')
print("Hello world from the image side!")

Executing the image:

>>> import py2img
>>> py2img.decode.exec('bar.png')
Hello world from the image side!

Converting strings to image objects vice versa

Let's say you would want to get a numpy array or a Pillow image returned instead of the file being saved, you can do this easily:

>>> import py2img
>>> py2img.encode.from_file_to_numpy("foo.py")
[[[ 99  72  74]
  [112  98 110]
  [ 81 111  73]
  [107 104 108]
  [ 98  71 120]]

 [[118  73  72]
  [100 118  99]
  [109 120 107]
  [ 73  71  90]
  [121  98  50]]

 [[ 48 103 100]
  [ 71 104 108]
  [ 73  71 108]
  [116  89  87]
  [100 108  73]]

 [[ 72  78 112]
  [ 90  71  85]
  [104  73 105]
  [107  65  65]
  [ 65  61  61]]]

File to Pillow image:

>>> import py2img
>>> py2img.encode.from_file_to_PIL("foo.py")
<PIL.Image.Image image mode=RGB size=5x4 at 0x20223FA3700>

String to file:

import py2img
py2img.encode.from_str('print("Hello world from the image side!")', output='bar.png')

String to numpy array:

>>> import py2img
>>> py2img.encode.from_str_to_numpy('print("Hello world from the image side!")')
[[[ 99  72  74]
  [112  98 110]
  [ 81 111  73]
  [107 104 108]
  [ 98  71 120]]

 [[118  73  72]
  [100 118  99]
  [109 120 107]
  [ 73  71  90]
  [121  98  50]]

 [[ 48 103 100]
  [ 71 104 108]
  [ 73  71 108]
  [116  89  87]
  [100 108  73]]

 [[ 72  78 112]
  [ 90  71  85]
  [104  73 105]
  [107  65  65]
  [ 65  61  61]]]

String to Pillow image:

>>> import py2img
>>> py2img.encode.from_str_to_PIL('print("Hello world from the image side!")')
<PIL.Image.Image image mode=RGB size=5x4 at 0x2BDCF463700>

Using other image modes

What if you wanted an image in grayscale or RGBA?
It is quite simple, simply modify the img_mode argument to your choice:

import py2img
py2img.encode.from_file("foo.py", output="bar.png", img_mode='L')
# Note: The 1 colormode is not supported.

This gives an image looking like this:
The image of foo.py in grayscale
Notice: Image modes with less channels will have a larger aspect ratio, but commonly smaller size.

Contact

For contact please refer to Itzsten#3103 or itzsten@gmail.com.

Releases

No releases published

Packages

No packages published

Languages