-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add loading image from filename. #2778
Add loading image from filename. #2778
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly looks good (the benefit of tiny PRs!) though the constructor needs to be sorted out before I can sign off
@@ -31,8 +31,38 @@ or pass in ``credentials`` and ``project`` explicitly. | |||
>>> client = vision.Client(project='my-project', credentials=creds) | |||
|
|||
|
|||
Creating an :class:`~google.cloud.vision.image.Image` object. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Creating an :class:`~google.cloud.vision.image.Image` object. | ||
------------------------------------------------------------- | ||
|
||
The :class:`~google.cloud.vision.image.Image` object is used to load your image |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
data from sources such as a Google Cloud Storage URI, raw bytes, or a file. | ||
|
||
|
||
**From a Google Cloud Storage URI** |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -214,7 +244,7 @@ in an image. | |||
|
|||
|
|||
Image Properties | |||
~~~~~~~~~~~~~~~~ | |||
---------------- |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
self._source = source_uri | ||
else: | ||
self._content = _bytes_to_unicode(b64encode(_to_bytes(content))) | ||
if filename: |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
with open(filename, 'rb') as file_obj: | ||
content = file_obj.read() | ||
|
||
if content: |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
:type source_uri: str | ||
:param source_uri: Google Cloud Storage URI of image. | ||
|
||
:type client: :class:`~google.cloud.vision.client.Client` | ||
:param client: Instance of Vision client. | ||
""" | ||
|
||
def __init__(self, client, content=None, source_uri=None): | ||
def __init__(self, client, content=None, filename=None, source_uri=None): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -7,6 +7,7 @@ localdeps = | |||
pip install --quiet --upgrade {toxinidir}/../core | |||
deps = | |||
{toxinidir}/../core | |||
mock |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -36,26 +36,26 @@ def _make_one(self, *args, **kw): | |||
def test_image_source_type_content(self): | |||
image = self._make_one(CLIENT_MOCK, content=IMAGE_CONTENT) | |||
|
|||
_AS_DICT = { | |||
_as_dict = { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
def test_image_source_type_google_cloud_storage(self): | ||
image = self._make_one(CLIENT_MOCK, source_uri=IMAGE_SOURCE) | ||
|
||
_AS_DICT = { | ||
_as_dict = { |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
:type source_uri: str | ||
:param source_uri: Google Cloud Storage URI of image. | ||
|
||
:type client: :class:`~google.cloud.vision.client.Client` | ||
:param client: Instance of Vision client. | ||
""" | ||
|
||
def __init__(self, client, content=None, source_uri=None): | ||
def __init__(self, client, content=None, filename=None, source_uri=None): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
def __init__(self, client, content=None, source_uri=None): | ||
def __init__(self, client, content=None, filename=None, source_uri=None): | ||
sources = set(source for source in (content, filename, source_uri) | ||
if source is not None) |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
if len(sources) != 1: | ||
raise ValueError( | ||
'Specify exactly one of \'content\', \'filename\', or' | ||
'\'source_uri\'.') |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
self._content = _bytes_to_unicode(b64encode(_to_bytes(content))) | ||
if filename is not None: | ||
with open(filename, 'rb') as file_obj: | ||
content = file_obj.read() |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -7,6 +7,7 @@ localdeps = | |||
pip install --quiet --upgrade {toxinidir}/../core | |||
deps = | |||
{toxinidir}/../core | |||
mock |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Thanks @dhermes! I cancelled travis and rerun after squashing. |
b77ed78
to
0bc1c4b
Compare
…e-support Add loading image from filename.
Add loading image from filename.
Towards #2753
Adding support to pass in a filename when instantiating an
Image
.