-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1532 from danforthcenter/read-images-http
Read images over HTTP
- Loading branch information
Showing
6 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## Open an image from a URL | ||
|
||
Opens an image file stored at a URL. | ||
|
||
**plantcv.io.open_url**(*url*) | ||
|
||
**returns** img | ||
|
||
- **Parameters:** | ||
- url - URL of the image file to be opened | ||
- **Context:** | ||
- Used to open an image file stored at a URL | ||
- **Example use:** | ||
|
||
```python | ||
from plantcv import plantcv as pcv | ||
|
||
# Set global debug behavior to None (default), "print" (to file), | ||
# or "plot" (Jupyter Notebooks or X11) | ||
pcv.params.debug = "plot" | ||
|
||
# Open image from URL | ||
img = pcv.io.open_url(url="https://plantcv.org/s/plantcv-hyperspectral.png") | ||
|
||
``` | ||
|
||
**Image** | ||
|
||
<img title="PlantCV logo" alt="PlantCV logo" src="https://plantcv.org/s/plantcv-hyperspectral.png" width="300px"> | ||
|
||
**Source Code:** [Here](https://github.com/danforthcenter/plantcv/blob/main/plantcv/plantcv/io/open_url.py) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from plantcv.plantcv.io.read_dataset import read_dataset | ||
from plantcv.plantcv.io.random_subset import random_subset | ||
from plantcv.plantcv.io.open_url import open_url | ||
|
||
# add new functions to end of lists | ||
__all__ = ["read_dataset", "random_subset"] | ||
__all__ = ["read_dataset", "random_subset", "open_url"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Open an image from a URL.""" | ||
import imageio.v3 as iio | ||
import cv2 | ||
from plantcv.plantcv import fatal_error | ||
from plantcv.plantcv._debug import _debug | ||
|
||
|
||
def open_url(url): | ||
"""Open an image from a URL and return it as a numpy array. | ||
Parameters | ||
---------- | ||
url : str | ||
URL of the image to be opened. | ||
Returns | ||
------- | ||
numpy.ndarray | ||
Image data as a numpy array. | ||
""" | ||
# Read the image from the URL using imageio | ||
image = iio.imread(url) | ||
|
||
# Check if the image is grayscale or RGB | ||
if len(image.shape) not in [2, 3]: | ||
fatal_error("Image is not RGB or grayscale.") | ||
|
||
if image.shape[-1] == 3: | ||
# Convert the image to RGB format | ||
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) | ||
|
||
# Debugging visualization | ||
_debug(visual=image, filename="url_image.png") | ||
|
||
return image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Tests for the open_url function.""" | ||
import pytest | ||
from plantcv.plantcv.io import open_url | ||
|
||
|
||
def test_open_url(): | ||
"""PlantCV Test""" | ||
url = ("https://github.com/danforthcenter/plantcv-tutorial-simple-rgb-workflow/blob/" + | ||
"af312af00e21c84efe942132a1910359faadd49a/img/1_B73_sand_C_2023-04-14_10_19_07.jpg?raw=true") | ||
rgb_img = open_url(url=url) | ||
assert rgb_img.shape == (3456, 4608, 3) | ||
|
||
|
||
def test_open_url_unsupported(): | ||
"""PlantCV Test""" | ||
url = "https://upload.wikimedia.org/wikipedia/commons/2/2c/Rotating_earth_%28large%29.gif" | ||
with pytest.raises(RuntimeError): | ||
_ = open_url(url=url) |