Thumbnails
The Thumbnails API provides functionality for generating, managing, and retrieving thumbnail images for files in Anchorpoint projects. Thumbnails improve browsing experience and provide visual previews of assets without opening the full files.
Usage
Thumbnail functions are accessed directly from the apsync module:
import apsync
import anchorpoint
ctx = anchorpoint.get_context()
# Generate thumbnail for a file
success = apsync.generate_thumbnail(
ctx.path,
"/output/directory",
with_preview=True,
with_detail=False
)
# Attach custom thumbnail
apsync.attach_thumbnail(ctx.path, "/path/to/custom_thumb.jpg", is_detail=False)
# Get existing thumbnail
thumb_path = apsync.get_thumbnail(ctx.path, is_detail=False)
Functions
-
generate_thumbnail(file_path, output_directory, with_preview=True, with_detail=False, workspace_id=None)Generates thumbnail images for a file automatically using Anchorpoint's built-in thumbnail generators.Arguments
file_path(str): Absolute path to the source fileoutput_directory(str): Directory where thumbnails should be savedwith_preview(bool, optional): Generate preview-sized thumbnail (default: True)with_detail(bool, optional): Generate detail-sized thumbnail (default: False)workspace_id(str, optional): Workspace ID for context (uses current if not provided)
Returns (bool): True if thumbnail generation succeeded, False otherwise
-
generate_thumbnails(file_paths, output_directory, with_preview=True, with_detail=False, workspace_id=None)Generates thumbnails for multiple files in batch mode for better performance.Arguments
file_paths(List[str]): List of absolute paths to source filesoutput_directory(str): Directory where thumbnails should be savedwith_preview(bool, optional): Generate preview-sized thumbnails (default: True)with_detail(bool, optional): Generate detail-sized thumbnails (default: False)workspace_id(str, optional): Workspace ID for context
Returns (List[str]): List of successfully generated thumbnail paths
-
attach_thumbnail(file_path, local_file, is_detail=True)Attaches a custom thumbnail image to a file, replacing any existing thumbnail.Arguments
file_path(str): Absolute path to the target filelocal_file(str): Path to the thumbnail image fileis_detail(bool, optional): Whether this is a detail thumbnail vs. preview (default: True)
-
attach_thumbnails(file_path, local_preview_file, local_detail_file)Attaches both preview and detail thumbnails to a file in one operation.Arguments
file_path(str): Absolute path to the target filelocal_preview_file(str): Path to the preview thumbnail imagelocal_detail_file(str): Path to the detail thumbnail image
-
get_thumbnail(file_path, is_detail)Retrieves the path to an existing thumbnail for a file.Arguments
file_path(str): Absolute path to the fileis_detail(bool): Whether to get the detail thumbnail (True) or preview thumbnail (False)
Returns (str or None): Path to the thumbnail file, or None if no thumbnail exists
Examples
Basic Thumbnail Generation
import apsync
import anchorpoint
import os
ctx = anchorpoint.get_context()
def generate_file_thumbnails():
"""Generate thumbnails for selected files"""
if not ctx.selected_files:
ui = anchorpoint.UI()
ui.show_info("No Selection", "Please select files to generate thumbnails for")
return
ui = anchorpoint.UI()
# Create output directory for thumbnails
thumb_dir = os.path.join(ctx.folder, ".thumbnails")
os.makedirs(thumb_dir, exist_ok=True)
success_count = 0
failed_files = []
for file_path in ctx.selected_files:
filename = os.path.basename(file_path)
try:
# Generate both preview and detail thumbnails
success = apsync.generate_thumbnail(
file_path,
thumb_dir,
with_preview=True,
with_detail=True,
workspace_id=ctx.workspace_id
)
if success:
success_count += 1
print(f"Generated thumbnail for: {filename}")
else:
failed_files.append(filename)
except Exception as e:
failed_files.append(filename)
print(f"Failed to generate thumbnail for {filename}: {e}")
# Show results
if success_count > 0:
ui.show_success("Thumbnails Generated",
f"Successfully generated thumbnails for {success_count} files")
if failed_files:
failed_list = "\n".join(failed_files)
ui.show_warning("Some Failed",
f"Failed to generate thumbnails for:\n{failed_list}")
generate_file_thumbnails()
Batch Thumbnail Processing
import apsync
import anchorpoint
import os
ctx = anchorpoint.get_context()
def batch_thumbnail_generation():
"""Generate thumbnails for all supported files in a directory"""
if not os.path.isdir(ctx.path):
ui = anchorpoint.UI()
ui.show_error("Not a Directory", "Please select a directory for batch processing")
return
# Supported file types for thumbnails
supported_extensions = {
'.jpg', '.jpeg', '.png', '.tiff', '.bmp', '.gif', # Images
'.psd', '.ai', '.eps', # Adobe files
'.fbx', '.obj', '.ma', '.mb', '.max', '.blend', # 3D models
'.mp4', '.avi', '.mov', '.mkv', # Videos
'.pdf', '.docx', '.xlsx' # Documents
}
# Find all supported files
supported_files = []
for root, dirs, files in os.walk(ctx.path):
for file in files:
_, ext = os.path.splitext(file.lower())
if ext in supported_extensions:
supported_files.append(os.path.join(root, file))
if not supported_files:
ui = anchorpoint.UI()
ui.show_info("No Supported Files", "No supported files found for thumbnail generation")
return
# Create thumbnail output directory
thumb_dir = os.path.join(ctx.path, ".anchorpoint_thumbnails")
os.makedirs(thumb_dir, exist_ok=True)
# Use batch generation for better performance
try:
# Generate thumbnails in batch
generated_thumbs = apsync.generate_thumbnails(
supported_files,
thumb_dir,
with_preview=True,
with_detail=False,
workspace_id=ctx.workspace_id
)
ui = anchorpoint.UI()
ui.show_success("Batch Complete",
f"Generated {len(generated_thumbs)} thumbnails out of {len(supported_files)} files")
except Exception as e:
ui = anchorpoint.UI()
ui.show_error("Batch Failed", f"Batch thumbnail generation failed: {e}")
batch_thumbnail_generation()
Custom Thumbnail Attachment
import apsync
import anchorpoint
import os
from PIL import Image, ImageDraw, ImageFont
ctx = anchorpoint.get_context()
def create_custom_thumbnails():
"""Create and attach custom thumbnails for files"""
if not ctx.selected_files:
ui = anchorpoint.UI()
ui.show_info("No Selection", "Please select files to create custom thumbnails for")
return
# Create temporary directory for custom thumbnails
temp_dir = anchorpoint.temp_dir()
custom_thumb_dir = os.path.join(temp_dir, "custom_thumbnails")
os.makedirs(custom_thumb_dir, exist_ok=True)
for file_path in ctx.selected_files:
filename = os.path.basename(file_path)
name_without_ext = os.path.splitext(filename)[0]
try:
# Create custom thumbnail image
thumb_path = create_text_thumbnail(file_path, custom_thumb_dir)
if thumb_path:
# Attach the custom thumbnail
apsync.attach_thumbnail(file_path, thumb_path, is_detail=False)
print(f"Attached custom thumbnail to: {filename}")
except Exception as e:
print(f"Failed to create custom thumbnail for {filename}: {e}")
ui = anchorpoint.UI()
ui.show_success("Custom Thumbnails", "Custom thumbnails created and attached")
def create_text_thumbnail(file_path, output_dir):
"""Create a text-based thumbnail image"""
filename = os.path.basename(file_path)
name_without_ext = os.path.splitext(filename)[0]
# Create thumbnail image
thumb_size = (256, 256)
image = Image.new('RGB', thumb_size, color='lightblue')
draw = ImageDraw.Draw(image)
# Try to load a font (fallback to default if not available)
try:
font = ImageFont.truetype("arial.ttf", 16)
except:
font = ImageFont.load_default()
# Draw file information
draw.text((10, 10), "CUSTOM", fill='darkblue', font=font)
draw.text((10, 30), name_without_ext[:20], fill='black', font=font)
# Add file extension
ext = os.path.splitext(filename)[1].upper()
draw.text((10, 220), ext, fill='darkred', font=font)
# Save thumbnail
thumb_path = os.path.join(output_dir, f"{name_without_ext}_thumb.jpg")
image.save(thumb_path, 'JPEG', quality=85)
return thumb_path
create_custom_thumbnails()