Libraries
imgix-python build
- Installation
- Usage
- Signed URLs
- Disabled Path Encoding
- Srcset Generation
- The
ixlib
Parameter - Testing
- License
Installation
Usage
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 use_https
to False
:
Signed URLs
To produce a signed URL, you must enable secure URLs on your source and then provide your signature key to the URL builder.
Disabled Path Encoding
Path encoding is enabled by default. It can be toggled off by setting disable_path_encoding
to True
in the optional options
paramater in create_url()
and create_srcset()
functions:
Normally this would output a source URL like https://demo.imgix.net/%20%3C%3E%5B%5D%7B%7D%7C%5E%25.jpg?h=100&2=100
, but since path encoding is disabled, it will output a source URL like https://sdk-test.imgix.net/ <>[]{}|^%.jpg?h=100&w=100
.
Normally this would output a source URL like https://sdk-test.imgix.net/image%3C%3E%5B%5D%7B%7D%20123.png?&w=100 100w
, but since path encoding is disabled, it will output a source URL like https://sdk-test.imgix.net//image<>[]{} 123.png?w=100 100w
.
Srcset Generation
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:
Fixed-Width Images
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 w
, h
, and ar
.
By invoking 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:
By default, this library generates a srcset
with pixel density values of 1
through 5
. These target ratios can be controlled by using the devicePixelRatios
parameters.
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.
Variable Quality
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 create_srcset
.
This behavior specifically occurs when a fixed-width image is rendered, for example:
The above will generate a srcset with the following q
to dpr
query params
:
By default, this library will automatically append a variable q
parameter mapped to each dpr
parameter when generating a fixed-width image srcset.
To customize variable qualities, you can pass a variable_qualities
dictionary in the options
while creating srcset as below:
The above script will produce the following output:
You can also pass variable_qualities
along with the device_pixel_ratios
option as below:
The above script will produce the following output:
Fluid-Width Images
Custom Widths
In situations where specific widths are desired when generating srcset
pairs, a user can specify them by passing an array of positive integers as widths
:
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 tol
erance are passed to the create_srcset
method, the custom widths list will take precedence.
Width Ranges
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 start
and stop
:
Formatted version of the above srcset attribute:
Width Tolerance
The srcset
width tol
erance dictates the maximum tol
erated 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 tol
erance 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 tol
erance:
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:
Explore Target Widths
The target_widths
function is used internally to generate lists of target widths to be used in calls to create_srcset
.
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 start
, stop
, and tol
erance are known in advance. Another approach is to use target_widths
to determine which combination of values for start
, stop
, and tol
erance work best.
Usage with UTF-8
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:
The ixlib
Parameter
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 include_library_param
to False
like so:
Testing
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.
License
More Core Libraries
- imgix-csharpbuildA C# client library for generating image URLs with imgixC# 14 a year ago
- imgix-gobuildA Go client library for generating image URLs with imgixGo 12 a year ago
- imgix-javabuildA Java client library for generating URLs with imgixJava 19 a year ago
- imgix-phpbuildA PHP client library for generating URLs with imgixPHP 111 a year ago
- imgix-rbbuildA Ruby gem for generating image URLs with imgixRuby 76 a year ago
- imgix-swiftbuildA Swift client library for generating URLs with imgixSwift 25 8 months ago
- @imgix/js-corebuildA JavaScript client library for generating image URLs with imgixJavaScript 120 < 1 day ago