Search code examples
pythonpng

SyntaxError: not a PNG file


For the longest time, I've been using

dfi.export(df1, "test.png")

to export a dataframe styler (df1) with type pandas.io.formats.style.Styler into a .png.

Today I get the following error:

Traceback (most recent call last):

  File ~\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py:3369 in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  Input In [48] in <cell line: 1>
    dfi.export(df1, "ice_cotton_" + str(dashboard_date).split(" ")[0])

  File ~\Anaconda3\lib\site-packages\dataframe_image\_pandas_accessor.py:48 in export
    return _export(

  File ~\Anaconda3\lib\site-packages\dataframe_image\_pandas_accessor.py:117 in _export
    img_str = converter(html)

  File ~\Anaconda3\lib\site-packages\dataframe_image\_screenshot.py:188 in run
    img = self.take_screenshot()

  File ~\Anaconda3\lib\site-packages\dataframe_image\_screenshot.py:140 in take_screenshot
    img = mimage.imread(buffer)

  File ~\Anaconda3\lib\site-packages\matplotlib\image.py:1560 in imread
    with img_open(fname) as image:

  File ~\Anaconda3\lib\site-packages\PIL\ImageFile.py:112 in __init__
    self._open()

  File ~\Anaconda3\lib\site-packages\PIL\PngImagePlugin.py:676 in _open
    raise SyntaxError("not a PNG file")

  File <string>
SyntaxError: not a PNG file

How do I fix this?


Solution

  • This is a set of possible solutions and only the last one worked for me:

    1. a previous version of matplotlib, e.g. 3.4.3
    2. use a backend, e.g. ‘mpl.use('ps')’
    3. previous versions of dataframe-image (tested 0.1.3, 0.1.2 and 0.1.0)
    4. table_conversion, e.g. dfi.export(table, table_conversion='firefox'): this gives no error but formatting is lost
    5. previous version of Chrome (i.e. January 27; 109.0.5414.119): this works!