本文整理汇总了Python中django.core.files.images.get_image_dimensions方法的典型用法代码示例。如果您正苦于以下问题:Python images.get_image_dimensions方法的具体用法?Python images.get_image_dimensions怎么用?Python images.get_image_dimensions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.images
的用法示例。
在下文中一共展示了images.get_image_dimensions方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: clean_image
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def clean_image(self):
image = self.cleaned_data.get('image')
if image:
try:
if image._size > settings.SPEAKER_IMAGE_MAXIMUM_FILESIZE_IN_MB * 1024 * 1024:
raise forms.ValidationError(
_('Maximum size is %d MB')
% settings.SPEAKER_IMAGE_MAXIMUM_FILESIZE_IN_MB
)
except AttributeError:
pass
w, h = get_image_dimensions(image)
if w < settings.SPEAKER_IMAGE_MINIMUM_DIMENSION[0] \
or h < settings.SPEAKER_IMAGE_MINIMUM_DIMENSION[1]:
raise forms.ValidationError(
_('Minimum dimension is %d x %d')
% settings.SPEAKER_IMAGE_MINIMUM_DIMENSION
)
return image
示例2: post
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def post(self, request, *args, **kwargs):
image = self.get_object() # type: Image
previous_url = image.image_file.url
ret = super(ImageEditBasicView, self).post(request, *args, **kwargs)
image = self.get_object() # type: Image
new_url = image.image_file.url
if new_url != previous_url:
try:
image.w, image.h = get_image_dimensions(image.image_file)
except TypeError:
pass
image.square_cropping = ImageService(image).get_default_cropping()
image.save(keep_deleted=True)
image.thumbnail_invalidate()
if image.solution:
image.solution.delete()
return ret
示例3: _get_image_dimensions
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def _get_image_dimensions(self):
from numbers import Number
if not hasattr(self, '_dimensions_cache'):
close = self.closed
if self.field.width_field and self.field.height_field:
width = getattr(self.instance, self.field.width_field)
height = getattr(self.instance, self.field.height_field)
#check if the fields have proper values
if isinstance(width, Number) and isinstance(height, Number):
self._dimensions_cache = (width, height)
else:
self.open()
self._dimensions_cache = get_image_dimensions(self, close=close)
else:
self.open()
self._dimensions_cache = get_image_dimensions(self, close=close)
return self._dimensions_cache
示例4: get_default_cropping
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def get_default_cropping(self, revision_label = None):
if revision_label is None or revision_label == '0':
w, h = self.image.w, self.image.h
if w == 0 or h == 0:
(w, h) = get_image_dimensions(self.image.image_file.file)
else:
try:
revision = self.image.revisions.get(label=revision_label)
except ImageRevision.DoesNotExist:
return '0,0,0,0'
w, h = revision.w, revision.h
if w == 0 or h == 0:
(w, h) = get_image_dimensions(revision.image_file.file)
shorter_size = min(w, h) # type: int
x1 = int(w / 2.0 - shorter_size / 2.0) # type: int
x2 = int(w / 2.0 + shorter_size / 2.0) # type: int
y1 = int(h / 2.0 - shorter_size / 2.0) # type: int
y2 = int(h / 2.0 + shorter_size / 2.0) # type: int
return '%d,%d,%d,%d' % (x1, y1, x2, y2)
示例5: clean
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def clean(self):
super(AdImageInlineForm, self).clean()
device = self.cleaned_data.get('device', None)
image = self.cleaned_data.get('image', None)
ad = self.cleaned_data.get('ad', None)
if image and device and ad:
allowed_size = settings.ADS_ZONES.get(ad.zone, {}). \
get('ad_size', {}). \
get(device, settings.ADS_DEFAULT_AD_SIZE)
allowed_w, allowed_h = [int(d) for d in allowed_size.split('x')]
w, h = get_image_dimensions(image)
if w != allowed_w or h != allowed_h:
self.add_error(
'image', _('Image size must be %(size)s') % {
'size': allowed_size, })
示例6: _calc_aspect_ratio
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def _calc_aspect_ratio(image):
width, height = get_image_dimensions(image)
aspect_ratio = height / width
ar_percent = aspect_ratio * 100
if ar_percent.is_integer():
ar_percent = int(ar_percent)
else:
ar_percent = round(ar_percent, 2)
return ar_percent
示例7: image_dimensions_validator
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def image_dimensions_validator(value):
"""
Validates dimensions of uploaded image.
Avatar images should be in square aspect ratios and have size of less than 640*640.
"""
w, h = get_image_dimensions(value)
if w != h:
raise forms.ValidationError(u'Avatar image should have same height and width.')
elif w > AVATAR_IMAGE_MAX_DIMENSION or h > AVATAR_IMAGE_MAX_DIMENSION:
raise forms.ValidationError(u'Avatar image width and height should be less than {0} pixels.'
.format(AVATAR_IMAGE_MAX_DIMENSION))
elif w < AVATAR_IMAGE_MIN_DIMENSION or h < AVATAR_IMAGE_MIN_DIMENSION:
raise forms.ValidationError(u'Avatar image width and height should be greater than {0} pixels.'
.format(AVATAR_IMAGE_MIN_DIMENSION))
示例8: test_not_closing_of_files
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def test_not_closing_of_files(self):
"""
Open files passed into get_image_dimensions() should stay opened.
"""
empty_io = BytesIO()
try:
images.get_image_dimensions(empty_io)
finally:
self.assertTrue(not empty_io.closed)
示例9: test_closing_of_filenames
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def test_closing_of_filenames(self):
"""
get_image_dimensions() called with a filename should closed the file.
"""
# We need to inject a modified open() builtin into the images module
# that checks if the file was closed properly if the function is
# called with a filename instead of a file object.
# get_image_dimensions will call our catching_open instead of the
# regular builtin one.
class FileWrapper:
_closed = []
def __init__(self, f):
self.f = f
def __getattr__(self, name):
return getattr(self.f, name)
def close(self):
self._closed.append(True)
self.f.close()
def catching_open(*args):
return FileWrapper(open(*args))
images.open = catching_open
try:
images.get_image_dimensions(os.path.join(os.path.dirname(__file__), "test1.png"))
finally:
del images.open
self.assertTrue(FileWrapper._closed)
示例10: test_multiple_calls
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def test_multiple_calls(self):
"""
Multiple calls of get_image_dimensions() should return the same size.
"""
img_path = os.path.join(os.path.dirname(__file__), "test.png")
with open(img_path, 'rb') as fh:
image = images.ImageFile(fh)
image_pil = Image.open(fh)
size_1 = images.get_image_dimensions(image)
size_2 = images.get_image_dimensions(image)
self.assertEqual(image_pil.size, size_1)
self.assertEqual(size_1, size_2)
示例11: test_bug_19457
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def test_bug_19457(self):
"""
Regression test for #19457
get_image_dimensions fails on some pngs, while Image.size is working good on them
"""
img_path = os.path.join(os.path.dirname(__file__), "magic.png")
size = images.get_image_dimensions(img_path)
with open(img_path, 'rb') as fh:
self.assertEqual(size, Image.open(fh).size)
示例12: test_invalid_image
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def test_invalid_image(self):
"""
get_image_dimensions() should return (None, None) for the dimensions of
invalid images (#24441).
brokenimg.png is not a valid image and it has been generated by:
$ echo "123" > brokenimg.png
"""
img_path = os.path.join(os.path.dirname(__file__), "brokenimg.png")
with open(img_path, 'rb') as fh:
size = images.get_image_dimensions(fh)
self.assertEqual(size, (None, None))
示例13: clean_picture
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def clean_picture(self):
"""
Implements validation of new user picture uploaded
"""
picture = self.cleaned_data.get('picture')
# New picture has been uploaded
if picture and isinstance(picture, UploadedFile):
# Validate content type
main, sub = picture.content_type.split('/')
if not (main == 'image' and sub in ['jpeg', 'pjpeg', 'gif', 'png']):
raise forms.ValidationError(
_('Please use a JPEG, GIF or PNG image.'))
w, h = get_image_dimensions(picture)
# Validate picture dimensions
max_width = max_height = 1024
if w > max_width or h > max_height:
raise forms.ValidationError(_(
'Please use an image that is '
'%(max_width)sx%(max_height)s pixels or smaller.'
) % {'max_width': max_width, 'max_height': max_height})
# Validate file size
if len(picture) > (500 * 1024):
raise forms.ValidationError(
_('User picture size may not exceed 500 kB.'))
return picture
示例14: get_image_resolution
# 需要导入模块: from django.core.files import images [as 别名]
# 或者: from django.core.files.images import get_image_dimensions [as 别名]
def get_image_resolution(image):
try:
w, h = image.w, image.h
if not (w and h):
w, h = get_image_dimensions(image.image_file)
except TypeError:
# This might happen in unit tests
w, h = 0, 0
return w, h