etetoolkit / ete

Python package for building, comparing, annotating, manipulating and visualising trees. It provides a comprehensive API and a collection of command line tools, including utilities to work with the NCBI taxonomy tree.
http://etetoolkit.org
GNU General Public License v3.0
782 stars 212 forks source link

fix type errors when rendering into a png #617

Closed pschwientek closed 1 year ago

pschwientek commented 2 years ago

the dimension conversion calculations yield float values but QImage expects int in constructor as well as .setDotsPerMeterX()

a quick fix is casting to int() but there are probably better ways

shvenkat commented 2 years ago

@pschwientek : Thanks for the PR! This worked for me with Python 3.10.2 and the following versions. I suggest also fixing the same issue for SVG, i.e.

--- a/treeview/main.py
+++ b/treeview/main.py
@@ -682,7 +682,7 @@
     if ext == "SVG":
         svg = QSvgGenerator()
         targetRect = QRectF(0, 0, w, h)
-        svg.setSize(QSize(w, h))
+        svg.setSize(QSize(int(round(w)), int(round(h))))
         svg.setViewBox(targetRect)
         svg.setTitle("Generated with ETE http://etetoolkit.org")
         svg.setDescription("Generated with ETE http://etetoolkit.org")
@@ -749,10 +749,10 @@
         scene.render(pp, targetRect, scene.sceneRect(), ratio_mode)
     else:
         targetRect = QRectF(0, 0, w, h)
-        ii= QImage(w, h, QImage.Format_ARGB32)
+        ii= QImage(int(round(w)), int(round(h)), QImage.Format_ARGB32)
         ii.fill(QColor(Qt.white).rgb())
-        ii.setDotsPerMeterX(dpi / 0.0254) # Convert inches to meters
-        ii.setDotsPerMeterY(dpi / 0.0254)
+        ii.setDotsPerMeterX(int(round(dpi / 0.0254))) # Convert inches to meters
+        ii.setDotsPerMeterY(int(round(dpi / 0.0254)))
         pp = QPainter(ii)
         pp.setRenderHint(QPainter.Antialiasing)
         pp.setRenderHint(QPainter.TextAntialiasing)

This works with:

Python 3.10.2
ete3                          3.1.2
PyQt5                         5.15.6
PyQt5-Qt5                     5.15.2
PyQt5-sip                     12.9.0
KennethVrb commented 1 year ago

I have come across the same issue and looking at your pull request i believe if you would just fix w and h variables after line 662 you will need even need to fix the dpi variable

jhcepas commented 1 year ago

fixed in 71d0d3b0f0d8e65a93af295a64a4860d2f79551c