operations on bits (0,1) in python - image and sound mapping

operations on bits (0,1) in python - image and sound mapping

Georgios Lazaridis
Coding, art, science

Operations on bit arrays - mapping pixels and sound

Below are the codes responsible for opening any file in its 1/0 structure, using BitArray to make the task easy and then mapping them accordingly into black and white square format bitmaps or rythmic, algorithmically generated, sounds.

The outputs of the both programs:

The code is splitted into two files, the first is for the image and the second is for the sound.

code 1:

import sys
from bitstring import BitArray
from PIL import Image
import math
import numpy

# open file in binary mode and read , jpg, png, bmp and gif files tested

f = open('/path/to/file/test.jpg', 'rb+', 2)

print(f.read(50))
print("\n")
b = BitArray(f.read())

# print stuff - count and bits

# print(b.bin)

d = BitArray(f.read(100))

print(d.bin)
print(b.count(1))
print("\n")
print(b.count(0))
print("\n")
print(b.count(1) + b.count(2))
print("\n")

# bits to string

str = b.bin
value = str

# create image

map = {'1': (255, 255, 255),
       '0': (0, 0, 0)}

data = [map[pixel] for pixel in value]
img = Image.new('RGB', (int(math.sqrt(len(value))) + 1, int(math.sqrt(len(value))) + 1), "white")
img.putdata(data)

# print for help / calculate square surface and edge / +1 for extending the surface

print(int(math.sqrt(len(value))) + 1)
img.show()
img.save('/path/to/bitmap/1.bmp')

# check if binary code matches**** 1 - save the old code


text_file = open("/path/to/Output.txt", "w")
text_file.write(str)
text_file.close()
# open the output bmp file and print the code
a = open('/path/to/output/1.bmp', 'rb+', 2)
a = BitArray(a.read())
str1 = a.bin
text_file = open("/path/to/Output1.txt", "w")
text_file.write(str1)
text_file.close()

code 2:

import sys
import pyaudio
import binascii
import wave
from bitstring import BitArray
import audioop
import numpy as np
from numpy import *
import struct
import matplotlib.pyplot as plt

# INPUT

f = open('/path/to/your/file.jpg', 'br+', 2)

print(f.read(50))
c = f.read(100)
d = BitArray(f.read(1000))
print("\n")
print(d.bin)
print("\n")

# bits to str

str = d.bin
print(d.count(0), d.count(1))
print("\n")

# create sin wave in wav file

# frequency is the number of times a wave repeats a second

frequency = 1000
frequency1 = 2000

# duration

num_samples = d.count(1)
num_samples1 = d.count(0)

# The sampling rate of the analog to digital convert

sampling_rate = 48000.0

# loudness

amplitude = 16000


# OUTPUT

file = "/path/to/your/file/test.wav"

nframes=num_samples
nframes1=num_samples1

comptype="NONE"

compname="not compressed"

# mono or stereo?

nchannels=1

sampwidth=2

# bits to string


value = d.bin

str = value


# create array of data

oh = array(range(d.count(1)+d.count(0)), dtype='a5')


# sine wave formula with parameters

sine_wave = [np.sin(2 * np.pi * frequency * x/sampling_rate) for x in range(num_samples)]
sine_wave1 = [np.sin(2 * np.pi * frequency1 * x/sampling_rate) for x in range(num_samples1)]

# open file and setup

wav_file = wave.open(file, 'w')
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes, comptype, compname))
wav_file.setparams((nchannels, sampwidth, int(sampling_rate), nframes1, comptype, compname))

# map sound to value

i = '1'
o = '0'

for i in oh:
        for s in sine_wave:
                wav_file.writeframes(struct.pack('h', int(s*amplitude)))
        break
for o in oh:
        for s in sine_wave1:
                wav_file.writeframes(struct.pack('h', int(s*amplitude)))
        break