OSError: cannot open resource printing tree image on Windows system
clabnet opened this issue · comments
First: thanks for this beautiful code. It is robust and very well documented.
I have installed all dependencies :
(base) jovyan@jupyter-lab:~/work/normalizer$ pip install 'bigtree[image]'
Requirement already satisfied: bigtree[image] in /opt/conda/lib/python3.11/site-packages (0.12.4)
Requirement already satisfied: pillow in /opt/conda/lib/python3.11/site-packages (from bigtree[image]) (10.0.1)
Requirement already satisfied: pydot in /opt/conda/lib/python3.11/site-packages (from bigtree[image]) (1.4.2)
Requirement already satisfied: pyparsing>=2.1.4 in /opt/conda/lib/python3.11/site-packages (from pydot->bigtree[image]) (3.1.1)
Trying to print a tree image on Windows system.
from bigtree import dataframe_to_tree_by_relation, tree_to_dot, tree_to_pillow, tree_to_dataframe
root = dataframe_to_tree_by_relation(df_slim)
tree_to_dataframe(
root,
name_col="item",
parent_col="parent",
path_col="path")
pillow_image = tree_to_pillow(root)
pillow_image.save("demo_pillow.png")
The tree is :
H-FUQF [parent=None]
└── FUQF.ALB.22.100 [parent=Node(/H-FUQF, level=0, t_n=1)]
└── 2999-12353-01- [parent=Node(/H-FUQF/FUQF.ALB.22.100, level=1, t_n=7)]
├── 221355-090-2 [parent=Node(/H-FUQF/FUQF.ALB.22.100/2999-12353-01-, level=2, t_n=10)]
├── 2922-01366-01- [parent=Node(/H-FUQF/FUQF.ALB.22.100/2999-12353-01-, level=2, t_n=10)]
├── 242241-402-1 [parent=Node(/H-FUQF/FUQF.ALB.22.100/2999-12353-01-, level=2, t_n=10)]
├── 2923-02256-01C [parent=Node(/H-FUQF/FUQF.ALB.22.100/2999-12353-01-, level=2, t_n=10)]
The error is :
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
Cell In[10], line 16
7 tree_to_dataframe(
8 root,
9 name_col="item",
10 parent_col="parent",
11 path_col="path")
13 #graph = tree_to_dot(root, node_colour="gold")
14 #graph.write_png("assets/demo.png")
---> 16 pillow_image = tree_to_pillow(root)
17 pillow_image.save("demo_pillow.png")
File /opt/conda/lib/python3.11/site-packages/bigtree/utils/exceptions.py:122, in optional_dependencies_image.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
117 except ImportError: # pragma: no cover
118 raise ImportError(
119 "Pillow not available. Please perform a\n\n"
120 "pip install 'bigtree[image]'\n\nto install required dependencies"
121 ) from None
--> 122 return func(*args, **kwargs)
File /opt/conda/lib/python3.11/site-packages/bigtree/tree/export.py:811, in tree_to_pillow(tree, width, height, start_pos, font_family, font_size, font_colour, bg_colour, **kwargs)
781 """Export tree to image (JPG, PNG).
782 Image will be similar format as `print_tree`, accepts additional keyword arguments as input to `yield_tree`
783
(...)
808 (PIL.Image.Image)
809 """
810 # Initialize font
--> 811 font = ImageFont.truetype(font_family, font_size)
813 # Initialize text
814 image_text = []
File /opt/conda/lib/python3.11/site-packages/PIL/ImageFont.py:791, in truetype(font, size, index, encoding, layout_engine)
788 return FreeTypeFont(font, size, index, encoding, layout_engine)
790 try:
--> 791 return freetype(font)
792 except OSError:
793 if not is_path(font):
File /opt/conda/lib/python3.11/site-packages/PIL/ImageFont.py:788, in truetype.<locals>.freetype(font)
787 def freetype(font):
--> 788 return FreeTypeFont(font, size, index, encoding, layout_engine)
File /opt/conda/lib/python3.11/site-packages/PIL/ImageFont.py:226, in FreeTypeFont.__init__(self, font, size, index, encoding, layout_engine)
224 load_from_bytes(f)
225 return
--> 226 self.font = core.getfont(
227 font, size, index, encoding, layout_engine=layout_engine
228 )
229 else:
230 load_from_bytes(font)
OSError: cannot open resource
Any suggestion please ?
Hi, thanks for raising this issue! The fix is implemented in v0.12.5, do upgrade bigtree with the command pip install --upgrade bigtree
.