Skip to content
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

Merged

Conversation

daspecster
Copy link
Contributor

Towards #2753

Adding support to pass in a filename when instantiating an Image.

@daspecster daspecster added the api: vision Issues related to the Cloud Vision API. label Nov 30, 2016
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Nov 30, 2016
@daspecster daspecster mentioned this pull request Nov 30, 2016
10 tasks
Copy link
Contributor

@dhermes dhermes left a 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.

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.

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.

This comment was marked as spam.

@@ -214,7 +244,7 @@ in an image.


Image Properties
~~~~~~~~~~~~~~~~
----------------

This comment was marked as spam.

This comment was marked as spam.

self._source = source_uri
else:
self._content = _bytes_to_unicode(b64encode(_to_bytes(content)))
if filename:

This comment was marked as spam.

with open(filename, 'rb') as file_obj:
content = file_obj.read()

if content:

This comment was marked as spam.

: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.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

@@ -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.

This comment was marked as spam.

This comment was marked as spam.

@@ -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.


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.

: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.

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.

if len(sources) != 1:
raise ValueError(
'Specify exactly one of \'content\', \'filename\', or'
'\'source_uri\'.')

This comment was marked as spam.

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.

This comment was marked as spam.

@@ -7,6 +7,7 @@ localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
{toxinidir}/../core
mock

This comment was marked as spam.

@daspecster
Copy link
Contributor Author

Thanks @dhermes! I cancelled travis and rerun after squashing.

@daspecster daspecster force-pushed the vision-add-filename-support branch from b77ed78 to 0bc1c4b Compare December 1, 2016 20:00
@daspecster daspecster merged commit e96c06a into googleapis:master Dec 1, 2016
@daspecster daspecster deleted the vision-add-filename-support branch December 1, 2016 20:23
richkadel pushed a commit to richkadel/google-cloud-python that referenced this pull request May 6, 2017
…e-support

Add loading image from filename.
parthea pushed a commit that referenced this pull request Oct 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: vision Issues related to the Cloud Vision API. cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants