- Signed URLs
- Srcset Generation
To begin creating imgix URLs, import the imgix library and create a URL builder. The URL builder can be reused to create URLs for any images on the domains it is provided.
HTTPS support is enabled by default. HTTP can be toggled on by setting
To produce a signed URL, you must enable secure URLs on your source and then provide your signature key to the URL builder.
The imgix-python package allows for generation of custom srcset attributes, which can be invoked through the
create_srcset method. By default, the generated srcset will allow for responsive size switching by building a list of image-width mappings.
The above will produce the following srcset attribute value which can then be served to the client:
In cases where enough information is provided about an image's dimensions,
create_srcset will instead build a srcset that will allow for an image to be served at different resolutions. The parameters taken into consideration when determining if an image is fixed-width are
create_srcset with either a width or the height and aspect ratio (along with
fit=crop, typically) provided, a different srcset will be generated for a fixed-width image instead.
Will produce the following attribute value:
For more information to better understand srcset, we highly recommend Eric Portis' "Srcset and sizes" article which goes into depth about the subject.
This library will automatically append a variable
q parameter mapped to each
dpr parameter when generating a fixed-width image srcset. This technique is commonly used to compensate for the increased file size of high-DPR images.
Since high-DPR images are displayed at a higher pixel density on devices, image quality can be lowered to reduce overall file size––without sacrificing perceived visual quality. For more information and examples of this technique in action, see this blog post.
This behavior will respect any overriding
q value passed in as a parameter. Additionally, it can be disabled altogether by passing
disable_variable_quality = true to
This behavior specifically occurs when a fixed-width image is rendered, for example:
The above will generate a srcset with the following
In situations where specific widths are desired when generating
srcset pairs, a user can specify them by passing an array of positive integers as
Note: in situations where a
srcset is being rendered as a fixed-width srcset, any custom
widths passed in will be ignored.
Additionally, if both
widths and a width
tolerance are passed to the
create_srcset method, the custom widths list will take precedence.
In certain circumstances, you may want to limit the minimum or maximum value of the non-fixed
srcset generated by the
create_srcset method. To do this, you can specify the widths at which a srcset should
Formatted version of the above srcset attribute:
tolerance dictates the maximum
tolerated difference between an image's downloaded size and its rendered size.
For example, setting this value to
0.10 means that an image will not render more than 10% larger or smaller than its native size. In practice, the image URLs generated for a width-based srcset attribute will grow by twice this rate.
A lower tolerance means images will render closer to their native size (thereby increasing perceived image quality), but a large srcset list will be generated and consequently users may experience lower rates of cache-hit for pre-rendered images on your site.
By default, srcset width
tolerance is set to 0.08 (8 percent), which we consider to be the ideal rate for maximizing cache hits without sacrificing visual quality. Users can specify their own width tolerance by providing a positive scalar value as width
In this case, the
width_tolerance is set to 20 percent, which will be reflected in the difference between subsequent widths in a srcset pair:
target_widths function is used internally to generate lists of target widths to be used in calls to
It is a way to generate, play with, and explore different target widths separately from srcset attributes. One way of generating a srcset attribute is:
The above is convenient if
tolerance are known in advance. Another approach is to use
target_widths to determine which combination of values for
tolerance work best.
For usage with non-ASCII characters, please be sure that your project's source files specify UTF-8 encoding:
If you don't add this encoding, and you have an image with the name 'tiburón.jpeg', for example, you will get the following error trying to run your script:
For security and diagnostic purposes, we sign all requests with the language and version of library used to generate the URL.
This can be disabled by setting
False like so:
Run the following to execute the project's tests and code linter:
If you have cloned this repo or downloaded it locally, you can also run
python -m doctest -v README.md to test the examples in this readme.
More Core Libraries
- imgix-csharpbuildA C# client library for generating image URLs with imgixC# 12 12 days ago
- imgix-elixirbuildAn Elixir client library for generating image URLs with imgixElixir 30 6 months ago
- imgix-gobuildA Go client library for generating image URLs with imgixGo 2 2 days ago
- imgix-javabuildA Java client library for generating URLs with imgixJava 14 12 days ago
- imgix-phpbuildA PHP client library for generating URLs with imgixPHP 93 12 days ago
- imgix-rbbuildA Ruby gem for generating image URLs with imgixRuby 68 < 1 day ago
- imgix-swiftbuildA Swift client library for generating URLs with imgixSwift 19 23 days ago