Debian Codegraph @

docs/source/examples/imageData.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 """
5 Copyright © 2014 - 2021 German Neuroinformatics Node (G-Node)
6
7 All rights reserved.
8
9 Redistribution and use in source and binary forms, with or without
10 modification, are permitted under the terms of the BSD License. See
11 LICENSE file in the root of the Project.
12
13 Author: Jan Grewe <[email protected]>
14
15 See https://github.com/G-node/nix/wiki for more information.
16
17 We use the "Lenna" image in this tutorial.
18
19 "Lenna" by Original full portrait: "Playmate of the Month". Playboy
20 Magazine. November 1972, photographed by Dwight Hooker.This 512x512
21 electronic/mechanical scan of a section of the full portrait:
22 Alexander Sawchuk and two others[1] - The USC-SIPI image
23 database. Via Wikipedia -
24 http://en.wikipedia.org/wiki/File:Lenna.png#mediaviewer/File:Lenna.png
25
26 """
27
28 import nixio
29 import numpy as np
30 from PIL import Image as img
31
32 import docutils
33
34
35 def load_image():
36 image = img.open('lenna.png')
37 pix = np.array(image)
38 channels = list(image.mode)
39 return pix, channels
40
41
42 def plot_data(data_array):
43 img_data = np.zeros(data_array.shape)
44 data_array.read_direct(img_data)
45 img_data = np.array(img_data, dtype='uint8')
46 new_img = img.fromarray(img_data)
47 if not docutils.is_running_under_pytest():
48 new_img.show()
49
50
51 if __name__ == '__main__':
52 img_data, channels = load_image()
53 # create a new file overwriting any existing content
54 file_name = 'image_example.nix'
55 file = nixio.File.open(file_name, nixio.FileMode.Overwrite)
56
57 # create a 'Block' that represents a grouping object. Here, the recording session.
58 # it gets a name and a type
59 block = file.create_block("block name", "nix.session")
60
61 # create a 'DataArray' and store the image data
62 data = block.create_data_array("lenna", "nix.image.rgb", data=img_data)
63 # add descriptors for width, height and channel dimensions
64 data.append_sampled_dimension(1, label="height")
65 data.append_sampled_dimension(1, label="width")
66 data.append_set_dimension(labels=channels)
67
68 # let's plot the data from the stored information
69 plot_data(data)
70 file.close()