Skip to content

Commit

Permalink
fix max image limit
Browse files Browse the repository at this point in the history
  • Loading branch information
PaleNeutron committed Sep 16, 2024
1 parent e8359ff commit 70d6832
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions dataframe_image/_pandas_accessor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import io
from contextlib import contextmanager
from pathlib import Path
from typing import Literal

Expand All @@ -20,6 +21,15 @@
MAX_ROWS = 100




@contextmanager
def disable_max_image_pixels():
pre_limit = Image.MAX_IMAGE_PIXELS
Image.MAX_IMAGE_PIXELS = None
yield
Image.MAX_IMAGE_PIXELS = pre_limit

@pd.api.extensions.register_dataframe_accessor("dfi")
class _Export:
def __init__(self, df):
Expand Down Expand Up @@ -156,10 +166,6 @@ def generate_html(


def save_image(img_str, filename):
# swap back to original value
pre_limit = Image.MAX_IMAGE_PIXELS
Image.MAX_IMAGE_PIXELS = None
Image.MAX_IMAGE_PIXELS = pre_limit

try:
with open(filename, "wb") as f:
Expand Down Expand Up @@ -194,7 +200,10 @@ def export(
use_mathjax,
)
html = generate_html(obj, filename, max_rows, max_cols)
img_str = converter(html)

with disable_max_image_pixels():
img_str = converter(html)

save_image(img_str, filename)


Expand Down Expand Up @@ -222,11 +231,12 @@ async def export_async(
use_mathjax,
)
html = generate_html(obj, filename, max_rows, max_cols)
# check if converter is async
if inspect.iscoroutinefunction(converter):
img_str = await converter(html)
else:
img_str = converter(html)
with disable_max_image_pixels():
# check if converter is async
if inspect.iscoroutinefunction(converter):
img_str = await converter(html)
else:
img_str = converter(html)
# TODO: use async file writing
save_image(img_str, filename)

Expand Down

0 comments on commit 70d6832

Please sign in to comment.