[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky] [Facebook]
[Build Your Own Python Action Arcade!]

[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Scale lines drawn on a scaled image in Python

[The white lines are scaled according to the image's scale]

In my last post, Draw on a scrolled image in Python, I mentioned that you might want to try scaling the lines when you scale the image. Who was I kidding? I only managed to resist trying it for a few hours.

This example does exactly that. When the image is at full scale, the program draws its lines 10 pixels thick. When you adjust the program's scale, the program changes the lines' thickness accordingly.

Scaling Line Thickness

When it starts, the program uses the following code to initialize line color and thickness.

# Set the line color and 100% scale line width. self.line_color = 'white' self.line_width = 5

This simply stores the desired line color and the thickness lines should have at 100% scale.

Later, when you start drawing, the following code starts a line.

def mouse_down(self, event): '''Begin drawing.''' self.new_points = [(event.x, event.y), (event.x, event.y)] self.lines.append(self.new_points) line_width = self.line_width * self.scale_var.get() self.new_line = self.canvas.create_line(self.new_points, fill=self.line_color, width=line_width)

This code creates the new_points list to hold the new line's points and saves the new line in self.lines as before. It then multiplies the 100% line thickness self.line_width by the current scale factor to get the scaled line width and uses that width to create the line.

The last change is in the mnu_scale method that executes when you change the program's scale. Most of the code is similar to the previous version; here's the only change.

# Calculate the line width. line_width = self.line_width * new_scale # Draw the rescaled lines. for line in self.lines: self.canvas.create_line(line, fill=self.line_color, width=line_width)

Like the previous piece of code, this snippet multiplies the 100% line width by the current scale to get the desired line width and then uses it when it calls create_line to draw the lines at the new scale.

Conclusion

That's all there is to it. If your program includes other drawn objects, you could easily use a similar technique to scale with line widths.

Another approach would be to draw lines directly on the image rather than creating line objects inside the Canvas widget. That technique would be particularly useful if you will want to save the modified image. Give it a try if you like.

As always, download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.