Domanda

The built-in MacOS screen capture program (command-shift-4) has a nice feature where you can hit the spacebar and capture just a window, like this:

enter image description here

I would like to programmatically look at a directory of images (they are PNGs), determinate if they have the shadow, and automatically crop it. I need this to run on a Mac. I'd like to write this in Python. I am told that Pillow is the correct way to manage images in Python now, but I'm not sure how to read individual pixel and to crop images.

È stato utile?

Soluzione 2

Here is code that uses Python Image Library and Python 2.7 to do the trick:

#!/usr/bin/env                                                                                                                       
# Removes the shadow from MacOS-Generated screen shots.                                                                              

import Image,os

if __name__=="__main__":
    image = Image.open(os.sys.argv[1])
    image = image.convert('RGBA')

    (width,height) = image.size
    def find_first_non_alpha_x():
        for i in range(width):
            if image.getpixel((i,height/2))[3]==255:
                return i
        raise RuntimeError("No non-alpha pixels on midline")

    def find_last_non_alpha_x():
        for i in range(width-1,0,-1):
            if image.getpixel((i,height/2))[3]==255:
                return i
        raise RuntimeError("No non-alpha pixels on midline")

    def find_first_non_alpha_y():
        for i in range(height):
            if image.getpixel((width/2,i))[3]==255:
                return i
        raise RuntimeError("No non-alpha pixels on midline")

    def find_last_non_alpha_y():
        for i in range(height-1,0,-1):
            if image.getpixel((width/2,i))[3]==255:
                return i
        raise RuntimeError("No non-alpha pixels on midline")

    x1 = find_first_non_alpha_x()
    y1 = find_first_non_alpha_y()
    x2  = find_last_non_alpha_x()
    y2  = find_last_non_alpha_y()

    y = image.crop((x1-1,y1-1,x2+1,y2+1))
    y.save(os.sys.argv[1]+"-cropped.png")

Altri suggerimenti

Here are some recommendations regardless of the library you will be using.

There are invariants on the window: 4 corners, title bar with mostly uniform color and 3 disk-shaped buttons.

If you can detect the buttons and the title bar, you can easily find the top corners. The bottom corners are symmetrical to the top corners.

A possible solution

  • Apply Hough transform to find circles
  • Find 3 consecutive circles along the horizontal axis (the buttons)
  • Apply Hough transform to find vertical and horizontal lines
  • Find a quad containing the 3 circles (title bar)
  • The 2 top corners of the window are located around the top corners of the title bar.
  • Form a patch by taking a neighborhood around a corner
  • Apply an edge detection algorithm in the patch
  • Reflect the pixels of the patch vertically
  • Apply patch matching vertically. For example with DP
  • Repeat the matching for the 2 top corners to find the bottom ones

With the 4 corner you know the bounding box of the window and you can solve the cropping problem

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top