-
Notifications
You must be signed in to change notification settings - Fork 11
Optimize or resize images
↑ Parent: Python utils
← Previous: Access MagiCollections
If you use MagiModel (or BaseModel
), you can compress images 2 ways:
- Asynchronously and by keeping the original image (recommended):
- On upload, and the original image is lost:
In both cases, you can specify some compression settings in your model with the variable tinypng_settings
.
Settings can be either:
-
{ 'resize': 'cover' }
or{ 'resize': 'thumb' }
, with default values{ 'width': 200, 'height': 200 }
-
{ 'resize': 'fit' }
, with default values{ 'max_width': MAX_WIDTH, 'max_height': MAX_HEIGHT, 'min_width': MIN_WIDTH, 'min_height': MIN_HEIGHT }
(default values in your django settings)
See TinyPNG's documentation for more details about how images get resized.
TinyPNG has a cost per API call, so it's better to only use it for images that will get displayed very often. For other images, including user-uploaded content, it's recommended to generate thumbnails without TinyPNG.
Thumbnail settings can be specified in your model with the variable thumbnail_size
.
-
{ 'resize': 'thumb' }
or{ 'resize': 'cover' }
, with default values{ 'width': 200, 'height': 200 }
- Will make a square thumbnail by cropping out any extra.
-
{ 'resize': 'fit' }
or{ 'resize': 'scale'}
, with default values{ 'width': 200, 'height': 200 }
- Will resize an image to fit within the given width and height but keep original ratio. Doesn't add extra pixels, so the resulting image will not be always be the given size.
By default, 'resize' is set to 'thumb'.
It's not recommended, but if for some reason, you need to do compress images manually, you can use shrinkImageFromData
:
from magi.utils import shrinkImageFromData
f = open('/tmp/image.png', 'r+')
image = shrinkImageFromData(f.read(), 'image.png')
You can also provide some settings:
image = shrinkImageFromData(f.read(), 'image.png', settings={
'resize': 'cover',
'width': 200,
'height': 200,
})
The following utils functions all return a Django ImageFile
and take as parameters:
- Image data
- Filename
- Some settings, see per function
-
return_data
, when set toTrue
, will return the resulting image data in addition to theImageFile
-
return_pil_image
, when set toTrue
, will return theImage
fromPIL
library in addition to theImageFile
from magi.utils import imageSquareThumbnailFromData
f = open('image.png', 'r+')
idol.image = imageSquareThumbnailFromData(f.read(), 'maki.png', size=250)
idol.save()
Function name | Description | Settings | Example |
---|---|---|---|
Original image: |
|||
imageSquareThumbnailFromData |
Will make a square thumbnail by cropping out any extra | size=200 |
|
imageThumbnailFromData |
Will resize an image to fit within the given width and height but keep original ratio. Doesn't add extra pixels, so the resulting image will not be always be the given size. | width=200, height=200 |
|
imageResizeScaleFromData |
Same as imageThumbnailFromData but resulting width/height is calculated by MagiCircles. I'm honestly not sure why I wrote this function manually. |
width=200, height=200 |
|
imageResizeFromData |
Will resize the image to the given width and height, stretching it if needed. | width=200, height=200 |
|
→ Next: Validators
I. Introduction
II. Tutorials
III. References
- Files tree
- Default collections
- Default pages
- Website settings
- MagiCollection settings
- Abstract models
- Abstract collections
- MagiForm settings
- MagiFiltersForm settings
- Single pages settings
IV. Utils
-
MagiModel utils
- MagiModel images and files
- DateTime fields
- BaseAccount model
- Save choices values as integer rather than strings
- Store comma separated values
- Store dictionaries
- Store Markdown texts
- Translate fields values in multiple languages
- Store JSON
- More model field types
- Transform images before saving them
- Check choices at form level instead of model level
- Use an internal cache for foreign keys in models
- Customize views with MagiModel properties
- Events
- Form utils
- Python utils
- Templates utils
- Javascript utils
- Enable and disable existing collections and pages
- Translations
- Page titles and descriptions
- Characters
- Seasons
- Roles and permissions
- Allow your staff team to change configurations
- Custom users preferences and settings
- Background illustrations
- Activities tabs
- Corner popups
VI. More