| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
"""Copyright © 2014 German Neuroinformatics Node (G-Node) |
| 5 |
|
| 6 |
All rights reserved. |
| 7 |
|
| 8 |
Redistribution and use in source and binary forms, with or without |
| 9 |
modification, are permitted under the terms of the BSD License. See |
| 10 |
LICENSE file in the root of the Project. |
| 11 |
|
| 12 |
Author: Jan Grewe <[email protected]> |
| 13 |
|
| 14 |
See https://github.com/G-node/nix/wiki for more information. |
| 15 |
|
| 16 |
We use the "Lenna" image in this tutorial. |
| 17 |
|
| 18 |
"Lenna" by Original full portrait: "Playmate of the Month". Playboy |
| 19 |
Magazine. November 1972, photographed by Dwight Hooker.This 512x512 |
| 20 |
electronic/mechanical scan of a section of the full portrait: |
| 21 |
Alexander Sawchuk and two others[1] - The USC-SIPI image |
| 22 |
database. Via Wikipedia - |
| 23 |
http://en.wikipedia.org/wiki/File:Lenna.png#mediaviewer/File:Lenna.png |
| 24 |
|
| 25 |
""" |
| 26 |
import numpy as np |
| 27 |
import nixio as nix |
| 28 |
import matplotlib.pyplot as plt |
| 29 |
|
| 30 |
import docutils |
| 31 |
|
| 32 |
from PIL import Image as img |
| 33 |
|
| 34 |
|
| 35 |
def print_metadata_table(section, ax): |
| 36 |
columns = ['Name', 'Value'] |
| 37 |
cell_text = [] |
| 38 |
for p in section.items(): |
| 39 |
for i, v in enumerate(p[1].values): |
| 40 |
value = str(v) |
| 41 |
if len(value) > 30: |
| 42 |
value = value[:30] + '...' |
| 43 |
if i == 0: |
| 44 |
row_data = [p[0], value] |
| 45 |
else: |
| 46 |
row_data = [p[0], value] |
| 47 |
|
| 48 |
cell_text.append(row_data) |
| 49 |
if len(cell_text) > 0: |
| 50 |
nrows, ncols = len(cell_text)+1, len(columns) |
| 51 |
ax.axis('off') |
| 52 |
the_table = ax.table(cellText=cell_text, |
| 53 |
colLabels=columns, |
| 54 |
loc='center') |
| 55 |
for cell in the_table.get_children(): |
| 56 |
cell.set_height(.075) |
| 57 |
cell.set_fontsize(16) |
| 58 |
|
| 59 |
|
| 60 |
def load_image(): |
| 61 |
image = img.open('lenna.png') |
| 62 |
pix = np.array(image) |
| 63 |
channels = list(image.mode) |
| 64 |
return pix, channels |
| 65 |
|
| 66 |
|
| 67 |
def plot_data(data_array): |
| 68 |
img_data = np.zeros(data_array.shape) |
| 69 |
data_array.read_direct(img_data) |
| 70 |
img_data = np.array(img_data, dtype='uint8') |
| 71 |
new_img = img.fromarray(img_data) |
| 72 |
|
| 73 |
fig = plt.figure() |
| 74 |
img_axis = fig.add_subplot(121) |
| 75 |
img_axis.imshow(new_img) |
| 76 |
|
| 77 |
info_axis = fig.add_subplot(122) |
| 78 |
print_metadata_table(data_array.metadata, info_axis) |
| 79 |
fig.subplots_adjust(left=0.075, right=0.975, bottom=0.075, top=0.975) |
| 80 |
if docutils.is_running_under_pytest(): |
| 81 |
plt.close() |
| 82 |
else: |
| 83 |
fig.savefig('image_with_metadata.png') |
| 84 |
fig.show() |
| 85 |
|
| 86 |
|
| 87 |
def add_image_information(nix_file): |
| 88 |
section = nix_file.create_section('Image metadata', 'image_source') |
| 89 |
section['Original name'] = 'Lenna' |
| 90 |
section['Journal'] = 'Playboy Magazine' |
| 91 |
section['Year'] = 1972 |
| 92 |
section['Month'] = 'November' |
| 93 |
section['Author'] = 'Dwight Hooker' |
| 94 |
section['Source'] = 'http://en.wikipedia.org/wiki/File:Lenna.png#mediaviewer/File:Lenna.png' |
| 95 |
section['Comment'] = '512x512 electronic/mechanical scan of a section of the full portrait: Alexander Sawchuk and two others[1] - The USC-SIPI image database.' |
| 96 |
section['Model'] = 'Lena Soederberg' |
| 97 |
return section |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == '__main__': |
| 101 |
img_data, channels = load_image() |
| 102 |
|
| 103 |
file_name = 'image_with_source_example.h5' |
| 104 |
file = nix.File.open(file_name, nix.FileMode.Overwrite) |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
block = file.create_block("block name", "nix.session") |
| 109 |
|
| 110 |
|
| 111 |
data = block.create_data_array("lenna", "nix.image.rgb", data=img_data) |
| 112 |
|
| 113 |
height_dim = data.append_sampled_dimension(1) |
| 114 |
height_dim.label = "height" |
| 115 |
width_dim = data.append_sampled_dimension(1) |
| 116 |
width_dim.label = "width" |
| 117 |
color_dim = data.append_set_dimension() |
| 118 |
color_dim.labels = channels |
| 119 |
section = add_image_information(file) |
| 120 |
data.metadata = section |
| 121 |
|
| 122 |
plot_data(data) |
| 123 |
file.close() |