R
R
RuslanCC2013-05-27 08:02:33
Image processing
RuslanCC, 2013-05-27 08:02:33

Automatic cropping of excess background in a photo?

Hello!
Please tell me, there are about 2000 similar photos of furniture:
6f864e562721cafe56887fe743ea8fb2.png
The problem is that all objects are of different sizes, but you need to bring them to a common view.
All photos are on a white background. The gray frame is the size of the photo, the red one is how you need to crop it. Those. the object must be inscribed in a rectangle and sprinkle the excess.
Can this be done automatically? It is also desirable to add 100px of white background after the crop.
Thank you!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
P
Paul Smith @Paul, 2013-05-27
Smith

Here is a python script (2.7, PIL):

from PIL import Image
import math
import sys

way = '1.png'
print sys.argv[1]
way = sys.argv[1]
image = Image.open(way)
width = image.size[0]
height = image.size[1]

alpha = image.getpixel((2,2))



left = width
right = 0
top = height
bottom = 0
print str(alpha)
for x in range(width):
    for y in range(height):
        rng = math.sqrt(math.pow(image.getpixel((x,y))[0]-alpha[0],2)+math.pow(image.getpixel((x,y))[1]-alpha[1],2)+math.pow(image.getpixel((x,y))[2]-alpha[2],2))
        #print str(rng)
        if rng > 89:
            
            
            if x<left:
                left = x
            if x>right:
                right = x
            if y<top:
                top = y
            if y>bottom:
                bottom = y
print str(left)+ ','+str(top)+'   '+str(right)+','+str(bottom)                

width = right-left
height = bottom-top
img = Image.new("RGBA", (width,height), (0,0,0,0))
for x in range(width):
    for y in range(height):
        img.putpixel((x,y),image.getpixel((x+left,y+top)))


img.save("test1.png", "PNG")

print "Done!"


As a parameter, it receives the name of the file, crops it, and creates a file crop_name.extension .
The result of the work:
This:

Turns into this:

Feed this script to a bat file, which for all files in the folder will apply it and you will be happy.

A
Alexander, 2013-05-27
@Palehin

In Photoshop:
Open one picture that needs to be cropped, go to the menu Window -> Actions, Create a new Action (Create New Action), recording of actions will automatically start. Go to Image -> Trim, check Top Left Pixel Color, press ok. Stop recording Action (stop button). It should look like the picture:

Next, go to File -> Automate -> Batch,
In the Action field - specify the Action you just created
In the Source field - specify Folder, press the Choose button, select your folder with pictures.
In the Destination field - specify Save and Close
and click OK.

M
Maxim Timokhin, 2013-05-27
@timokhin

You can record an action in Photoshop, something like: Magic Wand (in the corner) -> Cut -> Select All -> Copy -> New File -> Paste -> Save. And run through the Image Processor.

6
65520, 2013-05-27
@65520

crop
www.imagemagick.org/script/command-line-options.php#trim
frame
www.imagemagick.org/script/command-line-options.php#border

V
Vitaly Zheltyakov, 2013-05-27
@VitaZheltyakov

gimp + bimp . Use the auto framing procedure.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question