Help with DRY

I just wrote something that clearly repeats itself too much, but can't think through how to abstract it so that it doesn't.

Given an image, and knowing the height and width of the image, I want to know if the entire border is the same color (bg_blk).

(This uses Pillow, but knowing that doesn't really matter to helping think through control flow logic.)

for x in range width:
    if img.getpixel((x,0)) != bg_blk:
        fix = False
        break

    if img.getpixel((x,height)) != bg_blk:
        fix = False
        break

for y in range height:
    if img.getpixel((0,y)) != bg_blk:
        fix = False
        break

    if img.getpixel((width,y)) != bg_blk:
        fix = False
        break

Posted Jan 5, 2017, 11:32 PM UTC

513
Loading comments…