本文整理汇总了Python中sorl.thumbnail.images.ImageFile.exists方法的典型用法代码示例。如果您正苦于以下问题:Python ImageFile.exists方法的具体用法?Python ImageFile.exists怎么用?Python ImageFile.exists使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sorl.thumbnail.images.ImageFile
的用法示例。
在下文中一共展示了ImageFile.exists方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
source = ImageFile(file_)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
if not thumbnail.exists():
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
source_image = default.engine.get_image(source)
# We might as well set the size since we have the image in memory
size = default.engine.get_image_size(source_image)
source.set_size(size)
self._create_thumbnail(source_image, geometry_string, options,
thumbnail)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
return thumbnail
示例2: create_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def create_thumbnail(file_, geometry_string, options, name):
thumbnail = ImageFile(name, default.storage)
if not thumbnail.exists():
source = ImageFile(file_)
source_image = default.engine.get_image(source)
size = default.engine.get_image_size(source_image)
source.set_size(size)
default.backend._create_thumbnail(source_image, geometry_string, options, thumbnail)
# Need to update both the source and the thumbnail with correct sizing
default.kvstore.set(source)
default.kvstore.set(thumbnail, source)
示例3: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
if not options.get('format'):
ext = str(file_).split('.')[-1].lower()
options['format'] = ext.upper() if ext in AUTO_FORMATS else settings.THUMBNAIL_FORMAT
source = ImageFile(file_)
for key, value in self.default_options.items():
options.setdefault(key, value)
# For the future I think it is better to add options only if they
# differ from the default settings as below. This will ensure the same
# filenames beeing generated for new options at default.
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
if not thumbnail.exists():
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
try:
source_image = default.engine.get_image(source)
except IOError:
if settings.THUMBNAIL_IMAGE_MISSING_DUMMY:
return DummyImageFile(geometry_string)
else:
raise
# We might as well set the size since we have the image in memory
size = default.engine.get_image_size(source_image)
source.set_size(size)
self._create_thumbnail(source_image, geometry_string, options,
thumbnail)
self._create_alternative_resolutions(source, geometry_string,
options, thumbnail.name)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
return thumbnail
示例4: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
source = ImageFile(file_)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
# For the future I think it is better to add options only if they
# differ from the default settings as below. This will ensure the same
# filenames beeing generated for new options at default.
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
if not thumbnail.exists():
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
# so we make the assumption that if the thumbnail is not cached, it doesn't exist
try:
source_image = default.engine.get_image(source)
except IOError:
# if S3Storage says file doesn't exist remotely, don't try to
# create it, exit early
# Will return working empty image type; 404'd image
logger.warn('Remote file [%s] at [%s] does not exist', file_,
geometry_string)
return thumbnail
# We might as well set the size since we have the image in memory
image_info = default.engine.get_image_info(source_image)
options['image_info'] = image_info
size = default.engine.get_image_size(source_image)
source.set_size(size)
self._create_thumbnail(source_image, geometry_string, options,
thumbnail)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
return thumbnail
示例5: create_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def create_thumbnail(file_, geometry_string, options, name, force=False):
if file_:
source = ImageFile(file_)
else:
return
thumbnail = ImageFile(name, default.storage)
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists() or force:
try:
source_image = default.engine.get_image(source)
except IOError as e:
logger.exception(e)
# if S3Storage says file doesn't exist remotely, don't try to
# create it and exit early.
# Will return working empty image type; 404'd image
logger.warn(
text_type('Remote file [%s] at [%s] does not exist'),
file_, geometry_string)
return
# We might as well set the size since we have the image in memory
try:
image_info = default.engine.get_image_info(source_image)
options['image_info'] = image_info
except AttributeError:
options['image_info'] = {}
size = default.engine.get_image_size(source_image)
source.set_size(size)
try:
default.backend._create_thumbnail(
source_image, geometry_string, options, thumbnail)
default.backend._create_alternative_resolutions(
source_image, geometry_string, options, thumbnail.name)
finally:
default.engine.cleanup(source_image)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
示例6: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
source = ImageFile(file_)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
options['mtime'] = os.path.getmtime(source.storage.path(source))###customization
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached and cached.exists():###customization
return cached
if not thumbnail.exists():
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
source_image = default.engine.get_image(source)
size = default.engine.get_image_size(source_image)
if options.get('autocrop', None):
source_image = autocrop(source_image, geometry_string, options)
# We might as well set the size since we have the image in memory
size = default.engine.get_image_size(source_image)
source.set_size(size)
### customization: race condition, do not raise an OSError when the dir exists.
# see sorl.thumbnail.images.ImageFile.write, it's not safe to simply throw
# /sub/dir/name.jpg to django.core.files.storage.FileSystemStorage._save
full_path = thumbnail.storage.path(name)
directory = os.path.dirname(full_path)
if not os.path.exists(directory):
try:
os.makedirs(directory)
except OSError:
pass
### end of customization
self._create_thumbnail(source_image, geometry_string, options,
thumbnail)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
return thumbnail
示例7: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, force_create=True, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
source = ImageFile(file_)
if settings.THUMBNAIL_PRESERVE_FORMAT:
format = self._get_format(file_)
if format == "GIF":
options.setdefault("format", format)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
# For the future I think it is better to add options only if they
# differ from the default settings as below. This will ensure the same
# filenames beeing generated for new options at default.
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
if not force_create:
return source
if not thumbnail.exists():
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
source_image = default.engine.get_image(source)
# We might as well set the size since we have the image in memory
size = default.engine.get_image_size(source_image)
source.set_size(size)
self._create_thumbnail(source_image, geometry_string, options, thumbnail)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
return thumbnail
示例8: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
source = ImageFile(file_)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
# For the future I think it is better to add options only if they
# differ from the default settings as below. This will ensure the same
# filenames beeing generated for new options at default.
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
if not thumbnail.exists():
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
try:
source_image = default.engine.get_image(source)
# We might as well set the size since we have the image in memory
size = default.engine.get_image_size(source_image)
source.set_size(size)
self._create_thumbnail(source_image, geometry_string, options,
thumbnail)
self._create_alternative_resolutions(source_image, geometry_string,
options, thumbnail.name)
except IOError, e:
if settings.THUMBNAIL_DUMMY:
return DummyImageFile(geometry_string)
else:
raise e
示例9: get_thumbnail
# 需要导入模块: from sorl.thumbnail.images import ImageFile [as 别名]
# 或者: from sorl.thumbnail.images.ImageFile import exists [as 别名]
def get_thumbnail(self, file_, geometry_string, **options):
"""
Returns thumbnail as an ImageFile instance for file with geometry and
options given. First it will try to get it from the key value store,
secondly it will create it.
"""
logger.debug('Getting thumbnail for file [%s] at [%s]', file_, geometry_string)
if file_:
source = ImageFile(file_)
elif settings.THUMBNAIL_DUMMY:
return DummyImageFile(geometry_string)
else:
return None
# preserve image filetype
if settings.THUMBNAIL_PRESERVE_FORMAT:
options.setdefault('format', self._get_format(source))
for key, value in self.default_options.items():
options.setdefault(key, value)
# For the future I think it is better to add options only if they
# differ from the default settings as below. This will ensure the same
# filenames being generated for new options at default.
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
name = self._get_thumbnail_filename(source, geometry_string, options)
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
# We have to check exists() because the Storage backend does not
# overwrite in some implementations.
if settings.THUMBNAIL_FORCE_OVERWRITE or not thumbnail.exists():
try:
source_image = default.engine.get_image(source)
except IOError as e:
logger.exception(e)
if settings.THUMBNAIL_DUMMY:
return DummyImageFile(geometry_string)
else:
# if S3Storage says file doesn't exist remotely, don't try to
# create it and exit early.
# Will return working empty image type; 404'd image
logger.warning(
'Remote file [%s] at [%s] does not exist',
file_, geometry_string,
)
return thumbnail
# We might as well set the size since we have the image in memory
image_info = default.engine.get_image_info(source_image)
options['image_info'] = image_info
size = default.engine.get_image_size(source_image)
source.set_size(size)
try:
self._create_thumbnail(source_image, geometry_string, options,
thumbnail)
self._create_alternative_resolutions(source_image, geometry_string,
options, thumbnail.name)
finally:
default.engine.cleanup(source_image)
# If the thumbnail exists we don't create it, the other option is
# to delete and write but this could lead to race conditions so I
# will just leave that out for now.
default.kvstore.get_or_set(source)
default.kvstore.set(thumbnail, source)
return thumbnail