add dither option for image processing (#1317)

* [Image] add dither option for image processing

* fix import order

* revert import location
and hardcode two dither method

* fix format
This commit is contained in:
San 2020-11-02 02:54:13 +08:00 committed by GitHub
parent 3fcdaaefe0
commit 31c604331c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 4 deletions

View file

@ -4,14 +4,14 @@ from esphome import core
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,
@ -28,6 +28,7 @@ IMAGE_SCHEMA = cv.Schema({
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),
})
@ -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
@ -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):

View file

@ -159,6 +159,7 @@ CONF_DISCOVERY = 'discovery'
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'