本文整理汇总了Python中sorl.thumbnail.parsers.parse_geometry函数的典型用法代码示例。如果您正苦于以下问题:Python parse_geometry函数的具体用法?Python parse_geometry怎么用?Python parse_geometry使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parse_geometry函数的17个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_geometry
def test_geometry(self):
g = parse_geometry('222x30')
self.assertEqual(g, (222, 30))
g = parse_geometry('222')
self.assertEqual(g, (222, None))
g = parse_geometry('x999')
self.assertEqual(g, (None, 999))
开发者ID:aalebedev,项目名称:sorl-thumbnail,代码行数:7,代码来源:tests.py
示例2: get_thumbnail_attributes
def get_thumbnail_attributes(self, image_thm, size_str):
if image_thm:
margin = [0, 0, 0, 0]
image_file = default.kvstore.get_or_set(ImageFile(image_thm))
x, y = parse_geometry(size_str, image_file.ratio)
ex = x - image_file.x
margin[3] = ex / 2
margin[1] = ex / 2
if ex % 2:
margin[1] += 1
ey = y - image_file.y
margin[0] = ey / 2
margin[2] = ey / 2
if ey % 2:
margin[2] += 1
margin_str = ' '.join(['%spx' % n for n in margin])
res_dt = {
'margin_str': margin_str,
}
if settings.LUGATI_FULL_IMAGE_PATH:
res_dt['thumbnail_url'] = settings.POS_SERVER + image_thm.url
else:
res_dt['thumbnail_url'] = image_thm.url
else:
res_dt = {
'thumbnail_url': '',
'margin_str': ''
}
return res_dt['thumbnail_url'], res_dt['margin_str']
开发者ID:xorsnn,项目名称:lugati,代码行数:31,代码来源:models.py
示例3: _create_alternative_resolutions
def _create_alternative_resolutions(self, source_image, geometry_string,
options, name):
"""
Creates the thumbnail by using default.engine with multiple output
sizes. Appends @<ratio>x to the file name.
"""
if not options['alternative_resolutions']:
return
ratio = default.engine.get_image_ratio(source_image)
geometry = parse_geometry(geometry_string, ratio)
file_type = name.split('.')[len(name.split('.')) - 1]
for resolution in options['alternative_resolutions']:
resolution_geometry = (int(geometry[0] * resolution), int(geometry[1] * resolution))
resolution_options = options.copy()
if 'crop' in options and isinstance(options['crop'], basestring):
crop = options['crop'].split(" ")
for i in xrange(len(crop)):
s = re.match("(\d+)px", crop[i])
if s:
crop[i] = "%spx" % int(int(s.group(1)) * resolution)
resolution_options['crop'] = " ".join(crop)
thumbnail_name = name.replace(".%s" % file_type,
"@%sx.%s" % (resolution, file_type))
image = default.engine.create(source_image, resolution_geometry, resolution_options)
thumbnail = ImageFile(thumbnail_name, default.storage)
default.engine.write(image, resolution_options, thumbnail)
size = default.engine.get_image_size(image)
thumbnail.set_size(size)
开发者ID:UKA,项目名称:sorl-thumbnail,代码行数:31,代码来源:base.py
示例4: margin
def margin(file_, geometry_string):
"""
Returns the calculated margin for an image and geometry
"""
if not file_ or (sorl_settings.THUMBNAIL_DUMMY or isinstance(file_, DummyImageFile)):
return 'auto'
margin = [0, 0, 0, 0]
image_file = default.kvstore.get_or_set(ImageFile(file_))
x, y = parse_geometry(geometry_string, image_file.ratio)
ex = x - image_file.x
margin[3] = ex / 2
margin[1] = ex / 2
if ex % 2:
margin[1] += 1
ey = y - image_file.y
margin[0] = ey / 2
margin[2] = ey / 2
if ey % 2:
margin[2] += 1
return ' '.join(['%dpx' % n for n in margin])
开发者ID:AlfiyaZi,项目名称:sorl-thumbnail,代码行数:28,代码来源:thumbnail.py
示例5: _create_thumbnail
def _create_thumbnail(self, source_image, geometry_string, options, thumbnail):
"""
Creates the thumbnail by using default.engine
"""
ratio = default.engine.get_image_ratio(source_image)
geometry = parse_geometry(geometry_string, ratio)
image = default.engine.create(source_image, geometry, options)
# To RGBA!
image = image.convert(mode="RGBA")
# Per pixel calculations.
#
# This is slow and could be optimized.. but it works in any case.
new_data = []
for item in image.getdata():
# If px is white, make transparent
if item[:3] == (255, 255, 255):
new_data.append((255, 255, 255, 0))
continue
# All other pixels are "whitened" by TO_GRAY_INTENSITY
# (given that 0 is white and 1 is original intensity)
converted_pixel = self.__calc_new_intensity(item)
new_data.append(converted_pixel)
image.putdata(new_data)
default.engine.write(image, options, thumbnail)
# It's much cheaper to set the size here
size = default.engine.get_image_size(image)
thumbnail.set_size(size)
开发者ID:pior,项目名称:mtlpyweb,代码行数:33,代码来源:gray_thumbs.py
示例6: _create_thumbnail
def _create_thumbnail(self, source_image, geometry_string, options, thumbnail):
"""
Creates the thumbnail by using default.engine
"""
ratio = default.engine.get_image_ratio(source_image)
geometry = parse_geometry(geometry_string, ratio)
image = default.engine.create(source_image, geometry, options)
default.engine.write(image, options, thumbnail)
# It's much cheaper to set the size here
size = default.engine.get_image_size(image)
thumbnail.set_size(size)
开发者ID:kvh,项目名称:sorl-thumbnail,代码行数:11,代码来源:base.py
示例7: _create_thumbnail
def _create_thumbnail(self, source_image, geometry_string, options, thumbnail):
"""
Creates the thumbnail by using default.engine
"""
logger.debug("Creating thumbnail file [%s] at [%s] with [%s]", thumbnail.name, geometry_string, options)
ratio = default.engine.get_image_ratio(source_image, options)
geometry = parse_geometry(geometry_string, ratio)
image = default.engine.create(source_image, geometry, options)
default.engine.write(image, options, thumbnail)
# It's much cheaper to set the size here
size = default.engine.get_image_size(image)
thumbnail.set_size(size)
开发者ID:julianwachholz,项目名称:sorl-thumbnail,代码行数:12,代码来源:base.py
示例8: get_thumbnail
def get_thumbnail(self, file_, geometry_string, **options):
source = ImageFile(file_)
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
# Generate a name for the thumbnail
name = self._get_thumbnail_filename(source, geometry_string, options)
# See if we've got a hit in the cache
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
# We cannot check if the file exists, as remote storage is slow. If
# we have reached this point, the image does not exist in our kvstore
# so create the entry and queue the generation of the image.
#
# Note: If the thumbnail file has been deleted, you will need to manually
# clear the corresponding row from the kvstore to have thumbnail rebuilt.
job = create_thumbnail.delay(file_, geometry_string, options, name)
if job:
geometry = parse_geometry(geometry_string)
# We can't add a source row to the kvstore without the size
# information being looked up, so add dummy information here
# We'll need to correct this information when we generate the thumbnail
source.set_size(geometry)
default.kvstore.get_or_set(source)
# We don't want to do any file access in this thread, so we tell sorlery
# to proceed as normal and cheekily update the name and storage after
# the hash has been calculated.
thumbnail.set_size(geometry)
default.kvstore.set(thumbnail, source)
# Now we go back and manually update the thumbnail to point at the source image
# Hopefully someone can suggest a better way to do this ... but the sorl internals
# don't make it easy to.
rawvalue = default.kvstore._get_raw(add_prefix(thumbnail.key))
rawvaluearr = deserialize(rawvalue)
rawvaluearr['name'] = file_.name
default.kvstore._set_raw(add_prefix(thumbnail.key), serialize(rawvaluearr))
thumbnail.name = file_.name
return thumbnail
开发者ID:aidanlister,项目名称:sorlery,代码行数:51,代码来源:backend.py
示例9: background_margin
def background_margin(file_, geometry_string):
"""
Returns the calculated margin for a background image and geometry
"""
if not file_ or settings.THUMBNAIL_DUMMY:
return 'auto'
margin = [0, 0]
image_file = default.kvstore.get_or_set(ImageFile(file_))
x, y = parse_geometry(geometry_string, image_file.ratio)
ex = x - image_file.x
margin[0] = ex / 2
ey = y - image_file.y
margin[1] = ey / 2
return ' '.join(['%spx' % n for n in margin])
开发者ID:SpiderOak,项目名称:sorl-thumbnail,代码行数:14,代码来源:thumbnail.py
示例10: test_truncated_generation_failure
def test_truncated_generation_failure(self):
"""
Confirm that generating a thumbnail for our "broken" image fails.
"""
name = 'data/broken.jpeg'
with open(name, 'rb') as broken_jpeg:
with self.assertRaises((OSError, IOError,)):
im = default.engine.get_image(broken_jpeg)
options = ThumbnailBackend.default_options
ratio = default.engine.get_image_ratio(im, options)
geometry = parse_geometry('120x120', ratio)
default.engine.create(im, geometry, options)
开发者ID:rootart,项目名称:sorl-thumbnail,代码行数:14,代码来源:test_engines.py
示例11: get_thumbnail
def get_thumbnail(self, file_, geometry_string, **options):
source = ImageFile(file_)
for key, value in self.default_options.items():
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 don't check if thumbnail exists as sorl-thumbnail does. It becomes
# very costly for remote storages.
# Furthermore, I have added following code to reduce/prevent duplicate
# tasks in celery. It's hacky.
# Finally, if there is no thumbnail, we create one.
from .tasks import create_thumbnail
job = create_thumbnail.delay(source.name, geometry_string, **options)
# Sometimes thumbnail generation takes quite some time, just return
# the original image then.
if job:
geometry = parse_geometry(geometry_string)
# We can't add a source row to the kvstore without the size
# information being looked up, so add dummy information here
# We'll need to correct this information when we generate the thumbnail
source.set_size(geometry)
default.kvstore.get_or_set(source)
# We don't want to do any file access in this thread, so we tell sorlery
# to proceed as normal and cheekily update the name and storage after
# the hash has been calculated.
thumbnail.set_size(geometry)
default.kvstore.set(thumbnail, source)
# Now we go back and manually update the thumbnail to point at the source image
# Hopefully someone can suggest a better way to do this ... but the sorl internals
# don't make it easy to.
rawvalue = default.kvstore._get_raw(add_prefix(thumbnail.key))
rawvaluearr = deserialize(rawvalue)
rawvaluearr['name'] = file_.name
default.kvstore._set_raw(add_prefix(thumbnail.key), serialize(rawvaluearr))
thumbnail.name = file_.name
return thumbnail
开发者ID:carriercomm,项目名称:sorl-thumbnail-async,代码行数:43,代码来源:backend.py
示例12: _create_alternative_resolutions
def _create_alternative_resolutions(self, source, geometry_string, options, name):
"""
Creates the thumbnail by using default.engine with multiple output
sizes. Appends @<ratio>x to the file name.
"""
if not options['alternative_resolutions']:
return
source_image = default.engine.get_image(source)
ratio = default.engine.get_image_ratio(source_image, options)
geometry_orig = parse_geometry(geometry_string, ratio)
file_type = name.split('.')[len(name.split('.'))-1]
for res in options['alternative_resolutions']:
geometry = (toint(geometry_orig[0]*res), toint(geometry_orig[1]*res))
thumbnail_name = name.replace(".%s" % file_type,
"@%sx.%s" % (res, file_type))
source_image = default.engine.get_image(source)
image = default.engine.create(source_image, geometry, options)
thumbnail = ImageFile(thumbnail_name, default.storage)
default.engine.write(image, options, thumbnail)
size = default.engine.get_image_size(image)
thumbnail.set_size(size)
开发者ID:rmoch,项目名称:sorl-thumbnail,代码行数:23,代码来源:base.py
示例13: sorl_margin
def sorl_margin(file_, geometry_string):
"""
This is copied from sorl/thumbnail/templatetags/thumbnail.py because of
problems with importing it. This should be removed when we remove
easy_thumbnail.
Returns the calculated margin for an image and geometry
"""
if not file_ or settings.THUMBNAIL_DUMMY:
return 'auto'
margin = [0, 0, 0, 0]
image_file = default.kvstore.get_or_set(ImageFile(file_))
x, y = parse_geometry(geometry_string, image_file.ratio)
ex = x - image_file.x
margin[3] = ex / 2
margin[1] = ex / 2
if ex % 2:
margin[1] += 1
ey = y - image_file.y
margin[0] = ey / 2
margin[2] = ey / 2
if ey % 2:
margin[2] += 1
return ' '.join([ '%spx' % n for n in margin ])
开发者ID:DjangoBD,项目名称:django-widgy,代码行数:24,代码来源:thumbnail_libs.py
示例14: clean_geometry
def clean_geometry(self):
data = self.cleaned_data['geometry']
try:
parse_geometry(data)
except ThumbnailParseError, e:
raise forms.ValidationError(str(e))
开发者ID:airtonix,项目名称:cms-thumbnail,代码行数:6,代码来源:forms.py
示例15: get_thumbnail
def get_thumbnail(self, file_, geometry_string, **options):
# Correct way for create source image, This is coped from begining of
# ThumbnailBackend.get_thumbnail
if file_:
source = ImageFile(file_)
elif settings.THUMBNAIL_DUMMY:
return DummyImageFile(geometry_string)
else:
return None
for key, value in self.default_options.iteritems():
options.setdefault(key, value)
for key, attr in self.extra_options:
value = getattr(settings, attr)
if value != getattr(default_settings, attr):
options.setdefault(key, value)
# Generate a name for the thumbnail
name = self._get_thumbnail_filename(source, geometry_string, options)
# See if we've got a hit in the cache
thumbnail = ImageFile(name, default.storage)
cached = default.kvstore.get(thumbnail)
if cached:
return cached
#fixing misssing 'image_info' to be compatible with sorl-thumbnail newewst version
else:
# 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 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.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
# We cannot check if the file exists, as remote storage is slow. If
# we have reached this point, the image does not exist in our kvstore
# so create the entry and queue the generation of the image.
#
# Note: If the thumbnail file has been deleted, you will need to manually
# clear the corresponding row from the kvstore to have thumbnail rebuilt.
job = create_thumbnail.delay(file_, geometry_string, options, name)
if job:
geometry = parse_geometry(geometry_string)
# We can't add a source row to the kvstore without the size
# information being looked up, so add dummy information here
# We'll need to correct this information when we generate the thumbnail
source.set_size(geometry)
default.kvstore.get_or_set(source)
# We don't want to do any file access in this thread, so we tell sorlery
# to proceed as normal and cheekily update the name and storage after
# the hash has been calculated.
thumbnail.set_size(geometry)
default.kvstore.set(thumbnail, source)
# Now we go back and manually update the thumbnail to point at the source image
# Hopefully someone can suggest a better way to do this ... but the sorl internals
# don't make it easy to.
rawvalue = default.kvstore._get_raw(add_prefix(thumbnail.key))
rawvaluearr = deserialize(rawvalue)
rawvaluearr['name'] = source.name
default.kvstore._set_raw(add_prefix(thumbnail.key), serialize(rawvaluearr))
thumbnail.name = source.name
return thumbnail
开发者ID:roojoom,项目名称:sorlery,代码行数:78,代码来源:backend.py
示例16: __init__
def __init__(self, geometry_string):
self.size = parse_geometry(
geometry_string,
settings.THUMBNAIL_DUMMY_RATIO,
)
开发者ID:SpiderOak,项目名称:sorl-thumbnail,代码行数:5,代码来源:images.py
示例17: get_placeholder_thumbnail
def get_placeholder_thumbnail(file_, geometry_string, **options):
"""Return a place holder url for the given geometry string"""
x, y = parse_geometry(geometry_string, ratio=1)
url = '//placehold.it/{}x{}'.format(x, y)
Url = namedtuple('Url', ('url',))
return Url(url=url)
开发者ID:punchagan,项目名称:akvo-rsr,代码行数:6,代码来源:utils.py
注:本文中的sorl.thumbnail.parsers.parse_geometry函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论