2015-12-13 4 views
8

मैं पीआईएल/तकिया के ImageDraw मॉड्यूल का उपयोग कर एक छवि पर मोटी आयत खींचने की कोशिश कर रहा हूं।क्या पीआईएल में आयताकार की चौड़ाई निर्दिष्ट करने का कोई तरीका है?

मैंने draw.rectangle([x1, y1, x2, y2], outline='yellow', width=3) का उपयोग करने की कोशिश की लेकिन यह चौड़ाई पैरामीटर की तरह प्रतीत नहीं होता है।

मैं अनुकरण कर सकता हूं कि मैं लाइनों के समूह के साथ क्या करना चाहता हूं, लेकिन मैं सोच रहा था कि ऐसा करने का उचित तरीका है या नहीं।

''' 
coordinates = [x1, y1, x2, y2] 

    (x1, y1) 
     *-------------- 
     |    | 
     |    | 
     |    | 
     |    | 
     |    | 
     |    | 
     --------------* 
         (x2, y2) 

''' 
def draw_rectangle(drawing, coordinates, color='yellow', width=3): 
    #top 
    line_coordinates = [coordinates[0], coordinates[1], coordinates[2], coordinates[1]] 
    drawing.line(line_coordinates, fill=color, width=width) 

    #left 
    line_coordinates = [coordinates[0], coordinates[1], coordinates[0], coordinates[3]] 
    drawing.line(line_coordinates, fill=color, width=width) 

    #right 
    line_coordinates = [coordinates[2], coordinates[1], coordinates[2], coordinates[3]] 
    drawing.line(line_coordinates, fill=color, width=width) 

    #bottom 
    line_coordinates = [coordinates[0], coordinates[3], coordinates[2], coordinates[3]] 
    drawing.line(line_coordinates, fill=color, width=width) 

उत्तर

5

जनहित याचिका के rectanglewidth तर्क का समर्थन नहीं करता है।

मैंने एक अक्षम विधि को बस खेलने के लिए अच्छा लिखा - लेकिन ध्यान दें, रेखा चौड़ाई सीमा के साथ केंद्रित नहीं है।

def draw_rectangle(draw, coordinates, color, width=1): 
    for i in range(width): 
     rect_start = (coordinates[0][0] - i, coordinates[0][1] - i) 
     rect_end = (coordinates[1][0] + i, coordinates[1][1] + i) 
     draw.rectangle((rect_start, rect_end), outline = color) 

# example usage 

im = Image.open(image_path) 
drawing = ImageDraw.Draw(im) 

top_left = (50, 50) 
bottom_right = (100, 100) 

outline_width = 10 
outline_color = "black" 

draw_rectangle(drawing, (top_left, bottom_right), color=outline_color, width=outline_width) 
0

किसी दृश्य को आकर्षित कर सकते हैं rects उदाहरण के लिए:

draw.rectangle([(x, y),(x+w,y+h) ], outline=(0,0,255,255)) 
draw.rectangle([(x+1, y+1),(x+w-1,y+h-1) ], outline=(0,0,255,255)) 
draw.rectangle([(x+2, y+2),(x+w-2,y+h-2) ], outline=(0,0,255,255)) 
... 
कारण का एक पाश और एक समारोह में

संबंधित मुद्दे

 संबंधित मुद्दे