This repository has been archived by the owner on Jan 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_array_as_image.py
86 lines (75 loc) · 3.24 KB
/
save_array_as_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
##############################################################################################
# This file is deprecated because Python 2.x is deprecated #
# A Python 3.x version of this file can be found at: #
# #
# https://github.com/Guymer/PyGuymer3/blob/master/save_array_as_image.py #
##############################################################################################
def save_array_as_image(img0, fname, form = "png", scale = False, pc_bot = 0.0, pc_top = 0.0, ct = u"fire"):
"""
Saves an array as an image with optional scaling and/or colour mapping.
Currently only "png" and "ppm" formats are available.
img0: a 2D NumPy array of any type with shape (ny,nx)
fname: output file name
form: output file format
scale: does the input need scaling?
pc_bot: the percentage to clip off the bottom of the histogram (if scaling
is requested)
pc_top: the percentage to clip off the top of the histogram (if scaling is
requested)
ct: the colour table to apply (if 3-channel output is requested)
"""
# Load modules ...
import json
import numpy
import os
# Load sub-functions ...
from .save_array_as_PPM import save_array_as_PPM
from .save_array_as_PNG import save_array_as_PNG
# Load colour tables ...
colour_tables = json.load(
open(
os.path.join(
os.path.dirname(__file__),
u"data",
u"json",
u"colour_tables.json"
),
"rt"
)
)
# Create uint8 image that will be passed to the external function ...
img2 = numpy.empty((img0.shape[0], img0.shape[1], 3), dtype = numpy.uint8)
# Check if scaling is required ...
if scale:
# Check input values ...
if pc_bot < 0.0:
raise Exception("pc_bot < 0.0")
if pc_top < 0.0:
raise Exception("pc_top < 0.0")
if (pc_bot + pc_top) > 100.0:
raise Exception("(pc_bot + pc_top) > 100.0")
# Create copy and find the percentiles ...
img1 = img0.astype(numpy.float64)
p_lo = numpy.percentile(img1, pc_bot)
p_hi = numpy.percentile(img1, 100.0 - pc_top)
# Scale the image, clip scale image, convert scaled image to correct
# type, clean up ...
img1 = 255.0 * (img1 - p_lo) / (p_hi - p_lo)
numpy.place(img1, img1 > 255.0, 255.0)
numpy.place(img1, img1 < 0.0, 0.0)
for ix in xrange(img0.shape[1]):
for iy in xrange(img0.shape[0]):
img2[iy, ix, :] = colour_tables[ct][img1[iy, ix].astype(numpy.uint8)][:]
else:
# Convert image to correct type ...
for ix in xrange(img0.shape[1]):
for iy in xrange(img0.shape[0]):
img2[iy, ix, :] = colour_tables[ct][img0[iy, ix].astype(numpy.uint8)][:]
# Save image ...
if form == "png":
save_array_as_PNG(img2, fname, ftype_req = 4)
elif form == "ppm":
save_array_as_PPM(img2, fname)
else:
raise Exception("unknown format was requested")