> ## Documentation Index
> Fetch the complete documentation index at: https://cloudinary.com/documentation/llms.txt
> Use this file to discover all available pages before exploring further.

# Crop and resize images in Python (video tutorial)

## Overview

Learn how to dynamically generate images at different sizes and aspect ratios, and dynamically crop with AI in Python using Cloudinary.

## Video tutorial

  This video is brought to you by Cloudinary's video player - embed your own!Use the controls to set the playback speed, navigate to chapters of interest and select subtitles in your preferred language.

## Tutorial contents
This tutorial presents the following topics. Click a timestamp to jump to that part of the video.
### Managing image delivery
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=00 :player=cld} | Delivering images at full size can lead to longer loading times and increased bandwidth usage. The Cloudinary Python SDK allows you to efficiently [resize or crop](resizing_and_cropping) images to optimize performance. Additionally, you can programmatically change the aspect ratio while ensuring that the most important parts of the image remain in focus. |

### Generating image URLs
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=20 :player=cld} | Assuming you have Cloudinary configured, import the `CloudinaryImage` method. Then, use this method and pass it the public ID of an image to obtain its delivery URL. Include the `'format': 'auto'` and `'quality': 'auto'` parameters to ensure optimal delivery for different browsers.

```python
from cloudinary import CloudinaryImage

optimized_only = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'}
  ])
```

### Resizing using HTML 
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=0 :sec=48 :player=cld} | Open the image resized with HTML. In Dev Tools, you'll notice that the image is delivered at its original large size, even though it fits within the declared dimensions. This results in unnecessary bandwidth usage.

### Resizing images with Cloudinary
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=1 :sec=10 :player=cld} | To reduce bandwidth and loading times, resize the actual image by adding the `c_scale` parameter to the Cloudinary image method, adjusting the width to 600 pixels and significantly reducing the file size. Notice in Dev Tools the image is now significantly resized.

```python
scale_only = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'},
  {'crop': 'scale', 'width': 600}
  ])
```

### Cropping images for aspect ratio
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=1 :sec=31 :player=cld} | For specific aspect ratios, change the method from `'crop': 'scale'` to `'crop': 'crop'` and define dimensions (e.g., 2000 x 2000 pixels). Use the `'gravity': 'auto'` parameter to ensure the crop focuses on important areas, like a face.

```python
crop_with_gravity = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'},
  {'crop': 'crop', 'gravity': 'auto', 'width': 2000, 'height': 2000}
  ])
```

### Further scaling cropped images
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=2 :sec=04 :player=cld} | To further reduce size, chain transformations by adding `'crop': 'scale'` and specifying a width of 600 pixels. This approach provides flexibility and control over the final image dimensions, ensuring it meets your design needs while remaining as small as possible.

```python
crop_with_gravity_and_scale = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'},
  {'crop': 'crop', 'gravity': 'auto', 'width': 2000, 'height': 2000},
  {'crop': 'scale', 'width': 600}
  ])
```

### Exploring different cropping techniques
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=2 :sec=30 :player=cld} | You can crop to different dimensions to zoom in or out. For example, cropping to 3000 x 3000 pixels and then scaling down or using `'crop': 'auto'` to automatically determine the best crop based on the subject's position.

```python
large_crop = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'},
  {'crop': 'crop', 'gravity': 'auto', 'width': 3000, 'height': 3000},
  {'crop': 'scale', 'width': 600}
  ])

crop_and_gravity_auto = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'},
  {'crop': 'auto', 'gravity': 'auto', 'width': 600, 'height': 600}
  ])
```

### Automatic scaling with the fill cropping mode
{table:class=tutorial-bullets}|  | 
| --- | --- |
|{videotime:id=media :min=3 :sec=30 :player=cld} | The `fill` cropping mode automatically scales the image to fit specified dimensions while cropping as necessary, ensuring the final image is both visually appealing and appropriately sized.

```python
crop_fill = CloudinaryImage('moving-day').build_url(transformation=[
  {'format': 'auto', 'quality': 'auto'},
  {'crop': 'fill', 'width': 600, 'height': 600}
  ])
```

## Keep learning

> **READING**:
>
> * Watch more [Dev Hints videos](https://www.youtube.com/playlist?list=PL8dVGjLA2oMpaTbvoKCaRNBMQzBUIv7N8) on the [Cloudinary YouTube channel](https://www.youtube.com/cloudinary).

> * Learn more about [cropping images using automatic gravity](resizing_and_cropping#automatic_cropping_g_auto).

> * Learn more about the [Python SDK](django_integration).

> * Discover more transformations in the [Transformation URL API reference](transformation_reference).

> * Learn how cropping and resizing images is essential to [e-commerce workflows](ecommerce_workflows_product#step_3).

#### If you like this, you might also like...

  
  
  
    Transformation Basics
    Learn the basics of a transformation URL 
  

  
  
  
    Gravity Crops for Images
    Indicate which areas to keep when cropping 
  

  
  
  
    Manage Images in a Django App
    Use Django helper methods to upload, transform, and display assets. 
  

&nbsp;

&nbsp;Check out the Cloudinary Academy for free self-paced Cloudinary courses on a variety of developer or DAM topics, or register for formal instructor-led courses, either virtual or on-site.
&nbsp;
