Skip to content
Commits on Source (4)
trollimage/version.py export-subst
linters:
flake8:
fixer: true
fixers:
enable: true
python: 3
config: setup.cfg
## Version 1.6.3 (2018/12/20)
### Pull Requests Merged
#### Bugs fixed
* [PR 38](https://github.com/pytroll/trollimage/pull/38) - Fix casting and scaling float arrays in finalize
In this release 1 pull request was closed.
## Version 1.6.2 (2018/12/20)
### Pull Requests Merged
#### Bugs fixed
* [PR 37](https://github.com/pytroll/trollimage/pull/37) - Fix and cleanup alpha and fill value handling in XRImage finalize
* [PR 35](https://github.com/pytroll/trollimage/pull/35) - Fix xrimage alpha creation in finalize
In this release 2 pull requests were closed.
## Version 1.6.1 (2018/12/19)
### Pull Requests Merged
#### Bugs fixed
* [PR 34](https://github.com/pytroll/trollimage/pull/34) - Fix XRImage's finalize method when input data are integers
In this release 1 pull request was closed.
## Version 1.6.0 (2018/12/18)
### Issues Closed
* [Issue 30](https://github.com/pytroll/trollimage/issues/30) - ReadTheDoc page builds not up to date ([PR 32](https://github.com/pytroll/trollimage/pull/32))
* [Issue 21](https://github.com/pytroll/trollimage/issues/21) - Add 'convert' method to XRImage
In this release 2 issues were closed.
### Pull Requests Merged
#### Bugs fixed
* [PR 33](https://github.com/pytroll/trollimage/pull/33) - Fix crude stretch not calculating min/max per-band
#### Features added
* [PR 28](https://github.com/pytroll/trollimage/pull/28) - Add convert method of XRImage
In this release 2 pull requests were closed.
## Previous Versions
See changelog.rst for previous release notes.
include LICENSE.txt
include README.rst
include versioneer.py
include trollimage/version.py
# Releasing trollimage
1. checkout master
2. pull from repo
3. run the unittests
4. run `loghub` and update the `CHANGELOG.md` file:
```
loghub pytroll/trollimage -u <username> -st v0.8.0 -plg bug "Bugs fixed" -plg enhancement "Features added" -plg documentation "Documentation changes" -plg backwards-incompatibility "Backwards incompatible changes"
```
Don't forget to commit!
5. Create a tag with the new version number, starting with a 'v', eg:
```
git tag -a v0.22.45 -m "Version 0.22.45"
```
See [semver.org](http://semver.org/) on how to write a version number.
6. push changes to github `git push --follow-tags`
7. Verify travis tests passed and deployed sdist and wheel to PyPI
trollimage (1.5.7-1) unstable; urgency=medium
trollimage (1.6.3-1) unstable; urgency=medium
* Initial version (Closes: #916550)
-- Antonio Valentino <antonio.valentino@tiscali.it> Sat, 15 Dec 2018 21:02:06 +0000
-- Antonio Valentino <antonio.valentino@tiscali.it> Tue, 25 Dec 2018 11:57:08 +0000
......@@ -8,7 +8,7 @@ Build-Depends: debhelper (>= 11),
dh-python,
python3-all,
python3-dask,
python3-numpy,
python3-numpy (>= 1:1.13),
python3-pil,
python3-rasterio,
python3-setuptools,
......@@ -21,7 +21,7 @@ Homepage: https://github.com/pytroll/trollimage
Package: python3-trollimage
Architecture: all
Depends: python3-numpy,
Depends: python3-numpy (>= 1:1.13),
python3-pil,
python3-six,
${python3:Depends},
......
......@@ -7,6 +7,10 @@ Files: *
Copyright: 2009-2018 Martin Raspaud
License: GPL-3+
Files: versioneer.py
Copyright: 2018, Brian Warner
License: CC0-1.0
Files: debian/*
Copyright: 2018 Antonio Valentino <antonio.valentino@tiscali.it>
License: GPL-3+
......@@ -27,3 +31,14 @@ License: GPL-3+
.
On Debian systems, the complete text of the GNU General
Public License version 3 can be found in "/usr/share/common-licenses/GPL-3".
License: CC0-1.0
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
.
On Debian systems, the complete text of the CC0 1.0 Universal license can be
found in ‘/usr/share/common-licenses/CC0-1.0’.
===========
Colorspaces
-----------
===========
.. automodule:: trollimage.colorspaces
:members:
......
=============
Simple images
-------------
=============
.. automodule:: trollimage.image
:members:
......
......@@ -23,7 +23,6 @@ Contents:
colormap
Indices and tables
==================
......
Simple images
-------------
=============
XArray images
=============
.. automodule:: trollimage.xrimage
.. autoclass:: trollimage.xrimage.XRImage
:members:
:undoc-members:
:special-members:
:exclude-members: __dict__,__weakref__
......@@ -2,4 +2,20 @@
requires=numpy python-pillow
release=1
[bdist_wheel]
universal=1
[flake8]
max-line-length = 120
[versioneer]
VCS = git
style = pep440
versionfile_source = trollimage/version.py
versionfile_build =
tag_prefix = v
[coverage:run]
omit =
trollimage/version.py
versioneer.py
......@@ -26,12 +26,11 @@
"""
from setuptools import setup
import imp
version = imp.load_source('trollimage.version', 'trollimage/version.py')
import versioneer
setup(name="trollimage",
version=version.__version__,
version=versioneer.get_version(),
cmdclass=versioneer.get_cmdclass(),
description='Pytroll imaging library',
author='Martin Raspaud',
author_email='martin.raspaud@smhi.se',
......@@ -45,7 +44,7 @@ setup(name="trollimage",
url="https://github.com/pytroll/trollimage",
packages=['trollimage'],
zip_safe=False,
install_requires=['numpy >=1.6', 'pillow', 'six'],
install_requires=['numpy >=1.13', 'pillow', 'six'],
extras_require={
'geotiff': ['rasterio'],
'xarray': ['xarray', 'dask[array]'],
......
......@@ -23,4 +23,6 @@
"""The trollimage package
"""
from trollimage.version import __version__ # noqa
from .version import get_versions
__version__ = get_versions()['version']
del get_versions
This diff is collapsed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2013, 2014 Martin Raspaud
# This file helps to compute a version number in source trees obtained from
# git-archive tarball (such as those provided by githubs download-from-tag
# feature). Distribution tarballs (built by setup.py sdist) and build
# directories (produced by setup.py build) will contain a much shorter file
# that just contains the computed version number.
# Author(s):
# This file is released into the public domain. Generated by
# versioneer-0.18 (https://github.com/warner/python-versioneer)
# Martin Raspaud <martin.raspaud@smhi.se>
"""Git implementation of _version.py."""
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
import errno
import os
import re
import subprocess
import sys
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
def get_keywords():
"""Get the keywords needed to look up the version information."""
# these strings will be replaced by git during git-archive.
# setup.py/versioneer.py will grep for the variable names, so they must
# each be defined on a line of their own. _version.py will just call
# get_keywords().
git_refnames = " (HEAD -> master, tag: v1.6.3)"
git_full = "69942415f7bfbd9160189534ca60782eaa2e4b59"
git_date = "2018-12-20 15:10:36 -0600"
keywords = {"refnames": git_refnames, "full": git_full, "date": git_date}
return keywords
"""Version file.
class VersioneerConfig:
"""Container for Versioneer configuration parameters."""
def get_config():
"""Create, populate and return the VersioneerConfig() object."""
# these strings are filled in when 'setup.py versioneer' creates
# _version.py
cfg = VersioneerConfig()
cfg.VCS = "git"
cfg.style = "pep440"
cfg.tag_prefix = "v"
cfg.parentdir_prefix = "None"
cfg.versionfile_source = "trollimage/version.py"
cfg.verbose = False
return cfg
class NotThisMethod(Exception):
"""Exception raised if a method is not valid for the current scenario."""
LONG_VERSION_PY = {}
HANDLERS = {}
def register_vcs_handler(vcs, method): # decorator
"""Decorator to mark a method as the handler for a particular VCS."""
def decorate(f):
"""Store f in HANDLERS[vcs][method]."""
if vcs not in HANDLERS:
HANDLERS[vcs] = {}
HANDLERS[vcs][method] = f
return f
return decorate
def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False,
env=None):
"""Call the given command(s)."""
assert isinstance(commands, list)
p = None
for c in commands:
try:
dispcmd = str([c] + args)
# remember shell=False, so use git.cmd on windows, not just git
p = subprocess.Popen([c] + args, cwd=cwd, env=env,
stdout=subprocess.PIPE,
stderr=(subprocess.PIPE if hide_stderr
else None))
break
except EnvironmentError:
e = sys.exc_info()[1]
if e.errno == errno.ENOENT:
continue
if verbose:
print("unable to run %s" % dispcmd)
print(e)
return None, None
else:
if verbose:
print("unable to find command, tried %s" % (commands,))
return None, None
stdout = p.communicate()[0].strip()
if sys.version_info[0] >= 3:
stdout = stdout.decode()
if p.returncode != 0:
if verbose:
print("unable to run %s (error)" % dispcmd)
print("stdout was %s" % stdout)
return None, p.returncode
return stdout, p.returncode
def versions_from_parentdir(parentdir_prefix, root, verbose):
"""Try to determine the version from the parent directory name.
Source tarballs conventionally unpack into a directory that includes both
the project name and a version string. We will also support searching up
two directory levels for an appropriately named parent directory
"""
rootdirs = []
for i in range(3):
dirname = os.path.basename(root)
if dirname.startswith(parentdir_prefix):
return {"version": dirname[len(parentdir_prefix):],
"full-revisionid": None,
"dirty": False, "error": None, "date": None}
else:
rootdirs.append(root)
root = os.path.dirname(root) # up a level
if verbose:
print("Tried directories %s but none started with prefix %s" %
(str(rootdirs), parentdir_prefix))
raise NotThisMethod("rootdir doesn't start with parentdir_prefix")
@register_vcs_handler("git", "get_keywords")
def git_get_keywords(versionfile_abs):
"""Extract version information from the given file."""
# the code embedded in _version.py can just fetch the value of these
# keywords. When used from setup.py, we don't want to import _version.py,
# so we do it with a regexp instead. This function is not used from
# _version.py.
keywords = {}
try:
f = open(versionfile_abs, "r")
for line in f.readlines():
if line.strip().startswith("git_refnames ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["refnames"] = mo.group(1)
if line.strip().startswith("git_full ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["full"] = mo.group(1)
if line.strip().startswith("git_date ="):
mo = re.search(r'=\s*"(.*)"', line)
if mo:
keywords["date"] = mo.group(1)
f.close()
except EnvironmentError:
pass
return keywords
@register_vcs_handler("git", "keywords")
def git_versions_from_keywords(keywords, tag_prefix, verbose):
"""Get version information from git keywords."""
if not keywords:
raise NotThisMethod("no keywords at all, weird")
date = keywords.get("date")
if date is not None:
# git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant
# datestamp. However we prefer "%ci" (which expands to an "ISO-8601
# -like" string, which we must then edit to make compliant), because
# it's been around since git-1.5.3, and it's too difficult to
# discover which version we're using, or to work around using an
# older one.
date = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
refnames = keywords["refnames"].strip()
if refnames.startswith("$Format"):
if verbose:
print("keywords are unexpanded, not using")
raise NotThisMethod("unexpanded keywords, not a git-archive tarball")
refs = set([r.strip() for r in refnames.strip("()").split(",")])
# starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of
# just "foo-1.0". If we see a "tag: " prefix, prefer those.
TAG = "tag: "
tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)])
if not tags:
# Either we're using git < 1.8.3, or there really are no tags. We use
# a heuristic: assume all version tags have a digit. The old git %d
# expansion behaves like git log --decorate=short and strips out the
# refs/heads/ and refs/tags/ prefixes that would let us distinguish
# between branches and tags. By ignoring refnames without digits, we
# filter out many common branch names like "release" and
# "stabilization", as well as "HEAD" and "master".
tags = set([r for r in refs if re.search(r'\d', r)])
if verbose:
print("discarding '%s', no digits" % ",".join(refs - tags))
if verbose:
print("likely tags: %s" % ",".join(sorted(tags)))
for ref in sorted(tags):
# sorting will prefer e.g. "2.0" over "2.0rc1"
if ref.startswith(tag_prefix):
r = ref[len(tag_prefix):]
if verbose:
print("picking %s" % r)
return {"version": r,
"full-revisionid": keywords["full"].strip(),
"dirty": False, "error": None,
"date": date}
# no suitable tags, so version is "0+unknown", but full hex is still there
if verbose:
print("no suitable tags, using unknown + full revision id")
return {"version": "0+unknown",
"full-revisionid": keywords["full"].strip(),
"dirty": False, "error": "no suitable tags", "date": None}
@register_vcs_handler("git", "pieces_from_vcs")
def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command):
"""Get version from 'git describe' in the root of the source tree.
This only gets called if the git-archive 'subst' keywords were *not*
expanded, and _version.py hasn't already been rewritten with a short
version string, meaning we're inside a checked out source tree.
"""
GITS = ["git"]
if sys.platform == "win32":
GITS = ["git.cmd", "git.exe"]
out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root,
hide_stderr=True)
if rc != 0:
if verbose:
print("Directory %s not under git control" % root)
raise NotThisMethod("'git rev-parse --git-dir' returned error")
# if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty]
# if there isn't one, this yields HEX[-dirty] (no NUM)
describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty",
"--always", "--long",
"--match", "%s*" % tag_prefix],
cwd=root)
# --long was added in git-1.5.5
if describe_out is None:
raise NotThisMethod("'git describe' failed")
describe_out = describe_out.strip()
full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root)
if full_out is None:
raise NotThisMethod("'git rev-parse' failed")
full_out = full_out.strip()
pieces = {}
pieces["long"] = full_out
pieces["short"] = full_out[:7] # maybe improved later
pieces["error"] = None
# parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty]
# TAG might have hyphens.
git_describe = describe_out
# look for -dirty suffix
dirty = git_describe.endswith("-dirty")
pieces["dirty"] = dirty
if dirty:
git_describe = git_describe[:git_describe.rindex("-dirty")]
# now we have TAG-NUM-gHEX or HEX
if "-" in git_describe:
# TAG-NUM-gHEX
mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe)
if not mo:
# unparseable. Maybe git-describe is misbehaving?
pieces["error"] = ("unable to parse git-describe output: '%s'"
% describe_out)
return pieces
# tag
full_tag = mo.group(1)
if not full_tag.startswith(tag_prefix):
if verbose:
fmt = "tag '%s' doesn't start with prefix '%s'"
print(fmt % (full_tag, tag_prefix))
pieces["error"] = ("tag '%s' doesn't start with prefix '%s'"
% (full_tag, tag_prefix))
return pieces
pieces["closest-tag"] = full_tag[len(tag_prefix):]
# distance: number of commits since tag
pieces["distance"] = int(mo.group(2))
# commit: short hex revision ID
pieces["short"] = mo.group(3)
else:
# HEX: no tags
pieces["closest-tag"] = None
count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"],
cwd=root)
pieces["distance"] = int(count_out) # total number of commits
# commit date: see ISO-8601 comment in git_versions_from_keywords()
date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"],
cwd=root)[0].strip()
pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1)
return pieces
def plus_or_dot(pieces):
"""Return a + if we don't already have one, else return a ."""
if "+" in pieces.get("closest-tag", ""):
return "."
return "+"
def render_pep440(pieces):
"""Build up version string, with post-release "local version identifier".
Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you
get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty
Exceptions:
1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty]
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"] or pieces["dirty"]:
rendered += plus_or_dot(pieces)
rendered += "%d.g%s" % (pieces["distance"], pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
else:
# exception #1
rendered = "0+untagged.%d.g%s" % (pieces["distance"],
pieces["short"])
if pieces["dirty"]:
rendered += ".dirty"
return rendered
def render_pep440_pre(pieces):
"""TAG[.post.devDISTANCE] -- No -dirty.
Exceptions:
1: no tags. 0.post.devDISTANCE
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"]:
rendered += ".post.dev%d" % pieces["distance"]
else:
# exception #1
rendered = "0.post.dev%d" % pieces["distance"]
return rendered
def render_pep440_post(pieces):
"""TAG[.postDISTANCE[.dev0]+gHEX] .
The ".dev0" means dirty. Note that .dev0 sorts backwards
(a dirty tree will appear "older" than the corresponding clean one),
but you shouldn't be releasing software with -dirty anyways.
Exceptions:
1: no tags. 0.postDISTANCE[.dev0]
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"] or pieces["dirty"]:
rendered += ".post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
rendered += plus_or_dot(pieces)
rendered += "g%s" % pieces["short"]
else:
# exception #1
rendered = "0.post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
rendered += "+g%s" % pieces["short"]
return rendered
def render_pep440_old(pieces):
"""TAG[.postDISTANCE[.dev0]] .
The ".dev0" means dirty.
Eexceptions:
1: no tags. 0.postDISTANCE[.dev0]
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"] or pieces["dirty"]:
rendered += ".post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
else:
# exception #1
rendered = "0.post%d" % pieces["distance"]
if pieces["dirty"]:
rendered += ".dev0"
return rendered
def render_git_describe(pieces):
"""TAG[-DISTANCE-gHEX][-dirty].
Like 'git describe --tags --dirty --always'.
Exceptions:
1: no tags. HEX[-dirty] (note: no 'g' prefix)
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
if pieces["distance"]:
rendered += "-%d-g%s" % (pieces["distance"], pieces["short"])
else:
# exception #1
rendered = pieces["short"]
if pieces["dirty"]:
rendered += "-dirty"
return rendered
def render_git_describe_long(pieces):
"""TAG-DISTANCE-gHEX[-dirty].
Like 'git describe --tags --dirty --always -long'.
The distance/hash is unconditional.
Exceptions:
1: no tags. HEX[-dirty] (note: no 'g' prefix)
"""
if pieces["closest-tag"]:
rendered = pieces["closest-tag"]
rendered += "-%d-g%s" % (pieces["distance"], pieces["short"])
else:
# exception #1
rendered = pieces["short"]
if pieces["dirty"]:
rendered += "-dirty"
return rendered
def render(pieces, style):
"""Render the given version pieces into the requested style."""
if pieces["error"]:
return {"version": "unknown",
"full-revisionid": pieces.get("long"),
"dirty": None,
"error": pieces["error"],
"date": None}
if not style or style == "default":
style = "pep440" # the default
if style == "pep440":
rendered = render_pep440(pieces)
elif style == "pep440-pre":
rendered = render_pep440_pre(pieces)
elif style == "pep440-post":
rendered = render_pep440_post(pieces)
elif style == "pep440-old":
rendered = render_pep440_old(pieces)
elif style == "git-describe":
rendered = render_git_describe(pieces)
elif style == "git-describe-long":
rendered = render_git_describe_long(pieces)
else:
raise ValueError("unknown style '%s'" % style)
return {"version": rendered, "full-revisionid": pieces["long"],
"dirty": pieces["dirty"], "error": None,
"date": pieces.get("date")}
def get_versions():
"""Get version information or return default if unable to do so."""
# I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have
# __file__, we can work backwards from there to the root. Some
# py2exe/bbfreeze/non-CPython implementations don't do __file__, in which
# case we can only use expanded keywords.
cfg = get_config()
verbose = cfg.verbose
try:
return git_versions_from_keywords(get_keywords(), cfg.tag_prefix,
verbose)
except NotThisMethod:
pass
try:
root = os.path.realpath(__file__)
# versionfile_source is the relative path from the top of the source
# tree (where the .git directory might live) to this file. Invert
# this to find the root from __file__.
for i in cfg.versionfile_source.split('/'):
root = os.path.dirname(root)
except NameError:
return {"version": "0+unknown", "full-revisionid": None,
"dirty": None,
"error": "unable to find root of source tree",
"date": None}
try:
pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose)
return render(pieces, cfg.style)
except NotThisMethod:
pass
try:
if cfg.parentdir_prefix:
return versions_from_parentdir(cfg.parentdir_prefix, root, verbose)
except NotThisMethod:
pass
__version__ = "v1.5.7"
return {"version": "0+unknown", "full-revisionid": None,
"dirty": None,
"error": "unable to compute version", "date": None}
......@@ -164,17 +164,16 @@ def color_interp(data):
class XRImage(object):
"""Image class using xarray as internal storage."""
"""Image class using an :class:`xarray.DataArray` as internal storage."""
def __init__(self, data):
"""Initialize the image."""
"""Initialize the image with a :class:`~xarray.DataArray`."""
data = self._correct_dims(data)
# 'data' is an XArray, get the data from it as a dask array
if not isinstance(data.data, da.Array):
logger.debug("Convert image data to dask array")
data.data = da.from_array(data.data, chunks=(data.sizes['bands'],
4096, 4096))
data.data = da.from_array(data.data, chunks=(data.sizes['bands'], 4096, 4096))
self.data = data
self.height, self.width = self.data.sizes['y'], self.data.sizes['x']
......@@ -378,21 +377,160 @@ class XRImage(object):
return meta
def fill_or_alpha(self, data, fill_value=None):
"""Fill the data with fill_value, or create an alpha channels."""
if fill_value is None and not self.mode.endswith("A"):
def _create_alpha(self, data, fill_value=None):
"""Create an alpha band DataArray object.
If `fill_value` is provided and input data is an integer type
then it is used to determine invalid "null" pixels instead of
xarray's `isnull` and `notnull` methods.
The returned array is 1 where data is valid, 0 where invalid.
"""
not_alpha = [b for b in data.coords['bands'].values if b != 'A']
null_mask = data.sel(bands=not_alpha)
if np.issubdtype(data.dtype, np.integer) and fill_value is not None:
null_mask = null_mask != fill_value
else:
null_mask = null_mask.notnull()
# if any of the bands are valid, we don't want transparency
null_mask = data.sel(bands=not_alpha).notnull().any(dim='bands')
null_mask = null_mask.any(dim='bands')
null_mask = null_mask.expand_dims('bands')
null_mask['bands'] = ['A']
# match data dtype
return null_mask
data = xr.concat([data, null_mask.astype(data.dtype)],
dim="bands")
elif fill_value is not None:
data = data.fillna(fill_value)
def _add_alpha(self, data, alpha=None):
"""Create an alpha channel and concatenate it to the provided data.
If ``data`` is an integer type then the alpha band will be scaled
to use the smallest (min) value as fully transparent and the largest
(max) value as fully opaque. For float types the alpha band spans
0 to 1.
"""
null_mask = alpha if alpha is not None else self._create_alpha(data)
# if we are using integer data, then alpha needs to be min-int to max-int
# otherwise for floats we want 0 to 1
if np.issubdtype(data.dtype, np.integer):
# xarray sometimes upcasts this calculation, so cast again
null_mask = self._scale_to_dtype(null_mask, data.dtype).astype(data.dtype)
data = xr.concat([data, null_mask], dim="bands")
return data
def _scale_to_dtype(self, data, dtype):
"""Scale provided data to dtype range assuming a 0-1 range.
Float input data is assumed to be normalized to a 0 to 1 range.
Integer input data is not scaled, only clipped. A float output
type is not scaled since both outputs and inputs are assumed to
be in the 0-1 range already.
"""
if np.issubdtype(dtype, np.integer):
if np.issubdtype(data, np.integer):
# preserve integer data type
data = data.clip(np.iinfo(dtype).min, np.iinfo(dtype).max)
else:
# scale float data (assumed to be 0 to 1) to full integer space
dinfo = np.iinfo(dtype)
data = data.clip(0, 1) * (dinfo.max - dinfo.min) + dinfo.min
data = data.round()
return data
def _check_modes(self, modes):
"""Check that the image is in one of the given *modes*, raise an exception otherwise."""
if not isinstance(modes, (tuple, list, set)):
modes = [modes]
if self.mode not in modes:
raise ValueError("Image not in suitable mode, expected: %s, got: %s" % (modes, self.mode))
def _from_p(self, mode):
"""Convert the image from P or PA to RGB or RGBA."""
self._check_modes(("P", "PA"))
if self.mode.endswith("A"):
alpha = self.data.sel(bands=["A"]).data
mode = mode + "A" if not mode.endswith("A") else mode
else:
alpha = None
if not self.palette:
raise RuntimeError("Can't convert palettized image, missing palette.")
pal = np.array(self.palette)
pal = da.from_array(pal, chunks=pal.shape)
flat_indexes = self.data.data[0].ravel().astype('int64')
new_shape = (3,) + self.data.shape[1:3]
new_data = pal[flat_indexes].reshape(new_shape)
coords = dict(self.data.coords)
coords["bands"] = list(mode)
if alpha is not None:
new_arr = da.concatenate((new_data, alpha), axis=0)
data = xr.DataArray(new_arr, coords=coords, attrs=self.data.attrs, dims=self.data.dims)
else:
data = xr.DataArray(new_data, coords=coords, attrs=self.data.attrs, dims=self.data.dims)
return data
def _l2rgb(self, mode):
"""Convert from L (black and white) to RGB.
"""
self._check_modes(("L", "LA"))
bands = ["L"] * 3
if mode[-1] == "A":
bands.append("A")
data = self.data.sel(bands=bands)
data["bands"] = list(mode)
return data
def convert(self, mode):
if mode == self.mode:
return self.__class__(self.data)
if mode not in ["P", "PA", "L", "LA", "RGB", "RGBA"]:
raise ValueError("Mode %s not recognized." % (mode))
if mode == self.mode + "A":
data = self._add_alpha(self.data).data
coords = dict(self.data.coords)
coords["bands"] = list(mode)
data = xr.DataArray(data, coords=coords, attrs=self.data.attrs, dims=self.data.dims)
new_img = XRImage(data)
elif mode + "A" == self.mode:
# Remove the alpha band from our current image
no_alpha = self.data.sel(bands=[b for b in self.data.coords["bands"].data if b != "A"]).data
coords = dict(self.data.coords)
coords["bands"] = list(mode)
data = xr.DataArray(no_alpha, coords=coords, attrs=self.data.attrs, dims=self.data.dims)
new_img = XRImage(data)
elif mode.endswith("A") and not self.mode.endswith("A"):
img = self.convert(self.mode + "A")
new_img = img.convert(mode)
elif self.mode.endswith("A") and not mode.endswith("A"):
img = self.convert(self.mode[:-1])
new_img = img.convert(mode)
else:
cases = {
"P": {"RGB": self._from_p},
"PA": {"RGBA": self._from_p},
"L": {"RGB": self._l2rgb},
"LA": {"RGBA": self._l2rgb}
}
try:
data = cases[self.mode][mode](mode)
new_img = XRImage(data)
except KeyError:
raise ValueError("Conversion from %s to %s not implemented !"
% (self.mode, mode))
if self.mode.startswith('P') and new_img.mode.startswith('P'):
# need to copy the palette
new_img.palette = self.palette
return new_img
def _finalize(self, fill_value=None, dtype=np.uint8):
"""Wrapper around 'finalize' method for backwards compatibility."""
import warnings
......@@ -401,32 +539,51 @@ class XRImage(object):
return self.finalize(fill_value, dtype)
def finalize(self, fill_value=None, dtype=np.uint8):
"""Finalize the image.
"""Finalize the image to be written to an output file.
This adds an alpha band or fills data with a fill_value (if specified).
It also scales float data to the output range of the data type (0-255
for uint8, default). For integer input data this method assumes the
data is already scaled to the proper desired range. It will still fill
in invalid values and add an alpha band if needed. Integer input
data's fill value is determined by a special ``_FillValue`` attribute
in the ``DataArray`` ``.attrs`` dictionary.
This sets the channels in unsigned 8bit format ([0,255] range)
(if the *dtype* doesn't say otherwise).
"""
# if self.mode == "P":
# self.convert("RGB")
# if self.mode == "PA":
# self.convert("RGBA")
#
if self.mode == "P":
return self.convert("RGB").finalize(fill_value=fill_value, dtype=dtype)
if self.mode == "PA":
return self.convert("RGBA").finalize(fill_value=fill_value, dtype=dtype)
if np.issubdtype(dtype, np.floating) and fill_value is None:
logger.warning("Image with floats cannot be transparent, so "
"setting fill_value to 0")
fill_value = 0
final_data = self.fill_or_alpha(self.data, fill_value)
if np.issubdtype(dtype, np.integer):
final_data = final_data.clip(0, 1) * np.iinfo(dtype).max
final_data = final_data.round().astype(dtype)
final_data = self.data
# if the data are integers then this fill value will be used to check for invalid values
ifill = final_data.attrs.get('_FillValue') if np.issubdtype(final_data, np.integer) else None
if fill_value is None and not self.mode.endswith('A'):
# We don't have a fill value or an alpha, let's add an alpha
alpha = self._create_alpha(final_data, fill_value=ifill)
final_data = self._scale_to_dtype(final_data, dtype).astype(dtype)
final_data = self._add_alpha(final_data, alpha=alpha)
else:
final_data = final_data.astype(dtype)
# scale float data to the proper dtype
# this method doesn't cast yet so that we can keep track of NULL values
final_data = self._scale_to_dtype(final_data, dtype)
# Add fill_value after all other calculations have been done to
# make sure it is not scaled for the data type
if ifill is not None and fill_value is not None:
# cast fill value to output type so we don't change data type
fill_value = dtype(fill_value)
# integer fields have special fill values
final_data = final_data.where(final_data != ifill, dtype(fill_value))
elif fill_value is not None:
final_data = final_data.fillna(dtype(fill_value))
final_data = final_data.astype(dtype)
final_data.attrs = self.data.attrs
return final_data, ''.join(final_data['bands'].values)
def pil_image(self, fill_value=None):
......@@ -546,9 +703,11 @@ class XRImage(object):
the [0,1] range.
"""
if min_stretch is None:
min_stretch = self.data.min()
non_band_dims = tuple(x for x in self.data.dims if x != 'bands')
min_stretch = self.data.min(dim=non_band_dims)
if max_stretch is None:
max_stretch = self.data.max()
non_band_dims = tuple(x for x in self.data.dims if x != 'bands')
max_stretch = self.data.max(dim=non_band_dims)
if isinstance(min_stretch, (list, tuple)):
min_stretch = self.xrify_tuples(min_stretch)
......@@ -696,8 +855,12 @@ class XRImage(object):
img.channels[i].mask)
def colorize(self, colormap):
"""Colorize the current image using
*colormap*. Works only on"L" or "LA" images.
"""Colorize the current image using `colormap`.
.. note::
Works only on "L" or "LA" images.
"""
if self.mode not in ("L", "LA"):
......@@ -710,16 +873,14 @@ class XRImage(object):
l_data = self.data.sel(bands=['L'])
# TODO: dask-ify colorize
def _colorize(colormap, l_data):
channels = colormap.colorize(l_data.data)
def _colorize(l_data, colormap):
# 'l_data' is (1, rows, cols)
# 'channels' will be a list of 3 (RGB) or 4 (RGBA) arrays
channels = colormap.colorize(l_data)
return np.concatenate(channels, axis=0)
# TODO: xarray-ify colorize
# delayed = dask.delayed(colormap.colorize)(l_data.data)
delayed = dask.delayed(_colorize)(colormap, l_data)
shape = (3, l_data.sizes['y'], l_data.sizes['x'])
new_data = da.from_delayed(delayed, shape=shape, dtype=np.float64)
new_data = l_data.data.map_blocks(_colorize, colormap,
chunks=(3,) + l_data.data.chunks[1:], dtype=np.float64)
if alpha is not None:
new_data = da.concatenate([new_data, alpha.data], axis=0)
......@@ -732,12 +893,15 @@ class XRImage(object):
coords['bands'] = list(mode)
attrs = self.data.attrs
dims = self.data.dims
self.data = xr.DataArray(new_data, coords=coords, attrs=attrs,
dims=dims)
self.data = xr.DataArray(new_data, coords=coords, attrs=attrs, dims=dims)
def palettize(self, colormap):
"""Palettize the current image using
*colormap*. Works only on"L" or "LA" images.
"""Palettize the current image using `colormap`.
.. note::
Works only on "L" or "LA" images.
"""
if self.mode not in ("L", "LA"):
......@@ -746,31 +910,23 @@ class XRImage(object):
l_data = self.data.sel(bands=['L'])
def _palettize(data):
arr, palette = colormap.palettize(data.reshape(data.shape[1:]))
new_shape = (1, arr.shape[0], arr.shape[1])
arr = arr.reshape(new_shape)
return arr, palette
delayed = dask.delayed(_palettize)(l_data.data)
new_data, palette = delayed[0], delayed[1]
new_data = da.from_delayed(new_data, shape=l_data.shape,
dtype=l_data.dtype)
# XXX: Can we complete this method without computing the data?
new_data, self.palette = da.compute(new_data, palette)
new_data = da.from_array(new_data,
chunks=self.data.data.chunks)
# returns data and palette, only need data
return colormap.palettize(data)[0]
new_data = l_data.data.map_blocks(_palettize, dtype=l_data.dtype)
self.palette = tuple(colormap.colors)
if self.mode == "L":
mode = "P"
else:
mode = "PA"
new_data = da.concatenate([new_data, self.data.sel(bands=['A'])], axis=0)
self.data.data = new_data
self.data.coords['bands'] = list(mode)
def blend(self, other):
"""Alpha blend *other* on top of the current image.
"""
"""Alpha blend *other* on top of the current image."""
raise NotImplementedError("This method has not be implemented for "
"xarray support.")
......
This diff is collapsed.