Skip to content

Commit 6b770df

Browse files
committed
Update documentation for ImageBatch API
- Update API docs with correct ImageBatch usage and benefits - Add comprehensive batch processing section to advanced usage - Update getting started guide with modern batch examples - Document auto-building, explicit building, and is_built property
1 parent c8d289f commit 6b770df

File tree

3 files changed

+127
-17
lines changed

3 files changed

+127
-17
lines changed

docs/advanced-usage.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,72 @@ class MyAppConfig(AppConfig):
3535
thumbnail.queue(Profile, build="src")
3636
```
3737

38+
## Batch Processing
39+
40+
When rendering multiple images (e.g., in a list view), use `ImageBatch` to optimize database queries:
41+
42+
```python
43+
from easy_images import ImageBatch, Img
44+
45+
def product_list_view(request):
46+
products = Product.objects.all()
47+
48+
# Create a batch for efficient processing
49+
batch = ImageBatch()
50+
thumbnail = Img(batch=batch, width=300, format="webp")
51+
52+
# Process all images
53+
product_images = []
54+
for product in products:
55+
bound_img = thumbnail(product.image, alt=product.name)
56+
product_images.append({
57+
'product': product,
58+
'image': bound_img
59+
})
60+
61+
# First access triggers batch loading of all images
62+
# Subsequent accesses use cached data
63+
return render(request, 'products.html', {
64+
'product_images': product_images
65+
})
66+
```
67+
68+
### Benefits of Batching
69+
70+
1. **Reduced Database Queries**: One query loads all image metadata instead of N queries
71+
2. **Shared Processing**: Images with the same source share processing results
72+
3. **Memory Efficiency**: Fresh batches prevent memory accumulation
73+
4. **Incremental Loading**: Can add images to already-loaded batches
74+
75+
### Batch Building Strategies
76+
77+
```python
78+
# Option 1: Automatic building on property access (default)
79+
batch = ImageBatch()
80+
img = Img(batch=batch, width=300)
81+
bound = img(file, build="src")
82+
url = bound.base_url() # Triggers auto-build
83+
84+
# Option 2: Explicit batch building
85+
batch = ImageBatch()
86+
img = Img(batch=batch, width=300)
87+
bound1 = img(file1, build="src")
88+
bound2 = img(file2, build="srcset")
89+
batch.build() # Build all at once
90+
91+
# Option 3: Immediate building (for signals/queue)
92+
img = Img(width=300)
93+
bound = img(file, build="src", immediate=True)
94+
```
95+
3896
## Performance Tips
3997

4098
1. For high-traffic sites, use `build="src"` to generate base images immediately
41-
2. Set up Celery for distributed image processing
42-
3. Use `format="webp"` for best compression/performance balance
43-
4. Limit `densities` to `[2]` unless high-DPI support is critical (or turn it off entirely)
44-
5. Consider pre-generating common image sizes during deployment
99+
2. Use `ImageBatch` when rendering lists of images
100+
3. Set up Celery for distributed image processing
101+
4. Use `format="webp"` for best compression/performance balance
102+
5. Limit `densities` to `[2]` unless high-DPI support is critical (or turn it off entirely)
103+
6. Consider pre-generating common image sizes during deployment
45104

46105
## Signals
47106

docs/api.md

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,55 @@ img.queue(MyModel) # Processes all ImageFields on MyModel
2525
```
2626

2727
#### `ImageBatch` - Batch Processing
28+
29+
The `ImageBatch` class optimizes database queries when processing multiple images:
30+
2831
```python
29-
from easy_images import ImageBatch
32+
from easy_images import ImageBatch, Img
3033

34+
# Create a shared batch for efficient processing
3135
batch = ImageBatch()
32-
img1 = batch.add(source_file=model1.image_field, options={'width': 800})
33-
img2 = batch.add(source_file=model2.image_field, options={'width': 600})
3436

35-
# Get HTML for all images
36-
html1 = img1.as_html()
37-
html2 = img2.as_html()
37+
# Create image configurations with the batch
38+
img = Img(batch=batch, width=800, format="webp")
39+
thumbnail = Img(batch=batch, width=200, format="jpg")
40+
41+
# Add images to the batch
42+
bound1 = img(model1.image_field, alt="Main image")
43+
bound2 = thumbnail(model2.image_field, alt="Thumbnail")
44+
45+
# Access images - batch loading happens automatically
46+
html1 = bound1.as_html() # First access triggers batch loading
47+
html2 = bound2.as_html() # Already loaded, no extra queries
48+
49+
# Or explicitly build all images in the batch
50+
batch.build()
3851
```
3952

53+
**Key benefits:**
54+
- Batches database queries for multiple images
55+
- Shares loaded image data across all items in the batch
56+
- Supports incremental loading when adding images to an already-loaded batch
57+
- Automatic lazy building when properties are accessed
58+
4059
#### `BoundImg` - Processed Image
41-
```python
42-
# Get image URLs
43-
main_url = processed_img.base_url()
44-
srcset = processed_img.srcset # List of available sizes
4560

46-
# Build images immediately (instead of queue)
47-
processed_img.build('all') # Options: 'all', 'base', 'srcset'
61+
A `BoundImg` represents a single image item within a batch:
62+
63+
```python
64+
# Properties (trigger auto-building if needed)
65+
main_url = bound_img.base_url() # URL of the base image
66+
srcset_items = bound_img.srcset # List of SrcSetItem objects
67+
alt_text = bound_img.alt # Alt text
68+
sizes_attr = bound_img.sizes # Sizes attribute for responsive images
69+
html = bound_img.as_html() # Complete <img> tag
70+
71+
# Check if images are built
72+
if bound_img.is_built:
73+
print("Images are ready")
74+
75+
# Manually trigger building
76+
bound_img.build('all') # Options: 'all', 'src', 'srcset'
4877
```
4978

5079
### `easy_images.engine`

docs/getting-started.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,29 @@ thumb = Img(width="md")
3434
html = thumb(profile.photo, alt="Profile photo").as_html()
3535
```
3636

37-
If you're going to be building several images, consider using the [`ImageBatch`](api.md#imagebatch) class to process them in bulk.
37+
### Batch Processing for Multiple Images
38+
39+
When working with multiple images, use `ImageBatch` for better performance:
40+
41+
```python
42+
from easy_images import Img, ImageBatch
43+
44+
# Create a batch for efficient processing
45+
batch = ImageBatch()
46+
thumb = Img(batch=batch, width="md")
47+
48+
# Process multiple images
49+
images = []
50+
for profile in profiles:
51+
bound_img = thumb(profile.photo, alt=f"{profile.name}'s photo")
52+
images.append(bound_img)
53+
54+
# First access loads all images in one query
55+
for img in images:
56+
print(img.as_html())
57+
```
58+
59+
See the [API documentation](api.md#imagebatch-batch-processing) for more details.
3860

3961
### Using template tags
4062
```html

0 commit comments

Comments
 (0)