Skip to content

Commit

Permalink
Use Pythonic function name
Browse files Browse the repository at this point in the history
  • Loading branch information
wkentaro committed Aug 2, 2024
1 parent b46477b commit 901a899
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions labelme/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(
# is used for drawing the pending line a different color.
self.line_color = line_color

def scaleTranslate(self, point):
def _scale_point(self, point: QtCore.QPointF) -> QtCore.QPointF:
return QtCore.QPointF(point.x() * self.scale, point.y() * self.scale)

def setShapeRefined(self, shape_type, points, point_labels, mask=None):
Expand Down Expand Up @@ -197,22 +197,18 @@ def paint(self, painter):
QtCore.Qt.SmoothTransformation,
)

painter.drawImage(
# Position error will increase with the scaling factor
self.scaleTranslate(self.points[0]),
qimage,
)
painter.drawImage(self._scale_point(point=self.points[0]), qimage)

line_path = QtGui.QPainterPath()
contours = skimage.measure.find_contours(np.pad(self.mask, pad_width=1))
for contour in contours:
contour += [self.points[0].y(), self.points[0].x()]
line_path.moveTo(
self.scaleTranslate(QtCore.QPointF(contour[0, 1], contour[0, 0]))
self._scale_point(QtCore.QPointF(contour[0, 1], contour[0, 0]))
)
for point in contour[1:]:
line_path.lineTo(
self.scaleTranslate(QtCore.QPointF(point[1], point[0]))
self._scale_point(QtCore.QPointF(point[1], point[0]))
)
painter.drawPath(line_path)

Expand All @@ -225,8 +221,8 @@ def paint(self, painter):
assert len(self.points) in [1, 2]
if len(self.points) == 2:
rectangle = QtCore.QRectF(
self.scaleTranslate(self.points[0]),
self.scaleTranslate(self.points[1]),
self._scale_point(self.points[0]),
self._scale_point(self.points[1]),
)
line_path.addRect(rectangle)
if self.shape_type == "rectangle":
Expand All @@ -236,17 +232,17 @@ def paint(self, painter):
assert len(self.points) in [1, 2]
if len(self.points) == 2:
raidus = labelme.utils.distance(
self.scaleTranslate(self.points[0] - self.points[1])
self._scale_point(self.points[0] - self.points[1])
)
line_path.addEllipse(
self.scaleTranslate(self.points[0]), raidus, raidus
self._scale_point(self.points[0]), raidus, raidus
)
for i in range(len(self.points)):
self.drawVertex(vrtx_path, i)
elif self.shape_type == "linestrip":
line_path.moveTo(self.scaleTranslate(self.points[0]))
line_path.moveTo(self._scale_point(self.points[0]))
for i, p in enumerate(self.points):
line_path.lineTo(self.scaleTranslate(p))
line_path.lineTo(self._scale_point(p))
self.drawVertex(vrtx_path, i)
elif self.shape_type == "points":
assert len(self.points) == len(self.point_labels)
Expand All @@ -256,17 +252,17 @@ def paint(self, painter):
else:
self.drawVertex(negative_vrtx_path, i)
else:
line_path.moveTo(self.scaleTranslate(self.points[0]))
line_path.moveTo(self._scale_point(self.points[0]))
# Uncommenting the following line will draw 2 paths
# for the 1st vertex, and make it non-filled, which
# may be desirable.
# self.drawVertex(vrtx_path, 0)

for i, p in enumerate(self.points):
line_path.lineTo(self.scaleTranslate(p))
line_path.lineTo(self._scale_point(p))
self.drawVertex(vrtx_path, i)
if self.isClosed():
line_path.lineTo(self.scaleTranslate(self.points[0]))
line_path.lineTo(self._scale_point(self.points[0]))

painter.drawPath(line_path)
if vrtx_path.length() > 0:
Expand All @@ -284,7 +280,7 @@ def paint(self, painter):
def drawVertex(self, path, i):
d = self.point_size
shape = self.point_type
point = self.scaleTranslate(self.points[i])
point = self._scale_point(self.points[i])
if i == self._highlightIndex:
size, shape = self._highlightSettings[self._highlightMode]
d *= size
Expand Down

0 comments on commit 901a899

Please sign in to comment.