Answer the question
In order to leave comments, you need to log in
Automatic cropping of excess background in a photo?
Hello!
Please tell me, there are about 2000 similar photos of furniture:
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
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!"
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.
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.
crop
www.imagemagick.org/script/command-line-options.php#trim
frame
www.imagemagick.org/script/command-line-options.php#border
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question