Python - Circle patter [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
What is the best way to compare floats for almost-equality in Python?
12 answers
I would like to create a circle in zeros matrix by changing specific positions from 0 to 1. Unfortunately, something is wrong in my if statement I cannot figure it out. Thanks for helping me!
img = np.zeros((20,20))
def convert2file(image, newfile):
img = Image.new('RGB', (len(image), len(image[0])), "black")
pixels = img.load()
for i in range(len(image)):
for j in range (len(image[i])):
if image[i][j] == 0:
pixels[j,i] = black
else:
pixels[j,i] = white
save2file(img, newfile)
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j - len(image)/2)**2 + (i - len(image)/2)**2 - len(image)/2**2) == 0.5**2:
ret[i][j] = 1
return ret
draw_pic(gen_circle(img))
python matrix
marked as duplicate by wwii, Community♦ Nov 22 at 3:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
|
show 4 more comments
up vote
0
down vote
favorite
This question already has an answer here:
What is the best way to compare floats for almost-equality in Python?
12 answers
I would like to create a circle in zeros matrix by changing specific positions from 0 to 1. Unfortunately, something is wrong in my if statement I cannot figure it out. Thanks for helping me!
img = np.zeros((20,20))
def convert2file(image, newfile):
img = Image.new('RGB', (len(image), len(image[0])), "black")
pixels = img.load()
for i in range(len(image)):
for j in range (len(image[i])):
if image[i][j] == 0:
pixels[j,i] = black
else:
pixels[j,i] = white
save2file(img, newfile)
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j - len(image)/2)**2 + (i - len(image)/2)**2 - len(image)/2**2) == 0.5**2:
ret[i][j] = 1
return ret
draw_pic(gen_circle(img))
python matrix
marked as duplicate by wwii, Community♦ Nov 22 at 3:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
So, what is wrong exactly?
– Andreas
Nov 22 at 1:38
Can you provide what is the expected result?
– Andreas
Nov 22 at 1:48
1
Looks like the condition always evaluatesFalse
and None of the pixels get changed to one. You rarely will get two floating point calculations to be equivalent.
– wwii
Nov 22 at 1:50
Hard to find the proper image. The expected result is the circle from side to side, top to bottom which is made by 1. The area outside the circle stays 0.
– Frankie
Nov 22 at 1:51
1
So, is it a filled circle or just the outline?
– Andreas
Nov 22 at 2:02
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
What is the best way to compare floats for almost-equality in Python?
12 answers
I would like to create a circle in zeros matrix by changing specific positions from 0 to 1. Unfortunately, something is wrong in my if statement I cannot figure it out. Thanks for helping me!
img = np.zeros((20,20))
def convert2file(image, newfile):
img = Image.new('RGB', (len(image), len(image[0])), "black")
pixels = img.load()
for i in range(len(image)):
for j in range (len(image[i])):
if image[i][j] == 0:
pixels[j,i] = black
else:
pixels[j,i] = white
save2file(img, newfile)
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j - len(image)/2)**2 + (i - len(image)/2)**2 - len(image)/2**2) == 0.5**2:
ret[i][j] = 1
return ret
draw_pic(gen_circle(img))
python matrix
This question already has an answer here:
What is the best way to compare floats for almost-equality in Python?
12 answers
I would like to create a circle in zeros matrix by changing specific positions from 0 to 1. Unfortunately, something is wrong in my if statement I cannot figure it out. Thanks for helping me!
img = np.zeros((20,20))
def convert2file(image, newfile):
img = Image.new('RGB', (len(image), len(image[0])), "black")
pixels = img.load()
for i in range(len(image)):
for j in range (len(image[i])):
if image[i][j] == 0:
pixels[j,i] = black
else:
pixels[j,i] = white
save2file(img, newfile)
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j - len(image)/2)**2 + (i - len(image)/2)**2 - len(image)/2**2) == 0.5**2:
ret[i][j] = 1
return ret
draw_pic(gen_circle(img))
This question already has an answer here:
What is the best way to compare floats for almost-equality in Python?
12 answers
python matrix
python matrix
edited Nov 22 at 1:39
asked Nov 22 at 1:35
Frankie
144
144
marked as duplicate by wwii, Community♦ Nov 22 at 3:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by wwii, Community♦ Nov 22 at 3:08
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
So, what is wrong exactly?
– Andreas
Nov 22 at 1:38
Can you provide what is the expected result?
– Andreas
Nov 22 at 1:48
1
Looks like the condition always evaluatesFalse
and None of the pixels get changed to one. You rarely will get two floating point calculations to be equivalent.
– wwii
Nov 22 at 1:50
Hard to find the proper image. The expected result is the circle from side to side, top to bottom which is made by 1. The area outside the circle stays 0.
– Frankie
Nov 22 at 1:51
1
So, is it a filled circle or just the outline?
– Andreas
Nov 22 at 2:02
|
show 4 more comments
So, what is wrong exactly?
– Andreas
Nov 22 at 1:38
Can you provide what is the expected result?
– Andreas
Nov 22 at 1:48
1
Looks like the condition always evaluatesFalse
and None of the pixels get changed to one. You rarely will get two floating point calculations to be equivalent.
– wwii
Nov 22 at 1:50
Hard to find the proper image. The expected result is the circle from side to side, top to bottom which is made by 1. The area outside the circle stays 0.
– Frankie
Nov 22 at 1:51
1
So, is it a filled circle or just the outline?
– Andreas
Nov 22 at 2:02
So, what is wrong exactly?
– Andreas
Nov 22 at 1:38
So, what is wrong exactly?
– Andreas
Nov 22 at 1:38
Can you provide what is the expected result?
– Andreas
Nov 22 at 1:48
Can you provide what is the expected result?
– Andreas
Nov 22 at 1:48
1
1
Looks like the condition always evaluates
False
and None of the pixels get changed to one. You rarely will get two floating point calculations to be equivalent.– wwii
Nov 22 at 1:50
Looks like the condition always evaluates
False
and None of the pixels get changed to one. You rarely will get two floating point calculations to be equivalent.– wwii
Nov 22 at 1:50
Hard to find the proper image. The expected result is the circle from side to side, top to bottom which is made by 1. The area outside the circle stays 0.
– Frankie
Nov 22 at 1:51
Hard to find the proper image. The expected result is the circle from side to side, top to bottom which is made by 1. The area outside the circle stays 0.
– Frankie
Nov 22 at 1:51
1
1
So, is it a filled circle or just the outline?
– Andreas
Nov 22 at 2:02
So, is it a filled circle or just the outline?
– Andreas
Nov 22 at 2:02
|
show 4 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You need to fix the calculation for a couple reasons:
It is very difficult to get an arbitrary calculation to match a floating point value
Almost all of your calculation result in a value that is larger than 0
(some even exceed 100
), so most of it will be equal to false
The value of i
and j
is not balanced (as you started from 0
and end at 19
)
You need to select an arbitrary value as the condition (by looking at each of the calculation result, or by trial-and-error) and make the condition to be less than that (to fill the circle up).
import numpy as np
from math import fabs
img = np.zeros((20,20))
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j + 0.5 - len(image)/2)**2 + (i + 0.5 - len(image)/2)**2 - len(image)/2**2) <= 86:
ret[i][j] = 1
return ret
print(gen_circle(img))
add a comment |
up vote
0
down vote
How about this code:
def gen_circle(image):
ret = np.copy(image)
radius = len(image)/2
radius_sq = radius**2
for i in range(len(image)):
for j in range(len(image[i])):
if (i-radius)**2 + (j-radius)**2 <= radius_sq:
ret[i][j] = 1
return ret
1
ret[i][j] = 1
-->ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…
– wwii
Nov 22 at 2:12
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You need to fix the calculation for a couple reasons:
It is very difficult to get an arbitrary calculation to match a floating point value
Almost all of your calculation result in a value that is larger than 0
(some even exceed 100
), so most of it will be equal to false
The value of i
and j
is not balanced (as you started from 0
and end at 19
)
You need to select an arbitrary value as the condition (by looking at each of the calculation result, or by trial-and-error) and make the condition to be less than that (to fill the circle up).
import numpy as np
from math import fabs
img = np.zeros((20,20))
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j + 0.5 - len(image)/2)**2 + (i + 0.5 - len(image)/2)**2 - len(image)/2**2) <= 86:
ret[i][j] = 1
return ret
print(gen_circle(img))
add a comment |
up vote
0
down vote
accepted
You need to fix the calculation for a couple reasons:
It is very difficult to get an arbitrary calculation to match a floating point value
Almost all of your calculation result in a value that is larger than 0
(some even exceed 100
), so most of it will be equal to false
The value of i
and j
is not balanced (as you started from 0
and end at 19
)
You need to select an arbitrary value as the condition (by looking at each of the calculation result, or by trial-and-error) and make the condition to be less than that (to fill the circle up).
import numpy as np
from math import fabs
img = np.zeros((20,20))
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j + 0.5 - len(image)/2)**2 + (i + 0.5 - len(image)/2)**2 - len(image)/2**2) <= 86:
ret[i][j] = 1
return ret
print(gen_circle(img))
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You need to fix the calculation for a couple reasons:
It is very difficult to get an arbitrary calculation to match a floating point value
Almost all of your calculation result in a value that is larger than 0
(some even exceed 100
), so most of it will be equal to false
The value of i
and j
is not balanced (as you started from 0
and end at 19
)
You need to select an arbitrary value as the condition (by looking at each of the calculation result, or by trial-and-error) and make the condition to be less than that (to fill the circle up).
import numpy as np
from math import fabs
img = np.zeros((20,20))
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j + 0.5 - len(image)/2)**2 + (i + 0.5 - len(image)/2)**2 - len(image)/2**2) <= 86:
ret[i][j] = 1
return ret
print(gen_circle(img))
You need to fix the calculation for a couple reasons:
It is very difficult to get an arbitrary calculation to match a floating point value
Almost all of your calculation result in a value that is larger than 0
(some even exceed 100
), so most of it will be equal to false
The value of i
and j
is not balanced (as you started from 0
and end at 19
)
You need to select an arbitrary value as the condition (by looking at each of the calculation result, or by trial-and-error) and make the condition to be less than that (to fill the circle up).
import numpy as np
from math import fabs
img = np.zeros((20,20))
def gen_circle(image):
ret = np.copy(image)
for i in range(len(image)):
for j in range(len(image[i])):
if fabs((j + 0.5 - len(image)/2)**2 + (i + 0.5 - len(image)/2)**2 - len(image)/2**2) <= 86:
ret[i][j] = 1
return ret
print(gen_circle(img))
answered Nov 22 at 2:11
Andreas
1,8111618
1,8111618
add a comment |
add a comment |
up vote
0
down vote
How about this code:
def gen_circle(image):
ret = np.copy(image)
radius = len(image)/2
radius_sq = radius**2
for i in range(len(image)):
for j in range(len(image[i])):
if (i-radius)**2 + (j-radius)**2 <= radius_sq:
ret[i][j] = 1
return ret
1
ret[i][j] = 1
-->ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…
– wwii
Nov 22 at 2:12
add a comment |
up vote
0
down vote
How about this code:
def gen_circle(image):
ret = np.copy(image)
radius = len(image)/2
radius_sq = radius**2
for i in range(len(image)):
for j in range(len(image[i])):
if (i-radius)**2 + (j-radius)**2 <= radius_sq:
ret[i][j] = 1
return ret
1
ret[i][j] = 1
-->ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…
– wwii
Nov 22 at 2:12
add a comment |
up vote
0
down vote
up vote
0
down vote
How about this code:
def gen_circle(image):
ret = np.copy(image)
radius = len(image)/2
radius_sq = radius**2
for i in range(len(image)):
for j in range(len(image[i])):
if (i-radius)**2 + (j-radius)**2 <= radius_sq:
ret[i][j] = 1
return ret
How about this code:
def gen_circle(image):
ret = np.copy(image)
radius = len(image)/2
radius_sq = radius**2
for i in range(len(image)):
for j in range(len(image[i])):
if (i-radius)**2 + (j-radius)**2 <= radius_sq:
ret[i][j] = 1
return ret
answered Nov 22 at 2:10
John Anderson
2,2481412
2,2481412
1
ret[i][j] = 1
-->ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…
– wwii
Nov 22 at 2:12
add a comment |
1
ret[i][j] = 1
-->ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…
– wwii
Nov 22 at 2:12
1
1
ret[i][j] = 1
--> ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…– wwii
Nov 22 at 2:12
ret[i][j] = 1
--> ret[i,j] = 1
... docs.scipy.org/doc/numpy/user/…– wwii
Nov 22 at 2:12
add a comment |
So, what is wrong exactly?
– Andreas
Nov 22 at 1:38
Can you provide what is the expected result?
– Andreas
Nov 22 at 1:48
1
Looks like the condition always evaluates
False
and None of the pixels get changed to one. You rarely will get two floating point calculations to be equivalent.– wwii
Nov 22 at 1:50
Hard to find the proper image. The expected result is the circle from side to side, top to bottom which is made by 1. The area outside the circle stays 0.
– Frankie
Nov 22 at 1:51
1
So, is it a filled circle or just the outline?
– Andreas
Nov 22 at 2:02