Skip to content

Optimize or resize images

deby edited this page Feb 14, 2023 · 5 revisions

↑ Parent: Python utils

← Previous: Access MagiCollections

If you use MagiModel (or BaseModel), you can compress images 2 ways:

With TinyPNG

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)

TinyPNG resize settings

See TinyPNG's documentation for more details about how images get resized.

Without TinyPNG

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'.

Use TinyPNG manually

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,
})

Generate thumbnails manually (without TinyPNG)

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 to True, will return the resulting image data in addition to the ImageFile
  • return_pil_image, when set to True, will return the Image from PIL library in addition to the ImageFile
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: 1386x640
imageSquareThumbnailFromData Will make a square thumbnail by cropping out any extra size=200 250x250
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 250x115
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 250x115
imageResizeFromData Will resize the image to the given width and height, stretching it if needed. width=200, height=200 250x250

→ Next: Validators

I. Introduction

II. Tutorials

  1. Collections
    1. MagiModel
    2. MagiCollection
    3. MagiForm
    4. MagiFiltersForm
    5. MagiFields
  2. Single pages
  3. Configuring the navbar

III. References

IV. Utils

V. Advanced tutorials

VI. More

Clone this wiki locally