Skip to content

Commit

Permalink
add dither option for image processing (esphome#1317)
Browse files Browse the repository at this point in the history
* [Image] add dither option for image processing

* fix import order

* revert import location
and hardcode two dither method

* fix format
  • Loading branch information
zhujunsan authored Nov 1, 2020
1 parent 3fcdaae commit 31c6043
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
10 changes: 6 additions & 4 deletions esphome/components/image/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from esphome.components import display, font
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_FILE, CONF_ID, CONF_TYPE, CONF_RESIZE
from esphome.const import CONF_FILE, CONF_ID, CONF_TYPE, CONF_RESIZE, CONF_DITHER
from esphome.core import CORE, HexInt

_LOGGER = logging.getLogger(__name__)

DEPENDENCIES = ['display']
MULTI_CONF = True


ImageType = display.display_ns.enum('ImageType')
IMAGE_TYPE = {
'BINARY': ImageType.IMAGE_TYPE_BINARY,
Expand All @@ -28,6 +28,7 @@
cv.Required(CONF_FILE): cv.file_,
cv.Optional(CONF_RESIZE): cv.dimensions,
cv.Optional(CONF_TYPE, default='BINARY'): cv.enum(IMAGE_TYPE, upper=True),
cv.Optional(CONF_DITHER, default='NONE'): cv.one_of("NONE", "FLOYDSTEINBERG", upper=True),
cv.GenerateID(CONF_RAW_DATA_ID): cv.declare_id(cg.uint8),
})

Expand All @@ -53,8 +54,9 @@ def to_code(config):
_LOGGER.warning("The image you requested is very big. Please consider using"
" the resize parameter.")

dither = Image.NONE if config[CONF_DITHER] == 'NONE' else Image.FLOYDSTEINBERG
if config[CONF_TYPE] == 'GRAYSCALE':
image = image.convert('L', dither=Image.NONE)
image = image.convert('L', dither=dither)
pixels = list(image.getdata())
data = [0 for _ in range(height * width)]
pos = 0
Expand All @@ -76,7 +78,7 @@ def to_code(config):
pos += 1

elif config[CONF_TYPE] == 'BINARY':
image = image.convert('1', dither=Image.NONE)
image = image.convert('1', dither=dither)
width8 = ((width + 7) // 8) * 8
data = [0 for _ in range(height * width8 // 8)]
for y in range(height):
Expand Down
1 change: 1 addition & 0 deletions esphome/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
CONF_DISCOVERY_PREFIX = 'discovery_prefix'
CONF_DISCOVERY_RETAIN = 'discovery_retain'
CONF_DISTANCE = 'distance'
CONF_DITHER = 'dither'
CONF_DIV_RATIO = 'div_ratio'
CONF_DNS1 = 'dns1'
CONF_DNS2 = 'dns2'
Expand Down

0 comments on commit 31c6043

Please sign in to comment.