MCPcopy
hub / github.com/python-visualization/folium / image_to_url

Function image_to_url

folium/utilities.py:164–205  ·  view source on GitHub ↗

Infers the type of an image argument and transforms it into a URL. Parameters ---------- image: string or array-like object * If string is a path to an image file, its content will be converted and embedded in the output URL. * If string is a URL, it wi

(
    image: Any,
    colormap: Optional[Callable] = None,
    origin: str = "upper",
)

Source from the content-addressed store, hash-verified

162
163
164def image_to_url(
165 image: Any,
166 colormap: Optional[Callable] = None,
167 origin: str = "upper",
168) -> str:
169 """
170 Infers the type of an image argument and transforms it into a URL.
171
172 Parameters
173 ----------
174 image: string or array-like object
175 * If string is a path to an image file, its content will be converted and
176 embedded in the output URL.
177 * If string is a URL, it will be linked in the output URL.
178 * Otherwise a string will be assumed to be JSON and embedded in the
179 output URL.
180 * If array-like, it will be converted to PNG base64 string and embedded in the
181 output URL.
182 origin: ['upper' | 'lower'], optional, default 'upper'
183 Place the [0, 0] index of the array in the upper left or
184 lower left corner of the axes.
185 colormap: callable, used only for `mono` image.
186 Function of the form [x -> (r,g,b)] or [x -> (r,g,b,a)]
187 for transforming a mono image into RGB.
188 It must output iterables of length 3 or 4, with values between
189 0. and 1. You can use colormaps from `matplotlib.cm`.
190
191 """
192 if isinstance(image, str) and not _is_url(image):
193 fileformat = os.path.splitext(image)[-1][1:]
194 with open(image, "rb") as f:
195 img = f.read()
196 b64encoded = base64.b64encode(img).decode("utf-8")
197 url = f"data:image/{fileformat};base64,{b64encoded}"
198 elif "ndarray" in image.__class__.__name__:
199 img = write_png(image, origin=origin, colormap=colormap)
200 b64encoded = base64.b64encode(img).decode("utf-8")
201 url = f"data:image/png;base64,{b64encoded}"
202 else:
203 # Round-trip to ensure a nice formatted json.
204 url = json.loads(json.dumps(image))
205 return url.replace("\n", " ")
206
207
208def _is_url(url: str) -> bool:

Callers 2

__init__Method · 0.90
__init__Method · 0.90

Calls 1

_is_urlFunction · 0.85

Tested by

no test coverage detected