Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Sun eclipse crop by top border
import os, sys
from PIL import Image, ImageOps
from os import path
import imghdr
inputDir = "input"
outputDir = "output"
sunSize = 0
imgBoarder = 200
def treshold(x):
if x < 100:
x = 0
return x
def suncrop(image, border):
sunBorders = image.point(treshold).getbbox()
global sunSize
# get sun size from the first image where the sun is complete
if sunSize == 0:
sunSize = sunBorders[3] - sunBorders[1]
print "Sunsize %d px" % sunSize
# calculcate top point x from sun by cropping first line and divide left and right border /2
sunFirstLinePoints = (0, sunBorders[1], im.size[0], sunBorders[1]+1)
sunFirstLineCrop = im.crop(sunFirstLinePoints)
sunFirstLineBoarder = sunFirstLineCrop.point(treshold).getbbox()
topBorder = sunBorders[1] - imgBoarder
bottomSunBorder = sunBorders[1] + imgBoarder + sunSize
leftSunBoarder = ((sunFirstLineBoarder[0] + sunFirstLineBoarder[2])/2) - (sunSize/2) - imgBoarder
rightSunBoarder = ((sunFirstLineBoarder[0] + sunFirstLineBoarder[2])/2) + (sunSize/2) + imgBoarder
# crop whole image only by top point and sun size as reference
sunBorderBorders = (leftSunBoarder, topBorder, rightSunBoarder, bottomSunBorder)
return image.crop(sunBorderBorders)
sunSize = 0
files = [f for f in os.listdir(inputDir) if f.endswith(".jpg")]
files.sort()
for img in files:
im = Image.open("%s/%s" % (inputDir, img))
imtrim = suncrop(im, imgBoarder)
outfile = "%s/%s" % (outputDir, img)
print "Saving to %s" % outfile
imtrim.save(outfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment