Trying to find the percentage of black pixels in an image (Python 2)How do I find the location of my Python site-packages directory?Finding the index of an item given a list containing it in PythonPython try-elseFind all files in a directory with extension .txt in PythonHow to find if directory exists in PythonGrouping of lines in a text file and further processingPython read specific alternate linesLooping though file to extract linesRemove line character from a text file using Python (With a condition)What's the problem in this car counter problem?
Why don't electromagnetic waves interact with each other?
How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)
If Manufacturer spice model and Datasheet give different values which should I use?
I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine
How can bays and straits be determined in a procedurally generated map?
Is there really no realistic way for a skeleton monster to move around without magic?
I see my dog run
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
"which" command doesn't work / path of Safari?
DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?
How to report a triplet of septets in NMR tabulation?
N.B. ligature in Latex
What is the command to reset a PC without deleting any files
A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?
How do you conduct xenoanthropology after first contact?
Why is the design of haulage companies so “special”?
Set-theoretical foundations of Mathematics with only bounded quantifiers
A Journey Through Space and Time
Is there a familial term for apples and pears?
Book about a traveler who helps planets in need
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
How long does it take to type this?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
What typically incentivizes a professor to change jobs to a lower ranking university?
Trying to find the percentage of black pixels in an image (Python 2)
How do I find the location of my Python site-packages directory?Finding the index of an item given a list containing it in PythonPython try-elseFind all files in a directory with extension .txt in PythonHow to find if directory exists in PythonGrouping of lines in a text file and further processingPython read specific alternate linesLooping though file to extract linesRemove line character from a text file using Python (With a condition)What's the problem in this car counter problem?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
Black pixels are represented by a B while non black pixels are represented by an N. For each image, calculate the percentage of black pixels to the nearest tenth.
I need the output as 80.0 % but I got 0.0, what's wrong with my code? Any advice would be appreciated! Thanks!
line1 = "BBBBBBBBBB"
line2 = "BBNNBBNNBB"
line3 = "BBNNBBNNBB"
line4 = "BBBBBBBBBB"
line5 = "BBBBNNBBBB"
line6 = "BBNBBBBNBB"
line7 = "BBBNNNNBBB"
line8 = "BBBBBBBBBB"
data = ["BBBBBBBBBB", "BBNNBBNNBB", "BBNNBBNNBB", "BBBBBBBBBB", "BBBBNNBBBB", "BBNBBBBNBB", "BBBNNNNBBB", "BBBBBBBBBB"]
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB/numP)*100,1)
print(percentBlack(data))
python python-2.7
add a comment |
Black pixels are represented by a B while non black pixels are represented by an N. For each image, calculate the percentage of black pixels to the nearest tenth.
I need the output as 80.0 % but I got 0.0, what's wrong with my code? Any advice would be appreciated! Thanks!
line1 = "BBBBBBBBBB"
line2 = "BBNNBBNNBB"
line3 = "BBNNBBNNBB"
line4 = "BBBBBBBBBB"
line5 = "BBBBNNBBBB"
line6 = "BBNBBBBNBB"
line7 = "BBBNNNNBBB"
line8 = "BBBBBBBBBB"
data = ["BBBBBBBBBB", "BBNNBBNNBB", "BBNNBBNNBB", "BBBBBBBBBB", "BBBBNNBBBB", "BBNBBBBNBB", "BBBNNNNBBB", "BBBBBBBBBB"]
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB/numP)*100,1)
print(percentBlack(data))
python python-2.7
1
Did you try to cast to floats/doubles before computing the % ? you might be doing an integer division: if the result is between [0, 1] then you might end up with 0 ! What does debug mode tell you about your variables ?
– LoneWanderer
Mar 9 at 3:25
add a comment |
Black pixels are represented by a B while non black pixels are represented by an N. For each image, calculate the percentage of black pixels to the nearest tenth.
I need the output as 80.0 % but I got 0.0, what's wrong with my code? Any advice would be appreciated! Thanks!
line1 = "BBBBBBBBBB"
line2 = "BBNNBBNNBB"
line3 = "BBNNBBNNBB"
line4 = "BBBBBBBBBB"
line5 = "BBBBNNBBBB"
line6 = "BBNBBBBNBB"
line7 = "BBBNNNNBBB"
line8 = "BBBBBBBBBB"
data = ["BBBBBBBBBB", "BBNNBBNNBB", "BBNNBBNNBB", "BBBBBBBBBB", "BBBBNNBBBB", "BBNBBBBNBB", "BBBNNNNBBB", "BBBBBBBBBB"]
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB/numP)*100,1)
print(percentBlack(data))
python python-2.7
Black pixels are represented by a B while non black pixels are represented by an N. For each image, calculate the percentage of black pixels to the nearest tenth.
I need the output as 80.0 % but I got 0.0, what's wrong with my code? Any advice would be appreciated! Thanks!
line1 = "BBBBBBBBBB"
line2 = "BBNNBBNNBB"
line3 = "BBNNBBNNBB"
line4 = "BBBBBBBBBB"
line5 = "BBBBNNBBBB"
line6 = "BBNBBBBNBB"
line7 = "BBBNNNNBBB"
line8 = "BBBBBBBBBB"
data = ["BBBBBBBBBB", "BBNNBBNNBB", "BBNNBBNNBB", "BBBBBBBBBB", "BBBBNNBBBB", "BBNBBBBNBB", "BBBNNNNBBB", "BBBBBBBBBB"]
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB/numP)*100,1)
print(percentBlack(data))
python python-2.7
python python-2.7
edited Mar 9 at 3:46
Siong Thye Goh
1,72911016
1,72911016
asked Mar 9 at 3:15
JennyJenny
154
154
1
Did you try to cast to floats/doubles before computing the % ? you might be doing an integer division: if the result is between [0, 1] then you might end up with 0 ! What does debug mode tell you about your variables ?
– LoneWanderer
Mar 9 at 3:25
add a comment |
1
Did you try to cast to floats/doubles before computing the % ? you might be doing an integer division: if the result is between [0, 1] then you might end up with 0 ! What does debug mode tell you about your variables ?
– LoneWanderer
Mar 9 at 3:25
1
1
Did you try to cast to floats/doubles before computing the % ? you might be doing an integer division: if the result is between [0, 1] then you might end up with 0 ! What does debug mode tell you about your variables ?
– LoneWanderer
Mar 9 at 3:25
Did you try to cast to floats/doubles before computing the % ? you might be doing an integer division: if the result is between [0, 1] then you might end up with 0 ! What does debug mode tell you about your variables ?
– LoneWanderer
Mar 9 at 3:25
add a comment |
1 Answer
1
active
oldest
votes
Your code runs fine in Python 3.
I believe your code is written in Python 2 of which the division operator of two int produces an int. Here is a possible fix.
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB * 100.0/numP),1)
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
1
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073646%2ftrying-to-find-the-percentage-of-black-pixels-in-an-image-python-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your code runs fine in Python 3.
I believe your code is written in Python 2 of which the division operator of two int produces an int. Here is a possible fix.
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB * 100.0/numP),1)
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
1
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
add a comment |
Your code runs fine in Python 3.
I believe your code is written in Python 2 of which the division operator of two int produces an int. Here is a possible fix.
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB * 100.0/numP),1)
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
1
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
add a comment |
Your code runs fine in Python 3.
I believe your code is written in Python 2 of which the division operator of two int produces an int. Here is a possible fix.
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB * 100.0/numP),1)
Your code runs fine in Python 3.
I believe your code is written in Python 2 of which the division operator of two int produces an int. Here is a possible fix.
def percentBlack(data):
numB = 0
numP = 0
for line in data:
for pixel in line:
if pixel == "B":
numB += 1
numP += 1
return round((numB * 100.0/numP),1)
answered Mar 9 at 3:30
Siong Thye GohSiong Thye Goh
1,72911016
1,72911016
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
1
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
add a comment |
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
1
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
Yup I need it in Python 2. Great edit! That's what I need. So if I need a % symbol, do I just use this print statement: print(percentBlack(data)), "%"? Any other suggestion?
– Jenny
Mar 9 at 3:34
1
1
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
That should work, though I usually do print(str(percentBlack(data))+ "%")
– Siong Thye Goh
Mar 9 at 3:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073646%2ftrying-to-find-the-percentage-of-black-pixels-in-an-image-python-2%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Did you try to cast to floats/doubles before computing the % ? you might be doing an integer division: if the result is between [0, 1] then you might end up with 0 ! What does debug mode tell you about your variables ?
– LoneWanderer
Mar 9 at 3:25