Answered step by step
Verified Expert Solution
Question
1 Approved Answer
def setup ( ) : global source, destination, ti , e , tf size ( 8 0 0 0 , 6 0 0 0 )
def setup:
global source, destination, ti e tf
size
source loadImageimagejpeg"
# The destination image is created as a blank image the same size as the source.
destination createImagesourcewidth, source.height, RGB
e
ti
tf adptthti
whileabsti tfe:
temp adptthtf
ti tf
tf temp
printtf
img binarizetf
img cropimg
imagesource
imageimg source.height
imageimgsourceheight
def adpttht:
e
ti
r
g
g
for i in rangelenr:
if ri ti:
gappendri
else:
gappendri
sum sumg
sum sumg
l leng
l leng
a sum l if l else
a sum l if l else
tf a a if l and l else
return g g sum sum l l a a tf
# Example usage
g g sum sum l l a a tf adptth
printg: g
printg: g
printSum of elements in g: sum
printSum of elements in g: sum
printLength of g: l
printLength of g: l
printAverage of elements in g: a
printAverage of elements in g: a
printThreshold: tf
def binarizet:
source.loadPixels
destination.loadPixels
for x in rangesourcewidth:
for y in rangesourceheight:
loc x ysourcewidth
r brightnesssourcepixelsloc
#printr
# Test the brightness against the threshold
if r tf:
destination.pixelsloc color
else:
destination.pixelsloc color
destination.updatePixels
returndestination
def cropdestination:
xl
xr
yt
yb
destination.loadPixels
found False
for x in rangedestinationwidth:
for y in rangedestinationheight:
if found False:
loc loc x ysourcewidth
r brightnessdestinationpixelsloc
#printr
if r :
xl x
found True
found False
for x in reverserangedestinationwidth:
for y in rangedestinationheight:
if found False:
loc loc x ysourcewidth
r brightnessdestinationpixelsloc
#printr
if r :
xr x
found True
found False
for y in rangedestinationheight:
for x in rangedestinationwidth:
if found False:
loc loc x ysourcewidth
r brightnessdestinationpixelsloc
#printr
if r :
yt y
found True
found False
for y in reverserangedestinationheight:
for x in rangedestinationwidth:
if found False:
loc loc x ysourcewidth
r brightnessdestinationpixelsloc
#printr
if r :
yb y
found True
printxl xr yt yb
i destination.getxl yt xrxl ybyt
returni
Alternate code for this in python
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started