Python Forum
Question about ImagePath.Path() in Pillow library - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Question about ImagePath.Path() in Pillow library (/thread-39272.html)



Question about ImagePath.Path() in Pillow library - dfkettle - Jan-24-2023

Hi!

I've been learning how to use the various packages and methods in the Pillow library (v9.4.0 under Python 3.7.9), but I have a question about the ImagePath.Path class in the ImagePath module. If I run the example below, I get the same result whether I pass a Path object to Image.line() or I pass an ordinary list of tuples. So is there any reason to use ImagePath if you don't need to call any of its methods (compact(),getbox(), map(), toList(), transform())? In other words, simply creating an Path object doesn't really do anything? It's only the associated methods that do anything useful?

from PIL import Image, ImageDraw, ImagePath

img = Image.new("RGB",(800,400),(255,255,0))
cnv = ImageDraw.Draw(img)
#path = [(0,0),(250,300),(500,250),(0,0)]
path = ImagePath.Path([(0,0),(250,300),(500,250),(0,0)])
cnv.line(path,(0,0,0),3)
img.save("test.jpg")



RE: Question about ImagePath.Path() in Pillow library - snippsat - Jan-24-2023

(Jan-24-2023, 01:35 PM)dfkettle Wrote: So is there any reason to use ImagePath if you don't need to call any of its methods (compact(),getbox(), map(), toList(), transform())? In other words, simply creating an Path object doesn't really do anything?
Yes,you call methods if need eg more info like bounding box of the path.
Then call method getbox().
from PIL import Image, ImageDraw, ImagePath

img = Image.new("RGB",(800,400),(255,255,0))
cnv = ImageDraw.Draw(img)
path = ImagePath.Path([(0,0),(250,300),(500,250),(0,0)])
# Use getbbox() method to get bounding box of the path
>>> path.getbbox()
(0.0, 0.0, 500.0, 300.0)