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?
What defenses are there against being summoned by the Gate spell?
Is the language <p,n> belongs to NP class?
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
Do any Labour MPs support no-deal?
Prevent a directory in /tmp from being deleted
Why Is Death Allowed In the Matrix?
Type 1 Error & Type 2 Error's pregnancy test analogy: is it legit?
I probably found a bug with the sudo apt install function
Is the month field really deprecated?
declaring a variable twice in IIFE
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
Has the BBC provided arguments for saying Brexit being cancelled is unlikely?
Why was the small council so happy for Tyrion to become the Master of Coin?
Motorized valve interfering with button?
The Meaning of "Simply a child of her times"
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Mathematical cryptic clues
Underlining section titles
Is there really no realistic way for a skeleton monster to move around without magic?
Can I use wish to become the ruler of all dragons?
What is the offset in a seaplane's hull?
Why is an old chain unsafe?
Is there a familial term for apples and pears?
Is it possible to make sharp wind that can cut stuff from afar?
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