Skip to content

Commit

Permalink
fix font size calculation for newer pillow version
Browse files Browse the repository at this point in the history
  • Loading branch information
MaartenBransen committed Dec 1, 2023
1 parent a377e99 commit 0181d25
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
2 changes: 1 addition & 1 deletion scm_confocal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.7.4'
__version__ = '1.7.5'

from .sp8 import sp8_lif,sp8_image,sp8_series
from .visitech import visitech_series,visitech_faststack
Expand Down
57 changes: 30 additions & 27 deletions scm_confocal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2185,23 +2185,24 @@ def _on_lim_change(call):
#get size of text
from PIL import ImageFont
font = ImageFont.truetype(font,size=int(fontsize))
textsize = font.getsize(text)
offset = font.getoffset(text)
textsize = (textsize[0]-offset[0],textsize[1]-offset[1]+fontbaseline)
text_bbox = font.getbbox(text)
offset = (text_bbox[0],text_bbox[1])
textsize = (text_bbox[2]-text_bbox[0],text_bbox[3]-text_bbox[1])

#correct baseline for mu in case of micrometer
if 'µ' in text:
textsize = (textsize[0],textsize[1]-6*scale)
bb = font.getbbox(text.replace('µ','u'))
textsize = (textsize[0],bb[3]-bb[1])

#determine box height with appropriate paddings
if draw_text and draw_bar:#both
boxheight = barpad + barthickness + fontpad + textsize[1]
boxheight = barpad + barthickness+fontpad+textsize[1]+fontbaseline
boxwidth = max([2*barpad+barsize_px,2*fontpad+textsize[0]])
elif draw_bar:#bar only
boxheight = 2*barpad + barthickness
boxwidth = 2*barpad + barsize_px
else:#text only
boxheight = 2*fontpad + textsize[1]
boxheight = 2*fontpad + textsize[1] + fontbaseline
boxwidth = 2*fontpad + textsize[0]

#determine box/bar/text position based on loc
Expand Down Expand Up @@ -2249,8 +2250,8 @@ def _on_lim_change(call):
if draw_bar:

#calculate positions for bar
barx = (2*x + boxwidth)/2 - barsize_px/2
bary = y+boxheight-barpad-barthickness
barx = x + boxwidth/2 - barsize_px/2
bary = y + boxheight-barpad-barthickness

if len(barcolor) == 3:
barcolor = (*barcolor,255)
Expand All @@ -2267,8 +2268,16 @@ def _on_lim_change(call):
if draw_text:

#calculate position for text (horizontally centered in box)
textx = (2*x + boxwidth)/2 - (textsize[0]+offset[0])/2
texty = y + fontpad-offset[1]
textx = x + boxwidth/2 - textsize[0]/2 - offset[0]
texty = y + fontpad - offset[1]

# #for troubleshooting only draw text BB
# draw.rectangle([
# textx+text_bbox[0],
# texty+text_bbox[1],
# textx+text_bbox[2],
# texty+text_bbox[3]
# ])

if len(fontcolor) == 3:
barcolor = (*fontcolor,255)
Expand Down Expand Up @@ -2463,6 +2472,7 @@ def _on_lim_change(call):

#set up sizes for text
if draw_text:

fontpad = fontpad*scale
fontsize = 2*fontsize*scale
fontbaseline = fontbaseline*scale
Expand All @@ -2483,28 +2493,24 @@ def _on_lim_change(call):

#get size of text
font = ImageFont.truetype(font,size=int(fontsize))
textsize = font.getsize(text)
offset = font.getoffset(text)
textsize = (textsize[0]-offset[0],textsize[1]-offset[1]+fontbaseline)
text_bbox = font.getbbox(text)
offset = (text_bbox[0],text_bbox[1])
textsize = (text_bbox[2]-text_bbox[0],text_bbox[3]-text_bbox[1])

#correct baseline for mu in case of micrometer
if 'µ' in text:
textsize = (textsize[0],textsize[1]-6*scale)

#when not drawing the text, set text size and padding to 0
else:
textsize = (0,0)
fontpad = 0
bb = font.getbbox(text.replace('µ','u'))
textsize = (textsize[0],bb[3]-bb[1])

#determine box height with appropriate paddings
if draw_text and draw_bar:#both
boxheight = barpad + barthickness + fontpad + textsize[1]
boxheight = barpad + barthickness+fontpad+textsize[1]+fontbaseline
boxwidth = max([2*barpad+barsize_px,2*fontpad+textsize[0]])
elif draw_bar:#bar only
boxheight = 2*barpad + barthickness
boxwidth = 2*barpad + barsize_px
else:#text only
boxheight = 2*fontpad + textsize[1]
boxheight = 2*fontpad + textsize[1] + fontbaseline
boxwidth = 2*fontpad + textsize[0]

#determine box/bar/text position based on loc
Expand Down Expand Up @@ -2564,13 +2570,10 @@ def _on_lim_change(call):
width=0,
)

#draw the text
if draw_text:

#calculate position for text (horizontally centered in box)
textx = (2*x + boxwidth)/2 - (textsize[0]+offset[0])/2
texty = y + fontpad-offset[1]
textx = x + boxwidth/2 - textsize[0]/2 - offset[0]
texty = y + fontpad - offset[1]

if len(fontcolor) == 3:
barcolor = (*fontcolor,255)

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="scm_confocal",
version="1.7.4",
version="1.7.5",
author="Maarten Bransen",
author_email="m.bransen@uu.nl",
license='GNU General Public License v3.0',
Expand All @@ -11,7 +11,7 @@
install_requires=[
"numpy>=1.19.2",
"scipy>=1.6.0",
"pillow>=6.2.1",
"pillow>=9.2.0",
"pims>=0.5",
"readlif>=0.6.5",
"numba>=0.50.0",
Expand Down

0 comments on commit 0181d25

Please sign in to comment.