Images, hierarchical data, time
Image manipulation with scikit-image
Several image-processing libraries use numpy data structures underneath, e.g. Pillow
and skimage.io
. Let’s take a look at the latter.
from skimage import io # scikit-image is a collection of algorithms for image processing
= io.imread(fname="https://raw.githubusercontent.com/razoumov/publish/master/grids.png")
image type(image) # numpy array
# 1024^2 image, with three colour (RGB) channels image.shape
Let’s plot this image using matplotlib:
io.imshow(image)# io.show() # only if working in a terminal
# io.imsave("tmp.png", image)
Using numpy, you can easily manipulate pixels, e.g.
2] = 255 - image[:,:,2] image[:,:,
and then plot it again.
Hierarchical data formats
We already saw Python dictionaries. You can save them in a file using a variety of techniques. One of the most popular techniques, especially among web developers, is JSON (JavaScript Object Notation), as its internal mapping is similar to that of a Python dictionary, with key-value pairs. In the file all data are stored as human-readable text, including any non-ASCII (Unicode) characters.
import json
= {
x "name": "John",
"age": 30,
"married": True,
"children": ("Ann","Billy"),
"pets": None,
"cars": [
"model": "BMW 230", "mpg": 27.5},
{"model": "Ford Edge", "mpg": 24.1}
{
]
}len(x) # 6 key-value pairs
# here are the keys
x.keys()
= open("personal.json", "w")
filename = 2) # serialize `x` as a JSON-formatted stream to `filename`
json.dump(x, filename, indent # `indent` sets field offsets in the file (for human readability)
filename.close()
...
import json
= open("personal.json", "r")
filename = json.load(filename) # read into a new dictionary
data
filename.close()for k in data:
print(k, data[k])
If you want to read larger and/or binary data, there is BSON format. Going step further, there are popular scientific data formats such as NetCDF and HDF5 for storing large multi-dimensional arrays and/or large hierarchical datasets, but we won’t study them here.
Working with time
In its standard library Python has high-level functions to work with time and dates:
from time import *
0) # show the starting epoch on my system (typically 1970-Jan-01 on Unix-like systems)
gmtime(# number of seconds since then = current time
time() # convert that to human-readable time
ctime(time()) # same = current time
ctime()
= localtime() # convert current date/time to a structure
local
local.tm_year, local.tm_mon, local.tm_mday
local.tm_hour, local.tm_min, local.tm_sec# my time zone
local.tm_zone # Daylight Saving Time 1=on or 0=off local.tm_isdst
You can find many more examples here.