def open_libtiff(filename): """Opens a tiff file with ``tiffcp``, which needs to be installed on your system. :param filename: name of tiff image file :type filename: string :returns: PIL image :rtype: Image.Image """ # get info info = get_info_libtiff(filename) # extract temp = system.TempFile() command = (TIFFCP, '-c', 'none', '-r', '-1', filename, temp.path) try: returncode = system.shell_returncode(command) if returncode == 0: image = Image.open(temp.path) image.load() # otherwise temp file can't be deleted image.info.update(info) image.info['Convertor'] = 'libtiff' return image finally: temp.close(force_remove=False) raise IOError('Could not extract tiff image with tiffcp.')
def open_image_with_command(filename, command, app, extension='png', temp_ext=None): """Open with an external command (such as Inkscape, dcraw, imagemagick). :param filename: filename, from which a temporary filename will be derived :type filename: string :param command: conversion command with optional temp file interpolation :type command: string :param extension: file type :type extension: string :param temp_ext: if a temp file can not be specified to the command (eg dcraw), give the file extension of the command output :type temp_ext: string """ if temp_ext: # eg dcraw temp = None temp_file = system.replace_ext(filename, temp_ext) else: # imagemagick, ... if not extension.startswith('.'): extension = '.' + extension temp = system.TempFile(extension) temp_file = temp.path command.append(temp_file) try: stdout, stderr, err = system.call_out_err_temp( command, input=[filename], output_ext=temp_ext, ) if not err and os.path.exists(temp_file): # copy() otherwise temp file can't be deleted image = Image.open(temp_file).copy() image.info['Convertor'] = app return image finally: if temp: temp.close(force_remove=False) elif temp_ext and os.path.exists(temp_file): os.remove(temp_file) message = '%s (%s)\n\n%s: %s\n\n%s: %s\n\n%s: %s' % ( _('Could not open image with %s.') % app, '(%s)' % err, _('Command'), command, _('Output'), stdout, _('Error'), stderr, ) raise IOError(message)
def save_libtiff(image, filename, compression=None, **options): """Saves a tiff compressed file with tiffcp. :param image: PIL image :type image: Image.Image :param filename: name of tiff image file :type filename: string :param compression: g3, g4, jpeg, lzw, tiff_lzw :type compression: string :returns: log message :rtype: string """ if compression is None: compression = image.info['compression'] option = [] if compression in ['raw', 'none']: image.save(filename, 'tiff', **options) return '' elif compression in ['g3', 'g4'] and image.mode != '1': image = image.convert('1') elif compression == 'jpeg': option = ['-r', '16'] if image.mode == 'RGBA': image = image.convert('RGB') elif compression == 'tiff_lzw': compression = 'lzw' temp = system.TempFile() temp_c = system.TempFile() try: image.save(temp.path, 'tiff', **options) input = [TIFFCP, '-c', compression] if option: input.extend(option) input.extend([temp.path, temp_c.path]) stdout, stderr, err = system.call_out_err( input, universal_newlines=True, ) finally: temp.close() temp_c.close(dest=filename) if err or stdout or stderr: raise Exception( 'tiffcp (%s): %s%s\n%s' % (err, stdout, stderr, input), ) return ''
def read_metadata_temp(filename): try: image = pyexiv2.ImageMetadata(filename) image.read() temp = None except (IOError, UnicodeDecodeError): temp = system.TempFile(suffix=os.path.splitext(filename)[-1]) shutil.copy2(filename, temp.path) image = pyexiv2.ImageMetadata(temp.path) image.read() return image, temp
def _save_to_cache_size(cache_size_label, filename, size_label, thumb, thumb_cache, size, pnginfo, **options): thumb_cache.thumbnail(FREEDESKTOP_SIZE[cache_size_label], Image.ANTIALIAS) temp = system.TempFile('.png') imtools.save(thumb_cache, temp.path, pnginfo=pnginfo, **options) thumb_filename = get_freedesktop_filename(filename, cache_size_label) temp.close(dest=thumb_filename) os.chmod(thumb_filename, 0600) if cache_size_label == size_label: # make thumbnail as it is smaller than this thumb cache size thumb = thumbnail(thumb_cache, size) return thumb, thumb_cache
def open_image_with_command(filename, command, app, extension='png', temp_ext=None): """Open with an external command (such as Inkscape, dcraw, imagemagick). :param filename: filename, from which a temporary filename will be derived :type filename: string :param command: conversion command with optional temp file interpolation :type command: string :param extension: file type :type extension: string :param temp_ext: if a temp file can not be specified to the command (eg dcraw), give the file extension of the command output :type temp_ext: string """ temp = None temp_file = None if temp_ext: # eg dcraw temp_file = os.path.splitext(filename)[0] + '.' + temp_ext else: # imagemagick, ... if not extension.startswith('.'): extension = '.' + extension temp = system.TempFile(extension) temp_file = temp.path command.append(temp_file) try: output, error = system.shell(command) if os.path.exists(temp_file): image = Image.open(temp_file) image.load() # otherwise temp file can't be deleted image.info['Convertor'] = app return image finally: if temp: temp.close(force_remove=False) elif temp_ext and os.path.exists(temp_file): os.remove(temp_file) message = _('Could not open image with %s.') % app + \ '\n\n%s: %s\n\n%s: %s\n\n%s: %s' % \ (_('Command'), command, _('Output'), output, _('Error'), error) raise IOError(message)
def save_libtiff(image, filename, compression=None, **options): """Saves a tiff compressed file with tiffcp. :param image: PIL image :type image: Image.Image :param filename: name of tiff image file :type filename: string :param compression: g3, g4, jpeg, lzw, tiff_lzw :type compression: string :returns: log message :rtype: string """ if compression in ['raw', 'none']: image.save(filename, 'tiff', **options) return '' if compression is None: compression = image.info['compression'] elif compression in ['g3', 'g4'] and image.mode != '1': image = image.convert('1') if compression == 'jpeg': option = ['-r', '16'] if image.mode == 'RGBA': image = image.convert('RGB') else: option = [] if compression == 'tiff_lzw': compression = 'lzw' temp = system.TempFile() try: image.save(temp.path, 'tiff', **options) input = [TIFFCP, temp.path, '-c', compression] if option: input.extend(option) input.append(filename) out, err = system.shell(input) finally: temp.close() if out or err: return 'Subprocess "tiffcp"\ninput:\n%s\noutput:\n%s%s\n'\ % (input, out, err) return ''
def save(image, filename, **options): """Saves an image with a filename and raise the specific ``InvalidWriteFormatError`` in case of an error instead of a ``KeyError``. Also can hack around UnicodeEncodeError (eg for IM format) :param image: image :type image: pil.Image :param filename: image filename :type filename: string """ try: image.save(filename, **options) except KeyError, format: raise InvalidWriteFormatError(format) except UnicodeEncodeError: temp = system.TempFile(suffix=os.path.splitext(filename)[-1]) image.save(temp.path, **options) temp.close(dest=filename) def save_check_mode(image, filename, **options): #save image with pil save(image, filename, **options) #verify saved file try: image_file = Image.open(filename) image_file.verify() except IOError: # We can't verify the image mode with PIL, so issue no warnings. return '' if image.mode != image_file.mode: