Skip to content

Commit

Permalink
Added support for existing Image objects.
Browse files Browse the repository at this point in the history
With corresponding tests.

Resolves #1!
  • Loading branch information
obskyr committed Dec 27, 2018
1 parent 14aaad6 commit 8b07c38
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion colorgram/colorgram.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def hsl(self):
return self._hsl

def extract(f, number_of_colors):
image = Image.open(f)
image = f if isinstance(f, Image.Image) else Image.open(f)
if image.mode not in ('RGB', 'RGBA', 'RGBa'):
image = image.convert('RGB')

Expand Down
2 changes: 1 addition & 1 deletion readme.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Example
``colorgram.extract(image, number_of_colors)``
''''''''''''''''''''''''''''''''''''''''''''''
Extract colors from an image. ``image`` may be either a path to a file, or a file-like object. The function will return a list of ``number_of_colors`` ``Color`` objects.
Extract colors from an image. ``image`` may be either a path to a file, a file-like object, or a Pillow ``Image`` object. The function will return a list of ``number_of_colors`` ``Color`` objects.

``colorgram.Color``
'''''''''''''''''''
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from setuptools import setup

VERSION = '1.1.0'
VERSION = '1.2.0'

REQUIREMENTS = [
"pillow >= 3.3.1"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_access.py → tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# -*- coding: utf-8 -*-

import colorgram
from PIL import Image

def test_extract_from_file():
colorgram.extract('data/test.png', 1)

def test_extract_from_image_object():
image = Image.open('data/test.png')
colorgram.extract(image, 1)

def test_color_access():
color = colorgram.Color(255, 151, 210, 0.15)
Expand Down

0 comments on commit 8b07c38

Please sign in to comment.