def RGB_to_PNG(RGB_array, fnam): """Output an RGB array [shape (n,m,3)] as a .png file.""" # Get array shape n, m = RGB_array.shape[:2] # Replace filename ending with .png fnam_out = os.path.splitext(fnam)[0] + ".png" png.fromarray(RGB_array.reshape((n, m * 3)), mode="RGB").save(fnam_out)
def print_same_direction(width, height, angle, slant, gap, quantity_top, quantity_left, buffer=0, base_height=3, animate=False, save_image=False, save_3d_model=False, xy_res=.05, z_res=.05): buffer += int(height * slant) # print(buffer) hairs = [[Cilium( width, height, angle, slant, position=((width + gap) * x, (width + gap) * y) ) for x in range(quantity_top)] for y in range(quantity_left) ] layers = [] if save_image: folder_path = "ImagesFromEpoch" + str(int(time.time())) os.makedirs(folder_path) for i in range(base_height): layers.append([[True for x in range((width + gap) * quantity_top + 2 * buffer)] for y in range((width + gap) * quantity_left + 2 * buffer) ]) for i in range(height): # For every layer picture_array = [[False for x in range((width + gap) * quantity_top + 2 * buffer)] for y in range((width + gap) * quantity_left + 2 * buffer) ] # would be MUCH improved with matrix operations for row in hairs: for hair in row: # print ("hair position: ", hair.position) hair_array, position, offset = hair.get_new_layer() for y in range(len(hair_array)): # print_array(picture_array) # print("buffer: ", buffer, " offset: ", offset, " position: ", position) big_row = int(buffer + offset[1] + position[1] + y) start = int(buffer + offset[0] + position[0]) end = int(start + len(hair_array[y])) picture_array[big_row] \ = (picture_array[big_row][:start] + hair_array[y] + picture_array[big_row][end:]) if save_image: new_picture = picture_array[:] for y in range(len(new_picture)): for x in range(len(new_picture[y])): if new_picture[y][x]: new_picture[y][x] = 255 else: new_picture[y][x] = 0 img = png.fromarray(picture_array, mode='L') img.save(folder_path + '/' + str(i) +'.png') if animate: print_array(picture_array) time.sleep(.25) os.system('cls') if save_3d_model: layers.append(picture_array) if save_3d_model: fastSTL(layers, xy_res, z_res)
def save_image(image_numpy, image_path, save_rgb=False): #image_pil = Image.fromarray(image_numpy) #image_pil.save(image_path) if not save_rgb: image_pil = png.fromarray(image_numpy, 'RGBA;8') image_pil.save(image_path) else: img = Image.fromarray(image_numpy.astype('uint8')) img.save(image_path)
def make_grayscale(image): planes = image[3]['planes'] array = np.array(list(image[2]), dtype='int16').reshape(image[1] * image[0] * planes) grayscaled = make_array_grayscale(array, image[0], image[1], image[3]['planes']) image[3]['greyscale'] = True image[3]['alpha'] = False image[3]['planes'] = 1 return png.fromarray(grayscaled, 'L', image[3])
def recode_image(target_image_path, blank_image_path, recoded_image_path, encode_data_path): encoded = encode_file(target_image_path) blank_image = png.Reader(blank_image_path).read() blank_image_matrix = np.array(list(blank_image[2])).reshape( blank_image[1], blank_image[0]) if blank_image_matrix.shape != (encoded.height, encoded.width): raise Exception( f'The shape of blank image {blank_image_matrix.shape} is not equal to the shape of ' f'target image ({encoded.height}, {encoded.width})') matrix = decode(encoded, blank_image_matrix) img = png.fromarray(matrix, 'L', blank_image[3]) img.save(recoded_image_path) fp = open(encode_data_path, 'w') json.dump(encoded, fp, indent=1, default=lambda o: o.__dict__) fp.close()
unaligned_SPCR_nuc.append(record_dict[ID.split(' ')[0]].seq) c = Counter(unaligned_SPCR_nuc) freq_list = c.most_common() for seq in freq_list: if len(seq[0]) == 33: PixEt_ID = seq[0][1:5] PixEt_ID_num = seq_to_bin_num(PixEt_ID) if 0 < PixEt_ID_num < 101: PixEt = ID_2_PixEt_dict[PixEt_ID_num] if PixEt not in Seq_dict: Seq_dict[PixEt] = seq[0][5:34] PixEt = 1 for row in range(0, 10): for i in range(0, 10): if PixEt in Seq_dict: current = Seq_dict[PixEt] pix[i, row] = trip_to_num[current[0:3]] pix[i + 10, row + 10] = trip_to_num[current[3:6]] pix[i + 20, row + 20] = trip_to_num[current[6:9]] pix[i + 10, row] = trip_to_num[current[9:12]] pix[i + 20, row + 10] = trip_to_num[current[12:15]] pix[i, row + 20] = trip_to_num[current[15:18]] pix[i + 20, row] = trip_to_num[current[18:21]] pix[i, row + 10] = trip_to_num[current[21:24]] pix[i + 10, row + 20] = trip_to_num[current[24:27]] PixEt += 1 """Output""" png.fromarray(np.asarray(im), 'L;8').save("%s/reconstructed_image.png" % Data_Path)
pix[i+48,row] = Sing_seq[current[6]] pix[i+48,row+28] = Sing_seq[current[7]] pix[i+8,row] = Sing_seq[current[8]] pix[i+8,row+28] = Sing_seq[current[9]] pix[i+24,row] = Sing_seq[current[10]] pix[i+24,row+28] = Sing_seq[current[11]] pix[i+40,row] = Sing_seq[current[12]] pix[i+40,row+28] = Sing_seq[current[13]] pix[i,row+14] = Sing_seq[current[14]] pix[i,row+42] = Sing_seq[current[15]] pix[i+16,row+14] = Sing_seq[current[16]] pix[i+16,row+42] = Sing_seq[current[17]] pix[i+32,row+14] = Sing_seq[current[18]] pix[i+32,row+42] = Sing_seq[current[19]] pix[i+48,row+14] = Sing_seq[current[20]] pix[i+48,row+42] = Sing_seq[current[21]] pix[i+8,row+14] = Sing_seq[current[22]] pix[i+8,row+42] = Sing_seq[current[23]] pix[i+24,row+14] = Sing_seq[current[24]] pix[i+24,row+42] = Sing_seq[current[25]] pix[i+40,row+14] = Sing_seq[current[26]] pix[i+40,row+42] = Sing_seq[current[27]] PixEt +=1 """Output""" png.fromarray(np.asarray(im, np.uint8),'L;4').save("%s/reconstructed_image.png" % Data_Path)
pix[i,row] = trip_to_num[current[0:3]] pix[i+12,row+13] = trip_to_num[current[3:6]] pix[i+24,row] = trip_to_num[current[6:9]] pix[i+4,row+13] = trip_to_num[current[9:12]] pix[i+16,row] = trip_to_num[current[12:15]] pix[i+28,row+13] = trip_to_num[current[15:18]] pix[i+8,row] = trip_to_num[current[18:21]] pix[i+20,row+13] = trip_to_num[current[21:24]] if PixEt in [1,2,3,4,9,10,11,12,17,18,19,20,25,26,27,28,33,34,35,36,41,42,43,44,49,50,51,52,57,58,59,60,65,66,67,68,73,74,75,76,81,82,83,84,89,90,91,92,97,98,99,100]: pix[i+32,row] = trip_to_num[current[24:27]] else: pix[i-4,row+13] = trip_to_num[current[24:27]] PixEt +=1 png.fromarray(np.asarray(im),'L;8').save("%s/reconstructed_image_frame%s.png" % (Data_Path,frame)) #Called_vs_Right_analysis workbook = xlsxwriter.Workbook('%s/Called_vs_Right.xlsx' % Data_Path) bold = workbook.add_format({'bold': True}) worksheet1 = workbook.add_worksheet('Frame1') worksheet1.write(0,0,'Pixet',bold) worksheet1.write(0,1,'Intended',bold) worksheet1.write(0,2,'Called',bold) worksheet1.write(0,3,'Correct?',bold) row = 1 col = 0 Seq_dict = {} for record in Frame_SPCR_dict['Frame1']:
perlin = [[[ noise.pnoise3(i / (jitter_factor * n_frames), j / num_neurons, k / num_layers, base=j, repeatx=int(1 / jitter_factor), octaves=2) * noise_scale for i in range(n_frames) ] for j in range(num_neurons)] for k in range(num_layers)] for i in range(n_frames): start = time.time() print('frame {}/{}'.format(i + 1, n_frames)) for j in range(len(perlin)): for k in range(2, num_layers): net.state_dict()['layers.{}.weight'.format( (k + 1) * 2)][0][j] = perlin[k][j][i] colors = run_net(net, resolution, int(aspect_ratio * resolution)) colors[0][0] = 1 colors[-1][-1] = 0 colors = (colors * 255).round().astype(np.uint8) png.fromarray(colors, 'RGB').save('frames/out{}.png'.format(i)) if plot: plt.clf() plt.imshow(colors) plt.pause(0.0001) print('Done in {}s'.format(round(time.time() - start, 1)))
freq_list = c.most_common() for seq in freq_list: if len(seq[0]) == 33: PixEt_ID = seq[0][1:5] PixEt_ID_num = seq_to_bin_num(PixEt_ID) if 0 < PixEt_ID_num < 101: PixEt = ID_2_PixEt_dict[PixEt_ID_num] if PixEt not in Seq_dict: Seq_dict[PixEt] = seq[0][5:34] PixEt = 1 for row in range(0,10): for i in range(0,10): if PixEt in Seq_dict: current = Seq_dict[PixEt] pix[i,row] = trip_to_num[current[0:3]] pix[i+10,row+10] = trip_to_num[current[3:6]] pix[i+20,row+20] = trip_to_num[current[6:9]] pix[i+10,row] = trip_to_num[current[9:12]] pix[i+20,row+10] = trip_to_num[current[12:15]] pix[i,row+20] = trip_to_num[current[15:18]] pix[i+20,row] = trip_to_num[current[18:21]] pix[i,row+10] = trip_to_num[current[21:24]] pix[i+10,row+20] = trip_to_num[current[24:27]] PixEt +=1 """Output""" png.fromarray(np.asarray(im),'L;8').save("%s/reconstructed_image.png" % Data_Path)
pix[i + 32, row] = Sing_seq[current[4]] pix[i + 32, row + 28] = Sing_seq[current[5]] pix[i + 48, row] = Sing_seq[current[6]] pix[i + 48, row + 28] = Sing_seq[current[7]] pix[i + 8, row] = Sing_seq[current[8]] pix[i + 8, row + 28] = Sing_seq[current[9]] pix[i + 24, row] = Sing_seq[current[10]] pix[i + 24, row + 28] = Sing_seq[current[11]] pix[i + 40, row] = Sing_seq[current[12]] pix[i + 40, row + 28] = Sing_seq[current[13]] pix[i, row + 14] = Sing_seq[current[14]] pix[i, row + 42] = Sing_seq[current[15]] pix[i + 16, row + 14] = Sing_seq[current[16]] pix[i + 16, row + 42] = Sing_seq[current[17]] pix[i + 32, row + 14] = Sing_seq[current[18]] pix[i + 32, row + 42] = Sing_seq[current[19]] pix[i + 48, row + 14] = Sing_seq[current[20]] pix[i + 48, row + 42] = Sing_seq[current[21]] pix[i + 8, row + 14] = Sing_seq[current[22]] pix[i + 8, row + 42] = Sing_seq[current[23]] pix[i + 24, row + 14] = Sing_seq[current[24]] pix[i + 24, row + 42] = Sing_seq[current[25]] pix[i + 40, row + 14] = Sing_seq[current[26]] pix[i + 40, row + 42] = Sing_seq[current[27]] PixEt += 1 """Output""" png.fromarray(np.asarray(im, np.uint8), 'L;4').save("%s/reconstructed_image.png" % Data_Path)
f.read(width * height * iMIN) # Create images from next iNUM records print('Creating %d images in %s ...' % (iNUM, outputdir)) for i in range(0, iNUM): # Read record and make it the right shape bytes = f.read(width * height) if len(bytes) != (width * height): print('\n File truncated!') break data = np.frombuffer(bytes, dtype='B', count=width * height) pixels = np.reshape(data, [height, width]) # Image name idx = i + iMIN fname = 'img%06d.png' % idx if idx < len(fnames): fname = fnames[idx] # Create image file png.fromarray(pixels, 'L').save(outputdir + '/' + fname) # Bookkeeping Ncreated += 1 if (Ncreated % 10) == 0: sys.stdout.write(' created %d/%d images\r' % (Ncreated, iNUM)) sys.stdout.flush() print('Created ' + str(Ncreated) + ' image files') print('Done')