Beispiel #1
0
  def _resize(self, quality, width, height, format, resolution, frame, crop=False):
    """Resize and resample photo."""
    parameter_list = ['convert', '-colorspace', 'sRGB', '-depth', '8']
    if crop :
      parameter_list += '-thumbnail', '%sx%s^' % (width, height),\
                        '-gravity', 'center',\
                        '-extent','%sx%s' % (width, height)
    else:
      parameter_list += '-geometry', '%sx%s' % (width, height)
    parameter_list += '-quality', str(quality)
    if format not in VALID_TRANSPARENT_IMAGE_FORMAT_LIST:
      # ImageMagick way to remove transparent that works with multiple
      # images. http://www.imagemagick.org/Usage/masking/#remove
      parameter_list += '-bordercolor', 'white', '-border', '0'
    if resolution:
      parameter_list += '-density', '%sx%s' % (resolution, resolution)
    if frame is not None:
      parameter_list.append('-[%s]' % frame)
    else:
      parameter_list.append('-')

    if format:
      # Is there a way to make 'convert' fail if the format is unknown,
      # instead of treating this whole parameter as an output file path?
      # As a workaround, we run 'convert' in a non-writeable directory.
      if '/' in format or os.access('/', os.W_OK):
        raise ConversionError
      parameter_list.append('%s:-' % format)
    else:
      parameter_list.append('-')

    data = str(self.getData())
    if self.getContentType() == "image/svg+xml":
      data = transformUrlToDataURI(data)

    env = os.environ.copy()
    env.update({'LC_NUMERIC':'C'})
    process = subprocess.Popen(parameter_list,
                               env=env,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               cwd='/',
                               close_fds=True)
    try:
        # XXX: The only portable way is to pass what stdin.write can accept,
        #      which is a string for PIPE.
        image, err = process.communicate(data)
    finally:
        del process
    if image:
      return StringIO(image)
    raise ConversionError('Image conversion failed (%s).' % err)
Beispiel #2
0
    def _resize(self, quality, width, height, format, resolution, frame):
        """Resize and resample photo."""
        parameter_list = [
            'convert', '-colorspace', 'sRGB', '-depth', '8', '-quality',
            str(quality), '-geometry',
            '%sx%s' % (width, height)
        ]
        if format not in VALID_TRANSPARENT_IMAGE_FORMAT_LIST:
            # ImageMagick way to remove transparent that works with multiple
            # images. http://www.imagemagick.org/Usage/masking/#remove
            parameter_list += '-bordercolor', 'white', '-border', '0'
        if resolution:
            parameter_list += '-density', '%sx%s' % (resolution, resolution)
        if frame is not None:
            parameter_list.append('-[%s]' % frame)
        else:
            parameter_list.append('-')

        if format:
            # Is there a way to make 'convert' fail if the format is unknown,
            # instead of treating this whole parameter as an output file path?
            # As a workaround, we run 'convert' in a non-writeable directory.
            if '/' in format or os.access('/', os.W_OK):
                raise ConversionError
            parameter_list.append('%s:-' % format)
        else:
            parameter_list.append('-')

        data = str(self.getData())
        if self.getContentType() == "image/svg+xml":
            data = transformUrlToDataURI(data)

        env = os.environ.copy()
        env.update({'LC_NUMERIC': 'C'})
        process = subprocess.Popen(parameter_list,
                                   env=env,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   cwd='/',
                                   close_fds=True)
        try:
            # XXX: The only portable way is to pass what stdin.write can accept,
            #      which is a string for PIPE.
            image, err = process.communicate(data)
        finally:
            del process
        if image:
            return StringIO(image)
        raise ConversionError('Image conversion failed (%s).' % err)
Beispiel #3
0
  def _resize(self, quality, width, height, format, resolution, frame):
    """Resize and resample photo."""
    parameter_list = ['convert', '-colorspace', 'sRGB', '-depth', '8',
                                 '-quality', str(quality),
                                 '-geometry', '%sx%s' % (width, height)]
    if format not in VALID_TRANSPARENT_IMAGE_FORMAT_LIST:
      # ImageMagick way to remove transparent that works with multiple
      # images. http://www.imagemagick.org/Usage/masking/#remove
      parameter_list += '-bordercolor', 'white', '-border', '0'
    if resolution:
      parameter_list += '-density', '%sx%s' % (resolution, resolution)
    if frame is not None:
      parameter_list.append('-[%s]' % frame)
    else:
      parameter_list.append('-')

    if format:
      parameter_list.append('%s:-' % format)
    else:
      parameter_list.append('-')

    data = str(self.getData())
    if self.getContentType() == "image/svg+xml":
      data = transformUrlToDataURI(data)

    env = os.environ.copy()
    env.update({'LC_NUMERIC':'C'})
    process = subprocess.Popen(parameter_list,
                               env=env,
                               stdin=subprocess.PIPE,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               close_fds=True)
    try:
        # XXX: The only portable way is to pass what stdin.write can accept,
        #      which is a string for PIPE.
        image, err = process.communicate(data)
    finally:
        del process
    if image:
      return StringIO(image)
    raise ConversionError('Image conversion failed (%s).' % err)
Beispiel #4
0
    def _resize(self, quality, width, height, format, resolution, frame):
        """Resize and resample photo."""
        parameter_list = [
            'convert', '-colorspace', 'sRGB', '-depth', '8', '-quality',
            str(quality), '-geometry',
            '%sx%s' % (width, height)
        ]
        if format not in VALID_TRANSPARENT_IMAGE_FORMAT_LIST:
            # ImageMagick way to remove transparent that works with multiple
            # images. http://www.imagemagick.org/Usage/masking/#remove
            parameter_list += '-bordercolor', 'white', '-border', '0'
        if resolution:
            parameter_list += '-density', '%sx%s' % (resolution, resolution)
        if frame is not None:
            parameter_list.append('-[%s]' % frame)
        else:
            parameter_list.append('-')

        if format:
            parameter_list.append('%s:-' % format)
        else:
            parameter_list.append('-')

        data = str(self.getData())
        if self.getContentType() == "image/svg+xml":
            data = transformUrlToDataURI(data)

        process = subprocess.Popen(parameter_list,
                                   stdin=subprocess.PIPE,
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE,
                                   close_fds=True)
        try:
            # XXX: The only portable way is to pass what stdin.write can accept,
            #      which is a string for PIPE.
            image, err = process.communicate(data)
        finally:
            del process
        if image:
            return StringIO(image)
        raise ConversionError('Image conversion failed (%s).' % err)