# Path: transmogrify/settings.py
# DEFAULT_SETTINGS = {
#     'BASE_PATH': "/home/media/",
#     'DEBUG': False,
#     'EXTERNAL_PREFIX': "/external/",
#     'FALLBACK_SERVERS': (),
#     'IMAGE_OPTIMIZATION_CMD': '',
#     'NO_IMAGE_URL': "",
#     'OPENCV_PREFIX': '/usr/local/share/',
#     'ORIG_BASE_PATH': "/home/media/",
#     'ORIG_PATH_HANDLER': None,
#     'PATH_ALIASES': {},
#     'SECRET_KEY': "",
#     'USE_VHOSTS': False,
#     'VALID_DOMAINS': [],
#     'VHOST_DOC_BASE': "",
#     'VALID_IMAGE_EXTENSIONS': ['jpeg', 'jpg', 'gif', 'png', ],
#     'ALLOWED_PROCESSORS': ['__all__', ],
#     'CHECK_SECURITY': True,
# }
# USER_SETTINGS = DEFAULT_SETTINGS.copy()
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET")
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET_KEY")
#     USER_SETTINGS['DEBUG'] = bool_from_env("TRANSMOGRIFY_DEBUG")
#     USER_SETTINGS['BASE_PATH'] = os.environ.get("TRANSMOGRIFY_BASE_PATH")
#     USER_SETTINGS['USE_VHOSTS'] = bool_from_env("TRANSMOGRIFY_USE_VHOSTS", False)
#     USER_SETTINGS['VHOST_DOC_BASE'] = os.environ.get("TRANSMOGRIFY_VHOST_DOC_BASE", "")
#     USER_SETTINGS['NO_IMAGE_URL'] = os.environ.get("TRANSMOGRIFY_NO_IMG_URL", "")
#     USER_SETTINGS['PATH_ALIASES'] = dict(lists_from_env("TRANSMOGRIFY_PATH_ALIASES"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = dict(lists_from_env("TRANSMOGRIFY_FALLBACK_SERVERS"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = {}
#     USER_SETTINGS['ORIG_PATH_HANDLER'] = os.environ.get("TRANSMOGRIFY_ORIG_PATH_HANDLER", "")
#     USER_SETTINGS['ORIG_BASE_PATH'] = os.environ.get("TRANSMOGRIFY_ORIG_BASE_PATH", "/home/media/")
#     USER_SETTINGS['EXTERNAL_PREFIX'] = os.environ.get("TRANSMOGRIFY_EXTERNAL_PREFIX", "/external/")
#     USER_SETTINGS['ALLOWED_PROCESSORS'] = os.environ.get("TRANSMOGRIFY_ALLOWED_PROCESSORS", "__all__,").split(",")
#     USER_SETTINGS['IMAGE_OPTIMIZATION_CMD'] = list_from_env("TRANSMOGRIFY_IMAGE_OPTIMIZATION_CMD")
# PATH_ALIASES = {}
# FALLBACK_SERVERS = (
#     # Format is
#     # (regex, repl, host),
#     # (r"^/media/(.*), "\1", "http://www.example.com/"),
# )
# PROCESSORS = {}
# ALLOW_ALL_PROCESSORS = '__all__' in USER_SETTINGS['ALLOWED_PROCESSORS']
# def bool_from_env(key, default=False):
# def list_from_env(key, default=""):
# def lists_from_env(key, default=""):
# 
# Path: transmogrify/utils.py
# def is_tool(name):
#     try:
#         devnull = open(os.devnull)
#         # devnull = subprocess.PIPE
#         if not isinstance(name, (list, tuple)):
#             name = [name]
#         subprocess.Popen(name, stdout=devnull, stderr=devnull).communicate()
#     except OSError as e:
#         if e.errno == os.errno.ENOENT:
#             return False
#     return True


import subprocess
import os
import logging
import daiquiri
    from PIL import Image
    from PIL.ImageCms import profileToProfile
from .settings import IMAGE_OPTIMIZATION_CMD
from .utils import is_tool
    from io import BytesIO

try:
    HAS_PROFILE_TO_PROFILE = True
except ImportError:  # pragma: no cover
    HAS_PROFILE_TO_PROFILE = False


daiquiri.setup(level=logging.INFO)
logger = daiquiri.getLogger(__name__)


def convert_to_rgb(img):
    """
    Convert an image to RGB if it isn't already RGB or grayscale
    """
    if img.mode == 'CMYK' and HAS_PROFILE_TO_PROFILE:
        profile_dir = os.path.join(os.path.dirname(__file__), 'profiles')
        input_profile = os.path.join(profile_dir, "USWebUncoated.icc")
        output_profile = os.path.join(profile_dir, "sRGB_v4_ICC_preference.icc")
        return profileToProfile(img, input_profile, output_profile, outputMode='RGB')
    return img


def optimize(image, fmt='jpeg', quality=80):
    """
    Optimize the image if the IMAGE_OPTIMIZATION_CMD is set.

    IMAGE_OPTIMIZATION_CMD must accept piped input
    """


# Path: transmogrify/settings.py
# DEFAULT_SETTINGS = {
#     'BASE_PATH': "/home/media/",
#     'DEBUG': False,
#     'EXTERNAL_PREFIX': "/external/",
#     'FALLBACK_SERVERS': (),
#     'IMAGE_OPTIMIZATION_CMD': '',
#     'NO_IMAGE_URL': "",
#     'OPENCV_PREFIX': '/usr/local/share/',
#     'ORIG_BASE_PATH': "/home/media/",
#     'ORIG_PATH_HANDLER': None,
#     'PATH_ALIASES': {},
#     'SECRET_KEY': "",
#     'USE_VHOSTS': False,
#     'VALID_DOMAINS': [],
#     'VHOST_DOC_BASE': "",
#     'VALID_IMAGE_EXTENSIONS': ['jpeg', 'jpg', 'gif', 'png', ],
#     'ALLOWED_PROCESSORS': ['__all__', ],
#     'CHECK_SECURITY': True,
# }
# USER_SETTINGS = DEFAULT_SETTINGS.copy()
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET")
#     USER_SETTINGS['SECRET_KEY'] = os.environ.get("TRANSMOGRIFY_SECRET_KEY")
#     USER_SETTINGS['DEBUG'] = bool_from_env("TRANSMOGRIFY_DEBUG")
#     USER_SETTINGS['BASE_PATH'] = os.environ.get("TRANSMOGRIFY_BASE_PATH")
#     USER_SETTINGS['USE_VHOSTS'] = bool_from_env("TRANSMOGRIFY_USE_VHOSTS", False)
#     USER_SETTINGS['VHOST_DOC_BASE'] = os.environ.get("TRANSMOGRIFY_VHOST_DOC_BASE", "")
#     USER_SETTINGS['NO_IMAGE_URL'] = os.environ.get("TRANSMOGRIFY_NO_IMG_URL", "")
#     USER_SETTINGS['PATH_ALIASES'] = dict(lists_from_env("TRANSMOGRIFY_PATH_ALIASES"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = dict(lists_from_env("TRANSMOGRIFY_FALLBACK_SERVERS"))
#         USER_SETTINGS['FALLBACK_SERVERS'] = {}
#     USER_SETTINGS['ORIG_PATH_HANDLER'] = os.environ.get("TRANSMOGRIFY_ORIG_PATH_HANDLER", "")
#     USER_SETTINGS['ORIG_BASE_PATH'] = os.environ.get("TRANSMOGRIFY_ORIG_BASE_PATH", "/home/media/")
#     USER_SETTINGS['EXTERNAL_PREFIX'] = os.environ.get("TRANSMOGRIFY_EXTERNAL_PREFIX", "/external/")
#     USER_SETTINGS['ALLOWED_PROCESSORS'] = os.environ.get("TRANSMOGRIFY_ALLOWED_PROCESSORS", "__all__,").split(",")
#     USER_SETTINGS['IMAGE_OPTIMIZATION_CMD'] = list_from_env("TRANSMOGRIFY_IMAGE_OPTIMIZATION_CMD")
# PATH_ALIASES = {}
# FALLBACK_SERVERS = (
#     # Format is
#     # (regex, repl, host),
#     # (r"^/media/(.*), "\1", "http://www.example.com/"),
# )
# PROCESSORS = {}
# ALLOW_ALL_PROCESSORS = '__all__' in USER_SETTINGS['ALLOWED_PROCESSORS']
# def bool_from_env(key, default=False):
# def list_from_env(key, default=""):
# def lists_from_env(key, default=""):
# 
# Path: transmogrify/utils.py
# def is_tool(name):
#     try:
#         devnull = open(os.devnull)
#         # devnull = subprocess.PIPE
#         if not isinstance(name, (list, tuple)):
#             name = [name]
#         subprocess.Popen(name, stdout=devnull, stderr=devnull).communicate()
#     except OSError as e:
#         if e.errno == os.errno.ENOENT:
#             return False
#     return True


# Path: transmogrify/optimize.py
import subprocess
import os
import logging
import daiquiri
    from PIL import Image
    from PIL.ImageCms import profileToProfile
from .settings import IMAGE_OPTIMIZATION_CMD
from .utils import is_tool
    from io import BytesIO


try:
    HAS_PROFILE_TO_PROFILE = True
except ImportError:  # pragma: no cover
    HAS_PROFILE_TO_PROFILE = False


daiquiri.setup(level=logging.INFO)
logger = daiquiri.getLogger(__name__)


def convert_to_rgb(img):
    """
    Convert an image to RGB if it isn't already RGB or grayscale
    """
    if img.mode == 'CMYK' and HAS_PROFILE_TO_PROFILE:
        profile_dir = os.path.join(os.path.dirname(__file__), 'profiles')
        input_profile = os.path.join(profile_dir, "USWebUncoated.icc")
        output_profile = os.path.join(profile_dir, "sRGB_v4_ICC_preference.icc")
        return profileToProfile(img, input_profile, output_profile, outputMode='RGB')
    return img


def optimize(image, fmt='jpeg', quality=80):
    """
    Optimize the image if the IMAGE_OPTIMIZATION_CMD is set.

    IMAGE_OPTIMIZATION_CMD must accept piped input
    """


    if IMAGE_OPTIMIZATION_CMD and is_tool(IMAGE_OPTIMIZATION_CMD):