8000 [draft] excess image resize maintain aspect ratio by mcembalest · Pull Request #378 · nomic-ai/nomic · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[draft] excess image resize maintain aspect ratio #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions nomic/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
)
from .data_operations import AtlasMapData, AtlasMapDuplicates, AtlasMapEmbeddings, AtlasMapTags, AtlasMapTopics
from .settings import *
from .utils import assert_valid_project_id, download_feather
from .utils import assert_valid_project_id, download_feather, resize_pil


class AtlasUser:
Expand Down Expand Up @@ -1454,7 +1454,7 @@ def _add_blobs(
image = Image.open(blob)
image = image.convert("RGB")
if image.height > 512 or image.width > 512:
image = image.resize((512, 512))
image = resize_pil(image)
buffered = BytesIO()
image.save(buffered, format="JPEG")
images.append((uuid, buffered.getvalue()))
Expand All @@ -1463,7 +1463,7 @@ def _add_blobs(
elif isinstance(blob, Image.Image):
blob = blob.convert("RGB") # type: ignore
if blob.height > 512 or blob.width > 512:
blob = blob.resize((512, 512))
blob = resize_pil( 8000 blob)
buffered = BytesIO()
blob.save(buffered, format="JPEG")
images.append((uuid, buffered.getvalue()))
Expand Down
12 changes: 1 addition & 11 deletions nomic/embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from .dataset import AtlasClass
from .settings import *
from .utils import resize_pil

try:
from gpt4all import CancellationError, Embed4All
Expand Down Expand Up @@ -345,17 +346,6 @@ def image_api_request(
raise Exception((response.status_code, response.text))


def resize_pil(img):
width, height = img.size
# if image is too large, downsample before sending over the wire
max_width = 512
max_height = 512
if width > max_width or height > max_height:
downsize_factor = max(width // max_width, height // max_height)
img = img.resize((width // downsize_factor, height // downsize_factor))
return img


def _is_valid_url(url):
if not isinstance(url, str):
return False
Expand Down
10 changes: 10 additions & 0 deletions nomic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,3 +288,13 @@ def download_feather(
if not download_success or schema is None:
raise ValueError(f"Failed to download feather file from {url} after {num_attempts} attempts.")
return schema

def resize_pil(img):
width, height = img.size
# if image is too large, downsample before sending over the wire
max_width = 512
max_height = 512
if width > max_width or height > max_height:
downsize_factor = max(width // max_width, height // max_height)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new helper function resize_pil computes the downsize_factor using integer division (e.g. width // max_width), so for images only slightly above 512 (e.g. 600×700) the factor is 1 and no resizing occurs. Consider computing a floating‑point scale factor (e.g. scale = min(max_width/width, max_height/height)) and then resizing with new dimensions (int(width*scale), int(height*scale)). Also, specify a resampling filter like Image.LANCZOS for better quality.

img = img.resize((width // downsize_factor, height // downsize_factor))
return img
0