def export_plt(color_arr): d = ceil(log(len(color_arr), 2)) dim = ceil(sqrt(2**d)) img = Image.new('RGBA', (dim, dim), (0, 0, 0, 255)) draw = ImageDraw.Draw(img) max_x, max_y = 0, 0 for i in range(len(color_arr)): x, y = hc.d2xy(d, i) max_x = max(max_x, x) max_y = max(max_y, y) draw.point((x, y), fill=tuple(*color_arr[i])) if max_x < dim or max_y < dim: img = img.crop(box=(0, 0, min(max_x, dim), min(max_y, dim))) fig, ax = plt.subplots(1, 1) im = ax.imshow(img) def onclick(event): try: print('Offset: {:X}'.format( hc.xy2d(d, floor(event.xdata), floor(event.ydata)))) except TypeError: pass # silently disregard typerrors (caused by clicking outside plot) cid = fig.canvas.mpl_connect('button_press_event', onclick) plt.show() plt.disconnect(cid) del (im)
def export_to_png_H(color_arr, file_out): if file_out[-4:] != '.png': file_out += '.png' d = ceil(log(len(color_arr), 2)) dim = ceil(sqrt(2**d)) img = Image.new('RGBA', (dim, dim), (0, 0, 0, 255)) draw = ImageDraw.Draw(img) max_x, max_y = 0, 0 for i in range(len(color_arr)): x, y = hc.d2xy(d, i) max_x = max(max_x, x) max_y = max(max_y, y) draw.point((x, y), fill=(color_arr[i][0], color_arr[i][1], color_arr[i][2], color_arr[i][3])) if max_x < dim or max_y < dim: img = img.crop(box=(0, 0, min(max_x, dim), min(max_y, dim))) print('Saving {}'.format(file_out)) img.save(file_out, 'PNG')
def main(): # BEGIN SETTING UP AUDIO OUT out = alsaaudio.PCM(alsaaudio.PCM_PLAYBACK, device='default', mode=alsaaudio.PCM_NONBLOCK) out.setchannels(1) out.setrate(int(sample_rate)) out.setformat(alsaaudio.PCM_FORMAT_S16_LE) out.setperiodsize(int(sample_rate)) #TODO this may be too big # END SETTING UP AUDIO OUT t = Thread(target=setup_camera_taker) t.start() # BEGIN SETTING UP HILBERT CURVE print("starts hilbert_curve") q = math.log(res1 * res1, 2) curve = [ # curve is list of tuples along hc hc.d2xy(q, i) for i in range(res1 * res1) ] print("done hilbert_curve") # END SETTING UP HILBERT CURVE while True: mutex.acquire() output = [gPic[curve[i][0]][curve[i][1]] for i in range(res1 * res1)] #print("Generating audio...") T = 1 / sample_rate # spacing between sample points N = int(sample_rate * signal_time_length) # number of sample points ### BEGIN IRFFT ROUTINE fs = np.zeros(N // 2 + 1) frequency = lowest_frequency for i in range(len(output)): fs[int( frequency * T * N)] = output[i] #this will be the amplitude for this frequency frequency += frequency_step outputAudio = np.fft.irfft(fs) ### END IRFFT ROUTINE #print("Converting...") outputAudio *= 1e-100 byte_data = outputAudio.astype('float16').tobytes() out.write(byte_data) cameraSem.release() """
def curve2plane(self,index): """ Get the coordinates of the point mapped to curve[index] :param index: the index of interest on the curve :return: coordinates on the plane. Integer Tuple (x,y) """ #TODO try Hilbert Curve #iterates stuff in each row first. so y gets incremented fastest # 2^M = self.size**2 # 2^(M/2) = self.size # log_2(self.size) = M/2 M = 2*np.ceil(np.log(self.side_length)/np.log(2)) hx,hy = hilbert_curve.d2xy(M,index) return min(hx,self.side_length-1),min(hy,self.side_length-1)
def hilbert_convert(data_linear): """Map the data in data_linear into a hilbert curve.""" total_blocks = len(data_linear) # Scale to contain a hilbert curve m = 1 while (2**m) * 2 < total_blocks: m += 1 pixels = (2**m) * 2 width = int(math.sqrt(pixels)) height = width data_linear = stretch_array(data_linear, pixels) data = bytearray(pixels) for i, byte in zip(range(len(data_linear)), data_linear): x, y = hilbert_curve.d2xy(m, i) index = (y * width) + x data[index] = byte return data, width, height
import numpy import hilbert_curve #~ from multiprocessing.pool import ThreadPool side = 32 newshape = side * side #~ p = ThreadPool() hilbert_indexs = range(0, newshape) hilbert_map = map(lambda x: hilbert_curve.d2xy(newshape, x), hilbert_indexs) def arrange(img): hilbert_flat = map(lambda x: numpy.array(img[hilbert_map[x]]), hilbert_indexs) hilbert_flat = numpy.array(hilbert_flat) return hilbert_flat
from hilbert_curve import d2xy import numpy as np import math as mth import matplotlib.pyplot as plt m = 3 print("Hilbert Curve index: %i" % m) n = 2**m print("Total points: %i" % n) xplt = [] yplt = [] xmax, ymax = 0, 0 for d in range(n): x, y = d2xy(m, d) if x > xmax: xmax = x if y > ymax: ymax = y xplt.append(x) yplt.append(y) print("xmax = %i and ymax = %i" % (xmax, ymax)) plt.plot(xplt, yplt, 'ks') plt.plot(xplt, yplt, 'k') plt.title("Hilbert curve, index: %i" % m) plt.xticks(np.linspace(0, xmax, 4)) plt.yticks(np.linspace(0, ymax, 4))
from Tkinter import * import hilbert_curve side = 320 newshape = side * side hidx = range(0,newshape) hmap = map(lambda x: hilbert_curve.d2xy(newshape, x), hidx) master = Tk() w = Canvas(master, width=side, height=side) w.pack() w.create_line(0, 0, 200, 100) w.create_line(0, 100, 200, 0, fill="red", dash=(4, 4)) w.create_rectangle(50, 25, 150, 75, fill="blue") mainloop()
def main(): while True: img = Image.open(input_file).convert("L") pixels = img.load() output = [pixels[hc.d2xy(math.log(x * y, 2), i)] for i in range(x * x)]
# Get the size of the image x, y = img.size # It doesn't fit the criteria. Tish tish tish. if ((x != y) or (checkPowerOf2(x) == False)): exit("The image has to be a power of 2.") print("Serialising pixels...") # Here's where it gets interesting. # We create the output list output = [] # Then, we iterate over every pixel. for i in range(0, x**2): # hc.d2xy() Turns a linear integer # into the x,y coordinates of a plane. hilbert = hc.d2xy(math.log(x * y, 2), i) # Use those coordinates to get the pixel value # And thus serialise a 2d plane. output.append(pixels[hilbert]) print("Generating audio...") # Generate the Audio output list outputAudio = [0] * (44100 * num_seconds) pixel_count = 0 # Iterate over every sing pixel in the list for pixel in output: if pixel_count % 10 == 0: print(pixel_count, "pixels completed out of",
def __init__(self, side): newshape = side * side self.hidx = range(0,newshape) self.hmap = map(lambda x: hilbert_curve.d2xy(newshape, x), self.hidx)
def func(x): (a, b) = hilbert_curve.d2xy(arraysize, x) return (x, a, b)