Skip to content

Image

dwg_to_webp

dwg_to_webp(signed_url_dwg, signed_url_pdf)

Convert a DWG file to WebP format via Autodesk Design Automation PDF conversion.

This function submits a DWG file to Autodesk Design Automation for conversion to PDF, then downloads the resulting PDF and converts it to WebP format.

Parameters:

Name Type Description Default
signed_url_dwg str

Signed URL pointing to the source DWG file. This URL should provide read access to the DWG file in cloud storage.

required
signed_url_pdf str

Signed URL where the converted PDF will be stored. This URL should provide write access to cloud storage.

required

Returns:

Type Description
bytes

The WebP image as a byte array converted from the first page of the resulting PDF.

Raises:

Type Description
RuntimeError

If the PDF download fails or the Autodesk Design Automation conversion fails.

RequestException

If there are network issues during PDF download.

EnvironmentError

If required environment variables (APIM_SIGNED_BASE_URL) are not set.

Examples:

>>> dwg_url = "https://storage.blob.core.windows.net/container/file.dwg?sas=..."
>>> pdf_url = "https://storage.blob.core.windows.net/container/output.pdf?sas=..."
>>> webp_data = dwg_to_webp(dwg_url, pdf_url)
>>> with open('output.webp', 'wb') as f:
...     f.write(webp_data)
Note

This function requires environment variables:

  • AUTODESK_DA_CLIENT_ID and `AUTODESK_DA_CLIENT_SECRET' for interacting with the Design Automation Service.
  • APIM_SIGNED_BASE_URL for secure post back of the intermediate PDF results to Hoppa blob storage.

The conversion process is asynchronous and may take time depending on file size and Autodesk service load.

image_to_webp

image_to_webp(image_bytes, target_pixels=500000, quality=85)

Convert an image to a downsampled WebP format with specified quality and pixel count.

This function takes an image in bytes format, converts it to RGB, and optionally downsamples it to meet a target pixel count before converting to WebP format. If the original image has fewer pixels than the target, it returns the original bytes to avoid upsampling.

Parameters:

Name Type Description Default
image_bytes bytes

The input image file as a binary blob. Supports common image formats (JPEG, PNG, etc.) that can be opened by PIL.

required
target_pixels int

Desired total number of pixels in the output image. Only downsampling is performed.

500000
quality int

WebP compression quality from 1-100, where 100 is highest quality.

85

Returns:

Type Description
bytes

The WebP image as a byte array. Returns original bytes if the image already has fewer pixels than target_pixels.

Raises:

Type Description
UnidentifiedImageError

If the input bytes cannot be recognized as a valid image format.

ValueError

If quality is not in the range 1-100.

Examples:

>>> with open('large_image.jpg', 'rb') as f:
...     image_data = f.read()
>>> webp_data = image_to_webp(image_data, target_pixels=250_000, quality=80)
>>> with open('output.webp', 'wb') as f:
...     f.write(webp_data)
Note

The function uses high-quality Lanczos interpolation for resizing and ensures images are in RGB format before processing.

pdf_to_webp

pdf_to_webp(blob, target_pixels=1000000, quality=85, start=1, end=None, all_pages=False)

Convert PDF pages to resized WebP images with specified pixel count and quality.

This function extracts pages from a PDF document, renders them as images, and converts them to WebP format with optional downsampling. It supports processing single pages, page ranges, or all pages in the document.

Parameters:

Name Type Description Default
blob bytes

The input PDF file as a binary blob.

required
target_pixels int

Desired total number of pixels in each output image. Images are downsampled to meet this target.

1000000
quality int

WebP compression quality from 1-100, where 100 is highest quality.

85
start int

The first page to process (1-based index).

1
end int

The last page to process (1-based index, inclusive). If None, only the start page is processed.

None
all_pages bool

If True, processes all pages in the document, ignoring start and end parameters.

False

Returns:

Type Description
Union[bytes, List[bytes]]

Union[bytes, List[bytes]]: If processing a single page, returns bytes of the WebP image. If processing multiple pages, returns a list of WebP images as byte arrays.

Raises:

Type Description
Exception

If the PDF has no pages or cannot be opened.

FileDataError

If the PDF data is corrupted or invalid.

ValueError

If start/end page numbers are invalid or quality is out of range.

Examples:

>>> with open('document.pdf', 'rb') as f:
...     pdf_data = f.read()
>>> # Convert first page only
>>> webp_data = pdf_to_webp(pdf_data, start=1)
>>> # Convert pages 2-5
>>> webp_list = pdf_to_webp(pdf_data, start=2, end=5)
>>> # Convert all pages
>>> all_webp = pdf_to_webp(pdf_data, all_pages=True)
Note

Page numbering is 1-based for user convenience but converted to 0-based internally.

resize_image

resize_image(image, new_height, new_width)

Resize an image array to specified dimensions using nearest-neighbor sampling.

This function resizes an image represented as a NumPy array by computing linear indices for sampling. It uses a simple downsampling approach suitable for reducing image dimensions efficiently.

Parameters:

Name Type Description Default
image ndarray

The original image as a NumPy array with shape (height, width) for grayscale or (height, width, channels) for color.

required
new_height int

The target height in pixels. Must be positive.

required
new_width int

The target width in pixels. Must be positive.

required

Returns:

Type Description
ndarray

np.ndarray: Resized image as a NumPy array with shape (new_height, new_width) or (new_height, new_width, channels) maintaining the original channel count.

Raises:

Type Description
ValueError

If new_height or new_width are not positive integers.

IndexError

If the computed indices exceed the original image dimensions.

Examples:

>>> import numpy as np
>>> original_img = np.random.rand(100, 100, 3)  # 100x100 RGB image
>>> resized_img = resize_image(original_img, 50, 50)  # Resize to 50x50
>>> print(resized_img.shape)
(50, 50, 3)
Note

This function uses nearest-neighbor sampling which is fast but may not preserve image quality as well as more sophisticated interpolation methods. For high-quality resizing, consider using cv2.resize() or PIL.Image.resize().

svg_to_webp

svg_to_webp(svg_bytes, target_pixels=500000, quality=85)

Convert an SVG image to a downsampled WebP image using headless Chrome browser.

This function renders SVG content using a headless Chrome browser via Selenium, captures it as a PNG screenshot, and then converts it to WebP format with optional downsampling.

Parameters:

Name Type Description Default
svg_bytes bytes

The input SVG file as a binary blob containing valid SVG markup.

required
target_pixels int

Desired total number of pixels in the output image. Downsampling is applied if the rendered image exceeds this count.

500000
quality int

WebP compression quality from 1-100, where 100 is highest quality.

85

Returns:

Type Description
bytes

The WebP image as a byte array.

Raises:

Type Description
WebDriverException

If Chrome WebDriver is not available or fails to start.

UnicodeDecodeError

If svg_bytes cannot be decoded to create a data URL.

ValueError

If quality is not in the range 1-100.

Examples:

>>> with open('diagram.svg', 'rb') as f:
...     svg_data = f.read()
>>> webp_data = svg_to_webp(svg_data, target_pixels=1_000_000, quality=90)
>>> with open('diagram.webp', 'wb') as f:
...     f.write(webp_data)
Note

This function requires Chrome/Chromium browser and ChromeDriver to be installed and available in the system PATH. The SVG is rendered with a default window size of 1024x1024 pixels.