Skip to content

Commit 9e989e7

Browse files
authored
Merge pull request #1532 from danforthcenter/read-images-http
Read images over HTTP
2 parents f25882c + e3aa323 commit 9e989e7

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed

docs/io_open_url.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Open an image from a URL
2+
3+
Opens an image file stored at a URL.
4+
5+
**plantcv.io.open_url**(*url*)
6+
7+
**returns** img
8+
9+
- **Parameters:**
10+
- url - URL of the image file to be opened
11+
- **Context:**
12+
- Used to open an image file stored at a URL
13+
- **Example use:**
14+
15+
```python
16+
from plantcv import plantcv as pcv
17+
18+
# Set global debug behavior to None (default), "print" (to file),
19+
# or "plot" (Jupyter Notebooks or X11)
20+
pcv.params.debug = "plot"
21+
22+
# Open image from URL
23+
img = pcv.io.open_url(url="https://plantcv.org/s/plantcv-hyperspectral.png")
24+
25+
```
26+
27+
**Image**
28+
29+
<img title="PlantCV logo" alt="PlantCV logo" src="https://plantcv.org/s/plantcv-hyperspectral.png" width="300px">
30+
31+
**Source Code:** [Here](https://github.com/danforthcenter/plantcv/blob/main/plantcv/plantcv/io/open_url.py)

docs/updating.md

+5
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,11 @@ pages for more details on the input and output variable types.
565565
* pre v3.0dev2: device, img_inv = **plantcv.invert**(*img, device, debug=None*)
566566
* post v3.0dev2: img_inv = **plantcv.invert**(*gray_img*)
567567

568+
#### plantcv.io.open_url
569+
570+
* pre v4.2.1: NA
571+
* post v4.2.1: img = **plantcv.io.open_url**(*url*)
572+
568573
#### plantcv.io.random_subset
569574

570575
* pre v3.14.0: NA

mkdocs.yml

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ nav:
8888
- 'Image Fusion': image_fusion.md
8989
- 'Invert': invert.md
9090
- 'I/O Functions':
91+
- 'Open Image from URL': io_open_url.md
9192
- 'Random Subset': io_random_subset.md
9293
- 'Read Dataset': io_read_dataset.md
9394
- 'Kmeans Clustering Classifier': kmeans_classifier.md

plantcv/plantcv/io/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from plantcv.plantcv.io.read_dataset import read_dataset
22
from plantcv.plantcv.io.random_subset import random_subset
3+
from plantcv.plantcv.io.open_url import open_url
34

45
# add new functions to end of lists
5-
__all__ = ["read_dataset", "random_subset"]
6+
__all__ = ["read_dataset", "random_subset", "open_url"]

plantcv/plantcv/io/open_url.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""Open an image from a URL."""
2+
import imageio.v3 as iio
3+
import cv2
4+
from plantcv.plantcv import fatal_error
5+
from plantcv.plantcv._debug import _debug
6+
7+
8+
def open_url(url):
9+
"""Open an image from a URL and return it as a numpy array.
10+
11+
Parameters
12+
----------
13+
url : str
14+
URL of the image to be opened.
15+
16+
Returns
17+
-------
18+
numpy.ndarray
19+
Image data as a numpy array.
20+
"""
21+
# Read the image from the URL using imageio
22+
image = iio.imread(url)
23+
24+
# Check if the image is grayscale or RGB
25+
if len(image.shape) not in [2, 3]:
26+
fatal_error("Image is not RGB or grayscale.")
27+
28+
if image.shape[-1] == 3:
29+
# Convert the image to RGB format
30+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
31+
32+
# Debugging visualization
33+
_debug(visual=image, filename="url_image.png")
34+
35+
return image

tests/plantcv/io/test_open_url.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""Tests for the open_url function."""
2+
import pytest
3+
from plantcv.plantcv.io import open_url
4+
5+
6+
def test_open_url():
7+
"""PlantCV Test"""
8+
url = ("https://github.com/danforthcenter/plantcv-tutorial-simple-rgb-workflow/blob/" +
9+
"af312af00e21c84efe942132a1910359faadd49a/img/1_B73_sand_C_2023-04-14_10_19_07.jpg?raw=true")
10+
rgb_img = open_url(url=url)
11+
assert rgb_img.shape == (3456, 4608, 3)
12+
13+
14+
def test_open_url_unsupported():
15+
"""PlantCV Test"""
16+
url = "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif"
17+
with pytest.raises(RuntimeError):
18+
_ = open_url(url=url)

0 commit comments

Comments
 (0)