Skip to content

Commit

Permalink
Fix flake8 issues
Browse files Browse the repository at this point in the history
  • Loading branch information
djhoese committed Nov 25, 2023
1 parent 5c2c7fb commit ba55be6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
16 changes: 6 additions & 10 deletions doc/examples/utilities-examples.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
"""Example showing importing colormaps from on-disk files."""
from trollimage import utilities as tu


#
# Examples: importing colormaps
#
filename='setvak.rgb'
# Examples: importing colormaps
filename = 'setvak.rgb'
my_cmap = tu.cmap_from_text(filename)
print(my_cmap.colors)
my_cmap_norm = tu.cmap_from_text(filename, norm=True)
print(my_cmap_norm.colors)
my_cmap_transp = tu.cmap_from_text(filename,norm=True, transparency=True)
my_cmap_transp = tu.cmap_from_text(filename, norm=True, transparency=True)
print(my_cmap_transp.colors)
filename='hrv.rgb'
my_cmap_hex = tu.cmap_from_text(filename,hex=True)
filename = 'hrv.rgb'
my_cmap_hex = tu.cmap_from_text(filename, hex=True)
print(my_cmap_hex.colors)

#
# Example: converting PIL to trollimage.Image
#
image = "pifn.png"
timage = tu.pilimage2trollimage(image)
83 changes: 43 additions & 40 deletions trollimage/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,102 +7,105 @@
from trollimage.image import Image
from PIL import Image as Pimage


def _hex_to_rgb(value):
'''
_hex_to_rgb converts a string of 3 hex color values
into a tuple of decimal values
'''
"""Convert a string of 3 hex color values into a tuple of decimal values."""
value = value.lstrip('#')
dec = int(value, 16)
return dec
return dec

Check warning on line 15 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L15

Added line #L15 was not covered by tests


def _text_to_rgb(value, norm=False, cat=1, tot=1, offset=0.5, hex=False):

Check warning on line 18 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L18

Added line #L18 was not covered by tests
"""Take text line and convert to RGB color.
def _text_to_rgb(value,norm=False,cat=1, tot=1,offset=0.5,hex=False):
'''
_text_to_rgb takes as input a string composed by 3 values in the range [0,255]
This takes as input a string composed by 3 values in the range [0,255]
and returns a tuple of integers. If the parameters cat and tot are given,
the function generates a transparency value for this color and returns a tuple
of length 4.
tot is the total number of colors in the colormap
cat is the index of the current colour in the colormap
if norm is set to True, the input values are normalized between 0 and 1.
'''
"""
tokens = value.split()
if hex:
for i in range(len(tokens)):
tokens[i] = _hex_to_rgb(tokens[i])
transparency = float(cat)/float(tot)+offset
for i in range(len(tokens)):
tokens[i] = _hex_to_rgb(tokens[i])
transparency = float(cat) / float(tot) + offset

Check warning on line 33 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L31-L33

Added lines #L31 - L33 were not covered by tests
if transparency > 1.0:
transparency = 1.0
if norm:
return (float(tokens[0])/255.0, float(tokens[1])/255.0, float(tokens[2])/255.0, transparency)
else:
return (float(tokens[0]) / 255.0, float(tokens[1]) / 255.0, float(tokens[2]) / 255.0, transparency)

Check warning on line 37 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L37

Added line #L37 was not covered by tests
else:
return (int(tokens[0]), int(tokens[1]), int(tokens[2]), int(round(transparency * 255.0)))


def _make_cmap(colors, position=None, bit=False):
'''
_make_cmap takes a list of tuples which contain RGB values. The RGB
"""Convert list of tuples into Colormap object.
This takes a list of tuples which contain RGB values. The RGB
values may either be in 8-bit [0 to 255] (in which bit must be set to
True when called) or arithmetic [0 to 1] (default). _make_cmap returns
a cmap with equally spaced colors.
Arrange your tuples so that the first color is the lowest value for the
colorbar and the last is the highest.
position contains values from 0 to 1 to dictate the location of each color.
'''
bit_rgb = np.linspace(0,1,256)
if position == None:
position = np.linspace(0,1,len(colors))
else:
if len(position) != len(colors):
sys.exit("position length must be the same as colors")
elif position[0] != 0 or position[-1] != 1:
sys.exit("position must start with 0 and end with 1")
"""
if position is None:
position = np.linspace(0, 1, len(colors))
if len(position) != len(colors):
sys.exit("position length must be the same as colors")
elif position[0] != 0 or position[-1] != 1:
sys.exit("position must start with 0 and end with 1")

Check warning on line 58 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L53-L58

Added lines #L53 - L58 were not covered by tests
palette = [(i, (float(r), float(g), float(b), float(a))) for
i, (r, g, b, a) in enumerate(colors)]
i, (r, g, b, a) in enumerate(colors)]
cmap = Colormap(*palette)
return cmap


def cmap_from_text(filename, norm=False, transparency=False, hex=False):
'''
cmap_from_text takes as input a file that contains a colormap in text format
"""Convert text file colormap to Colormap object.
This takes as input a file that contains a colormap in text format
composed by lines with 3 values in the range [0,255] or [00,FF]
and returns a tuple of integers. If the parameters cat and tot are given,
the function generates a transparency value for this color and returns a tuple
of length 4.
tot is the total number of colors in the colormap
cat is the index of the current colour in the colormap
if norm is set to True, the input values are normalized between 0 and 1.
'''
lines = [line.rstrip('\n') for line in open(filename)]
_colors=[]
"""
lines = [line.rstrip('\n') for line in open(filename)]
_colors = []

Check warning on line 78 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L77-L78

Added lines #L77 - L78 were not covered by tests
_tot = len(lines)
_index = 1
for i in lines:
if transparency:
_colors.append(_text_to_rgb(i,norm=norm,cat=_index,tot=_tot,hex=hex))
_colors.append(_text_to_rgb(i, norm=norm, cat=_index, tot=_tot, hex=hex))

Check warning on line 83 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L83

Added line #L83 was not covered by tests
else:
_colors.append(_text_to_rgb(i,norm=norm,hex=hex))
_colors.append(_text_to_rgb(i, norm=norm, hex=hex))

Check warning on line 85 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L85

Added line #L85 was not covered by tests
_index = _index + 1
return _make_cmap(_colors)


def _image2array(filepath):
'''
"""Extract individual R, G, and B channels from on-disk image file.
Utility function that converts an image file in 3 np arrays
that can be fed into geo_image.GeoImage in order to generate
a PyTROLL GeoImage object.
'''
"""
im = Pimage.open(filepath).convert('RGB')
(width, height) = im.size
_r = np.array(list(im.getdata(0)))/255.0
_g = np.array(list(im.getdata(1)))/255.0
_b = np.array(list(im.getdata(2)))/255.0
_r = np.array(list(im.getdata(0))) / 255.0
_g = np.array(list(im.getdata(1))) / 255.0
_b = np.array(list(im.getdata(2))) / 255.0

Check warning on line 101 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L99-L101

Added lines #L99 - L101 were not covered by tests
_r = _r.reshape((height, width))
_g = _g.reshape((height, width))
_b = _b.reshape((height, width))
return _r, _g, _b


def pilimage2trollimage(pimage):
(r,g,b) = _image2array(pimage)
return Image((r,g,b), mode="RGB")
"""Convert PIL Image to trollimage Image."""
(r, g, b) = _image2array(pimage)
return Image((r, g, b), mode="RGB")

Check warning on line 111 in trollimage/utilities.py

View check run for this annotation

Codecov / codecov/patch

trollimage/utilities.py#L110-L111

Added lines #L110 - L111 were not covered by tests

0 comments on commit ba55be6

Please sign in to comment.