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;








2















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))









share|improve this question



















  • 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


















2















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))









share|improve this question



















  • 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














2












2








2








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))









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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













  • 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













1 Answer
1






active

oldest

votes


















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)





share|improve this answer























  • 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











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
);



);













draft saved

draft discarded


















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









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)





share|improve this answer























  • 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















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)





share|improve this answer























  • 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













1












1








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)





share|improve this answer













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)






share|improve this answer












share|improve this answer



share|improve this answer










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

















  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived