---
title: data_utils
---

<div class="python-reference">
<a id="fastvideo.models.dits.hyworld.data_utils-functions"></a>
<a id="fastvideo.models.dits.hyworld.data_utils"></a>

<h2 id="fastvideo.models.dits.hyworld.data_utils.resize_and_center_crop"><code>fastvideo.models.dits.hyworld.data_utils.resize_and_center_crop</code></h2>
<pre class="doc-signature"><code>resize_and_center_crop(image, target_width, target_height)</code></pre>

<details><summary>Source</summary><pre><code>def resize_and_center_crop(image, target_width, target_height):
    if target_height == image.shape&#91;0&#93; and target_width == image.shape&#91;1&#93;:
        return image

    pil_image = Image.fromarray(image)
    original_width, original_height = pil_image.size
    scale_factor = max(target_width / original_width, target_height / original_height)
    resized_width = int(round(original_width * scale_factor))
    resized_height = int(round(original_height * scale_factor))
    resized_image = pil_image.resize((resized_width, resized_height), Image.LANCZOS)
    left = (resized_width - target_width) / 2
    top = (resized_height - target_height) / 2
    right = (resized_width + target_width) / 2
    bottom = (resized_height + target_height) / 2
    cropped_image = resized_image.crop((left, top, right, bottom))
    return np.array(cropped_image)</code></pre></details>
<h2 id="fastvideo.models.dits.hyworld.data_utils.get_closest_ratio"><code>fastvideo.models.dits.hyworld.data_utils.get_closest_ratio</code></h2>
<pre class="doc-signature"><code>get_closest_ratio(height: float, width: float, ratios: list, buckets: list)</code></pre>
<p>Get the closest ratio in the buckets.</p>
<h3>Parameters</h3>
<dl>
<dt><code>height: float</code></dt><dd><p>video height</p></dd>
<dt><code>width: float</code></dt><dd><p>video width</p></dd>
<dt><code>ratios: list</code></dt><dd><p>video aspect ratio</p></dd>
<dt><code>buckets: list</code></dt><dd><p>buckets generated by <code>generate_crop_size_list</code></p></dd>
</dl>
<h3>Returns</h3>
<dl>
<dt><code></code></dt><dd><p>the closest size in the buckets and the corresponding ratio</p></dd>
</dl>
<details><summary>Source</summary><pre><code>def get_closest_ratio(height: float, width: float, ratios: list, buckets: list):
    &quot;&quot;&quot;
    Get the closest ratio in the buckets.

    Args:
        height (float): video height
        width (float): video width
        ratios (list): video aspect ratio
        buckets (list): buckets generated by `generate_crop_size_list`

    Returns:
        the closest size in the buckets and the corresponding ratio
    &quot;&quot;&quot;
    aspect_ratio = float(height) / float(width)

    ratios_array = np.array(ratios)
    closest_ratio_id = np.abs(ratios_array - aspect_ratio).argmin()
    closest_size = buckets&#91;closest_ratio_id&#93;
    closest_ratio = ratios_array&#91;closest_ratio_id&#93;

    return closest_size, closest_ratio</code></pre></details>
<h2 id="fastvideo.models.dits.hyworld.data_utils.generate_crop_size_list"><code>fastvideo.models.dits.hyworld.data_utils.generate_crop_size_list</code></h2>
<pre class="doc-signature"><code>generate_crop_size_list(base_size = 256, patch_size = 16, max_ratio = 4.0)</code></pre>

<details><summary>Source</summary><pre><code>def generate_crop_size_list(base_size=256, patch_size=16, max_ratio=4.0):
    num_patches = round((base_size / patch_size)**2)
    assert max_ratio &gt;= 1.0
    crop_size_list = &#91;&#93;
    wp, hp = num_patches, 1
    while wp &gt; 0:
        if max(wp, hp) / min(wp, hp) &lt;= max_ratio:
            crop_size_list.append((wp * patch_size, hp * patch_size))
        if (hp + 1) * wp &lt;= num_patches:
            hp += 1
        else:
            wp -= 1
    return crop_size_list</code></pre></details>
</div>
