Programmable Media

Python image and video upload

Last updated: Jun-25-2024

Cloudinary provides an API for uploading images, videos, and any other kind of file to the cloud. Files uploaded to Cloudinary are stored safely in the cloud with secure backups and revision history. Cloudinary's APIs allow secure uploading from your servers, directly from your visitors' browsers or mobile applications, or fetched via remote public URLs.

Cloudinary's Python SDK wraps Cloudinary's upload API and simplifies the integration. Python methods are available for easily performing Python image and video uploads to the cloud and Python view helper methods are available for uploading directly from a browser to Cloudinary.

This page covers common usage patterns for Python image and video upload with Cloudinary.

For details on all available upload functionality, see the Upload guide, and the upload method of the Upload API Reference.

Note
Most of the functionality provided by Cloudinary can be implemented using Python, regardless of your framework. Some features are only available with Django, as described in the documentation.

Tip
Cloudinary's Upload widget provides an alternative to using a Cloudinary SDK to add upload functionality to your application, eliminating the need to develop in-house interactive upload capabilities. The upload widget is an interactive, feature rich, simple-to-integrate user interface that enables you to add Cloudinary upload support to your website. The widget can be easily embedded in your web application with just a few lines of JavaScript code. See the Upload widget documentation for detailed information.

Upload widget main screen

Server-side upload

You can upload images, videos, or any other raw file to Cloudinary from your Python code. Uploading is done over HTTPS using a secure protocol based on your product environment's api_key and api_secret parameters.

Python image upload

The following method uploads an image to the cloud:

For example, uploading a local image file named 'my_image.jpg':

Here's a more advanced example. To upload a remote image file, apply transformations including cropping to a square with automatic crop and gravity, as well as applying a special effect to enhance the image, then store the transformed image under the public ID file_wine:

In addition to a local path and a remote HTTP or HTTPS URL, the file to upload can also be specified as a whitelisted storage bucket (S3 or Google Storage) URL, a data stream, a base64 data URI, or an FTP URL. For details and code examples of uploading using each of these data source types, see Required upload parameters.

For details on all available upload functionality, see the Upload guide, and the upload method of the Upload API Reference.

Image upload tutorial

Watch this video tutorial to see how to upload images to Cloudinary using the Python SDK:


Python video upload

You upload videos in the same way as images. However, with videos, you must specify the resource type as 'video' within the upload method. In addition, the upload method supports uploading files only up to 100 MB. To upload larger videos, use the upload_large method, which uploads large files to the cloud in chunks.

The upload_large method has the identical signature and options as the upload method, with the addition of an optional chunk_size parameter (default 20 MB).

The following example uploads dog.mp4 to Cloudinary and stores it with the public ID dog_closeup. It also performs two eager transformations that resize the video to a square and a small rectangle.

Video upload tutorial

Watch a video tutorial demonstrating how to upload small and large videos. You can find the code from the tutorial in GitHub.

Upload response

By default, uploading is performed synchronously. Once finished, the uploaded image or video is immediately available for transformation and delivery. An upload call returns a Hash with content similar to the following:

Note
If you need the upload response to return the actual image instead of a Hash then use the upload_resource method (which is identical to the upload method except for the response).

The response includes HTTP and HTTPS URLs for accessing the uploaded media asset as well as additional information regarding the uploaded asset: public ID, resource type, width and height, file format, file size in bytes, a signature for verifying the response and more.

File sources

Cloudinary's Python library supports uploading files from various sources.

  • You can upload an asset by specifying a local path of an asset. For example:

  • You can provide an IO object that you created:

  • If your assets are already publicly available online, you can specify their remote HTTP URLs instead of uploading the actual data. In this case, Cloudinary will fetch the asset from its remote URL for you. This option allows for a much faster migration of your existing assets. Here's an example:

  • If you have existing assets in an Amazon S3 bucket, you can point Cloudinary to their S3 URLs. Note - this option requires a quick manual setup. Contact us and we'll guide you on how to allow Cloudinary access to your relevant S3 buckets.

    Note
    If you are coding an application in Django where users upload assets through a web form, you can pass the parameter of your Django's request.FILES to the upload method:

For more information on file sources for upload, see File sources.

Django forms and models workflow

If you're using Django, you can seamlessly integrate Cloudinary's capabilities into your forms and models using Cloudinary's helper classes. This approach ensures that you can continue your standard workflow of saving delivery URLs directly into your database during uploads, while also enabling the delivery of optimized and transformed assets on your website or app with Cloudinary's features.

As shown in the example below, you can define a model class Photo in your models.py file. This class has an image field of the CloudinaryField class.

In the forms.py file we define a PhotoForm class that has a form field named image of the CloudinaryFileField class (by default).

The views.py file defines a view named upload which displays an HTML upload form and also handles posting of image files. Such images are uploaded to Cloudinary from your Django server by the CloudinaryFileField class.

The following HTML templates includes a form that uploads images to your server for uploading to Cloudinary:

Having stored the image ID, you can now embed the image or a transformed version of it using the cloudinary template tag:

In addition, you can assign tags, apply transformation or specify any Cloudinary's upload options when initializing the CloudinaryFileField class. The assets will be saved in the transformed state as specified by the provided upload options.

Manage images in a Django app video tutorial

Watch this video tutorial to see how to integrate Cloudinary in a Django app using the forms and modes helper methods:


This video is brought to you by Cloudinary's video player - embed your own!

View the code
You can find the code from this tutorial in GitHub.
Note
You can no longer access your full credentials directly from the Dashboard. Find your Cloud name on the Dashboard, and all credentials, including API Key, API Secret, and API environment variable, on the API Keys page of the Cloudinary Console Settings.

Important
When writing your own applications, follow your organization's policy on storing secrets and don't expose your API secret.

Modify the models.py file

Jump to this spot in the video  2:32 In the models.py file, replace the Django image field image = models.ImageField(upload_to='photos/') with the CloudinaryField. This grants access to Cloudinary functionalities, including uploading to the cloud and storing image delivery URLs in the database.

Adjust the forms.py file

Jump to this spot in the video  3:00 Within the forms.py file, import and use the CloudinaryFileField for file upload handling and Cloudinary storage.

views.py remains unchanged

Jump to this spot in the video  3:18 No modifications are needed in the views.py file. The CloudinaryField addition in models.py file ensures that valid upload submissions are saved to Cloudinary and their URLs are saved in the database.

The display function in views.py retrieves all Photos table images and displays them using the display.html template.

Update the display.html file

Jump to this spot in the video  3:47 Replace the HTML image tag <img src="{{ photo.image.url }}" alt="Image"> with Cloudinary's helper tags for image display.

Transform and optimize images

Jump to this spot in the video  4:03 The Cloudinary helper methods have already streamlined upload, storage and image retrieval and display. Now, apply a transformation to ensure your images fit the desired area, utilizing AI for outpainting and optimizing to reduce their file sizes.

Note
The ignore-foreground option is not used in the latest version of the generative fill feature.

Previewing the app with Cloudinary integration

Jump to this spot in the video  4:51 Re-run the app to see the effects of Cloudinary integration. Images now fit their frames without distortion because Cloudinary's AI expands narrow images to fill the specified area perfectly. Additionally, confirm reduced image sizes using the Media Inspector.

Direct uploading from the browser

The upload samples shown above allow your server-side Python code to upload media assets to Cloudinary. In this flow, if you have a web form that allows your users to upload images or videos, the media file's data is first sent to your server and then uploaded to Cloudinary.

A more efficient and powerful option is to allow your users to upload images and videos in your client-side code directly from the browser to Cloudinary instead of going through your servers. This method allows for faster uploading and a better user experience. It also reduces load from your servers and reduces the complexity of your Python applications.

You can upload files directly from the browser using signed or unsigned calls to the upload endpoint, as shown in the Upload multiple files using a form examples.

For signed uploads from your client-side code, a secure signature must be generated in your server-side Python code. You can use the api_sign_request method to generate SHA signatures:

For the full list of parameters available for signed uploads, see the upload method in the Upload API Reference.

Note
For security reasons, only this limited set of parameters can be used in an unsigned upload request.

Related topics
  • For more information on uploading media assets, see the Upload guide.
  • For details on all available upload parameters, see the upload method of the Upload API Reference.