55from collections .abc import Sequence
66from typing import Literal
77
8- from pygmt ._typing import PathLike
8+ from pygmt ._typing import AnchorCode , PathLike
99from pygmt .alias import Alias , AliasSystem
1010from pygmt .clib import Session
1111from pygmt .helpers import build_arg_list , fmt_docstring , use_alias
12- from pygmt .params import Box
12+ from pygmt .params import Box , Position
13+ from pygmt .src ._common import _parse_position
1314
1415
1516@fmt_docstring
16- @use_alias (D = "position" , G = "bitcolor" )
17+ @use_alias (G = "bitcolor" )
1718def image ( # noqa: PLR0913
1819 self ,
1920 imagefile : PathLike ,
21+ position : Position | Sequence [float | str ] | AnchorCode | None = None ,
22+ width : float | str | None = None ,
23+ height : float | str | None = None ,
24+ dpi : float | str | None = None ,
25+ replicate : int | Sequence [int ] | None = None ,
2026 box : Box | bool = False ,
2127 monochrome : bool = False ,
2228 invert : bool = False ,
@@ -33,10 +39,10 @@ def image( # noqa: PLR0913
3339 r"""
3440 Plot raster or EPS images.
3541
36- Reads Encapsulated PostScript (EPS) or raster image files and plots them. The
37- image can be scaled arbitrarily, and 1-bit raster images can be:
42+ Reads an Encapsulated PostScript file or a raster image file and plot it on a map.
43+ The image can be scaled arbitrarily, and 1-bit raster images can be:
3844
39- - inverted, i.e., black pixels (on) becomes white (off) and vice versa.
45+ - inverted, i.e., black pixels (on) become white (off) and vice versa.
4046 - colorized, by assigning different foreground and background colors.
4147 - made transparent where either the back- or foreground is painted.
4248
@@ -50,6 +56,7 @@ def image( # noqa: PLR0913
5056
5157 $aliases
5258 - B = frame
59+ - D = position, **+w**: width/height, **+r**: dpi, **+n**: replicate
5360 - F = box
5461 - I = invert
5562 - J = projection
@@ -66,11 +73,34 @@ def image( # noqa: PLR0913
6673 An Encapsulated PostScript (EPS) file or a raster image file. An EPS file must
6774 contain an appropriate BoundingBox. A raster file can have a depth of 1, 8, 24,
6875 or 32 bits and is read via GDAL.
69- position : str
70- [**g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**]\ *refpoint*\ **+r**\ *dpi*\
71- **+w**\ [**-**]\ *width*\ [/*height*]\ [**+j**\ *justify*]\
72- [**+n**\ *nx*\ [/*ny*]]\ [**+o**\ *dx*\ [/*dy*]].
73- Set reference point on the map for the image.
76+ position
77+ Position of the GMT logo on the plot. It can be specified in multiple ways:
78+
79+ - A :class:`pygmt.params.Position` object to fully control the reference point,
80+ anchor point, and offset.
81+ - A sequence of two values representing the x and y coordinates in plot
82+ coordinates, e.g., ``(1, 2)`` or ``("1c", "2c")``.
83+ - A :doc:`2-character justification code </techref/justification_codes>` for a
84+ position inside the plot, e.g., ``"TL"`` for Top Left corner inside the plot.
85+
86+ If not specified, defaults to the bottom-left corner of the plot (position
87+ ``(0, 0)`` with anchor ``"BL"``).
88+ width
89+ height
90+ Width (and height) of the image in plot coordinates (inches, cm, etc.). If
91+ ``height`` (or ``width``) is set to 0, then the original aspect ratio of the
92+ image is maintained. If ``width`` (or ``height``) is negative, the absolute
93+ value is used to interpolate image to the device resolution using the PostScript
94+ image operator. If neither dimensions nor ``dpi`` are set then revert to the
95+ default dpi [:gmt-term:`GMT_GRAPHICS_DPU`].
96+ dpi
97+ Set the dpi of the image in dots per inch, or append **c** to indicate this is
98+ dots per cm.
99+ replicate
100+ *nx* or (*nx*, *ny*).
101+ Replicate the (scaled) image *nx* times in the horizontal direction, and *ny*
102+ times in the vertical direction. If a single integer *nx* is given, *ny* = *nx*.
103+ [Default is (1, 1)].
74104 box
75105 Draw a background box behind the image. If set to ``True``, a simple rectangular
76106 box is drawn using :gmt-term:`MAP_FRAME_PEN`. To customize the box appearance,
@@ -104,7 +134,24 @@ def image( # noqa: PLR0913
104134 """
105135 self ._activate_figure ()
106136
137+ position = _parse_position (
138+ position ,
139+ kwdict = {"width" : width , "height" : height , "dpi" : dpi , "replicate" : replicate },
140+ default = Position ((0 , 0 ), cstype = "plotcoords" ), # Default to (0,0) in plotcoords
141+ )
142+
143+ # width is required when only height is given.
144+ if width is None and height is not None :
145+ width = 0
146+
107147 aliasdict = AliasSystem (
148+ D = [
149+ Alias (position , name = "position" ),
150+ Alias (width , name = "width" , prefix = "+w" ), # +wwidth/height
151+ Alias (height , name = "height" , prefix = "/" ),
152+ Alias (replicate , name = "replicate" , prefix = "+n" , sep = "/" , size = 2 ),
153+ Alias (dpi , name = "dpi" , prefix = "+r" ),
154+ ],
108155 F = Alias (box , name = "box" ),
109156 M = Alias (monochrome , name = "monochrome" ),
110157 I = Alias (invert , name = "invert" ),
0 commit comments