22image - Plot raster or EPS images.
33"""
44
5+ import warnings
56from collections .abc import Sequence
67from typing import Literal
78
89from pygmt ._typing import AnchorCode , PathLike
910from pygmt .alias import Alias , AliasSystem
1011from pygmt .clib import Session
11- from pygmt .helpers import build_arg_list , fmt_docstring , use_alias
12+ from pygmt .exceptions import GMTParameterError , GMTValueError
13+ from pygmt .helpers import build_arg_list , fmt_docstring
1214from pygmt .params import Axis , Box , Frame , Position
1315from pygmt .src ._common import _parse_position
1416
1517
1618@fmt_docstring
17- @use_alias (G = "bitcolor" )
1819def image (
1920 self ,
2021 imagefile : PathLike ,
@@ -26,6 +27,10 @@ def image(
2627 box : Box | bool = False ,
2728 monochrome : bool = False ,
2829 invert : bool = False ,
30+ bgcolor : str | None = None ,
31+ fgcolor : str | None = None ,
32+ transparent_color : str | None = None ,
33+ bitcolor : str | Sequence [str ] | None = None ,
2934 projection : str | None = None ,
3035 region : Sequence [float | str ] | str | None = None ,
3136 frame : Frame | Axis | Literal ["none" ] | str | Sequence [str ] | bool = False ,
@@ -54,10 +59,15 @@ def image(
5459
5560 Full GMT docs at :gmt-docs:`image.html`.
5661
57- $aliases
62+ **Aliases:**
63+
64+ .. hlist::
65+ :columns: 3
66+
5867 - B = frame
5968 - D = position, **+w**: width/height, **+r**: dpi, **+n**: replicate
6069 - F = box
70+ - G = **+b**: bgcolor, **+f**: fgcolor, **+t**: transparent_color, bitcolor
6171 - I = invert
6272 - J = projection
6373 - M = monochrome
@@ -74,7 +84,7 @@ def image(
7484 contain an appropriate BoundingBox. A raster file can have a depth of 1, 8, 24,
7585 or 32 bits and is read via GDAL.
7686 position
77- Position of the GMT logo on the plot. It can be specified in multiple ways:
87+ Position of the image on the plot. It can be specified in multiple ways:
7888
7989 - A :class:`pygmt.params.Position` object to fully control the reference point,
8090 anchor point, and offset.
@@ -106,14 +116,25 @@ def image(
106116 box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box appearance,
107117 pass a :class:`pygmt.params.Box` object to control style, fill, pen, and other
108118 box properties.
109- bitcolor : str or list
119+ bgcolor
120+ fgcolor
121+ For 1-bit images, set the background and foreground colors [Default is black and
122+ white, respectively]. Setting either to an empty string makes those pixels
123+ transparent. Cannot be both empty.
124+ transparent_color
125+ For color images, set a single color that should be made transparent.
126+ bitcolor
110127 [*color*][**+b**\|\ **f**\|\ **t**].
111- Change certain pixel values to another color or make them transparent.
112- For 1-bit images you can specify an alternate *color* for the
113- background (**+b**) or the foreground (**+f**) pixels, or give no color
114- to make those pixels transparent. Can be repeated with different
115- settings. Alternatively, for color images you can select a single
116- *color* that should be made transparent instead (**+t**).
128+ Change certain pixel values to another color or make them transparent. For 1-bit
129+ images you can specify an alternate *color* for the background (**+b**) or the
130+ foreground (**+f**) pixels, or give no color to make those pixels transparent.
131+ Alternatively, for color images you can select a single *color* that should be
132+ made transparent instead (**+t**). Pass a list to specify multiple settings.
133+
134+ .. deprecated:: 0.20.0
135+
136+ Use ``bgcolor``, ``fgcolor``, or ``transparent_color`` instead. Will be
137+ removed in 0.24.0.
117138 monochrome
118139 Convert color image to monochrome grayshades using the (television)
119140 YIQ-transformation.
@@ -142,6 +163,38 @@ def image(
142163 if width is None and height is not None :
143164 width = 0
144165
166+ # TODO(PyGMT>=0.24.0): Remove the deprecated "bitcolor" parameter.
167+ if bitcolor is not None :
168+ msg = (
169+ "The 'bitcolor' parameter has been deprecated since v0.20.0 and will be "
170+ "removed in v0.24.0. Use 'bgcolor', 'fgcolor' or 'transparent_color' "
171+ "instead."
172+ )
173+ warnings .warn (msg , category = FutureWarning , stacklevel = 2 )
174+ if any (v is not None for v in [bgcolor , fgcolor , transparent_color ]):
175+ raise GMTParameterError (
176+ conflicts_with = (
177+ "bitcolor" ,
178+ ["bgcolor" , "fgcolor" , "transparent_color" ],
179+ ),
180+ )
181+
182+ # 'bgcolor' and 'fgcolor' cannot both be empty.
183+ if bgcolor == "" and fgcolor == "" :
184+ _value = f"{ bgcolor = } , { fgcolor = } "
185+ raise GMTValueError (
186+ _value ,
187+ description = "bgcolor and fgcolor" ,
188+ reason = "'bgcolor' and 'fgcolor' cannot both be empty." ,
189+ )
190+ # GMT requires a color for the "+t" modifier.
191+ if transparent_color == "" :
192+ raise GMTValueError (
193+ transparent_color ,
194+ description = "value for 'transparent_color'" ,
195+ reason = "'transparent_color' cannot be empty." ,
196+ )
197+
145198 aliasdict = AliasSystem (
146199 D = [
147200 Alias (position , name = "position" ),
@@ -151,6 +204,13 @@ def image(
151204 Alias (dpi , name = "dpi" , prefix = "+r" ),
152205 ],
153206 F = Alias (box , name = "box" ),
207+ G = [
208+ Alias (bgcolor , name = "bgcolor" , suffix = "+b" ),
209+ Alias (fgcolor , name = "fgcolor" , suffix = "+f" ),
210+ Alias (transparent_color , name = "transparent_color" , suffix = "+t" ),
211+ ]
212+ if bitcolor is None
213+ else Alias (bitcolor , name = "bitcolor" ),
154214 M = Alias (monochrome , name = "monochrome" ),
155215 I = Alias (invert , name = "invert" ),
156216 ).add_common (
0 commit comments