0

Is there any library in Python that encodes data to bitmap patterns and decodes images to data, like the Piet language?

Hello World with Piet

Hello World with Piet

UPDATE

I want to get a string, like Hello, world or an URL and encode it as an image, then read this image and decode to a string. Something like a QR code, but using bitmap.

bodruk
  • 3,242
  • 8
  • 34
  • 52
  • Why are you trying to close this thread? – bodruk Mar 30 '16 at 19:56
  • The Piet language does not encode data in images. The source code of Piet programs is an image, and a Piet interpreter can execute a Piet source image. – Sven Marnach Mar 30 '16 at 20:46
  • 1
    My close vote reason would have been "unclear what you're asking". This question is underspecified. That didn't stop @jsbueno from making a valiant attempt anyway. – John Y Mar 30 '16 at 21:22
  • 2
    Take a look at [peng](https://github.com/NiklasRosenstein/peng). I think it does exactly what you want. – Niklas R Mar 31 '16 at 14:23

2 Answers2

3

You need an specification of how your data will be represented when used as an image. That is far more important than an existing piece of code that does the encoding.

On the case of the Piet Language, it is exactly what happens - this is the "language specification". From it people did build itnerpreters and tools (in a variety of other languages, including a Python interpreter).

But if you want to encode arbitrary data to an image, and then decode it back you first have to say how you want to encode it. The most straightforward way is one in which each byte of data is a color channel for one pixel, and a way to determine the exact length of the encoded data, and padding the bottom most image rows.

This would be straightforward to do with Python Imaging Library (Pillow)'s Image.frombytes method - though it would hardly result in a pleasing image.

One could also build a Python program to compile a Piet program that just "spills out" the input data back, given arbitrary data - and then, the piet interpreter above could decode the data.

The example bellow will pack a .wav sound file (I just did this at the Python console) as a colorfull image, pre-pending 4 bytes for the data length, and padding the bottom pixels with black.

import struct
from PIL import Image
data = open("2600PacManDies.wav", "rb").read()
size  = struct.pack("<I", len(data))
image_side = int(((len(data) + len(size)) / 3.0) ** 0.5) + 1
img = Image.frombytes("RGB", (image_side, image_side) , size + data + b"\x00" * (image_side ** 2 * 3 -  (len(size) + len(data))   ) )
img.save("2600_pacman_dies.png")

enter image description here

(Although as I've used a raw sound data file, one can even discover some patterns in the data looking at the image - the black stripes meaning the silent moments being the most obvious)

jsbueno
  • 99,910
  • 10
  • 151
  • 209
0

Depending on you plan for image manipulation or image processing , Python Image Library PIL may be what you are looking for.

Below is a demo code that applies inbuilt filters, derived from here There are numerous other image processing and pixel manipulation tools available within PIL

Demo Code

from PIL import Image
from PIL import ImageFilter

i = Image.open("gImage1.jpg")
im2 = i.filter(ImageFilter.EMBOSS)
im3 = i.filter(ImageFilter.FIND_EDGES)

im2.save("gImage1_EMBOSS.jpg")
im3.save("gImage1_FIND_EDGES.jpg")

Input Image

Input Image

Output Images
EMBOSS Filter

FIND EDGES FILTER

Anil_M
  • 10,893
  • 6
  • 47
  • 74