Change all pixels that lie in a given color range to a new color











up vote
4
down vote

favorite












This is what I have so far but it's not perfect... Any input would be very helpful!



def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = list(img.getdata())
red_list=
for i in pixels:
R= i[0]
G=i[1]
B=i[2]

if R >= from_color[0] and R<= to_color[0] and G >=from_color[1]
and G<= to_color[1] and B >= from_color[2] and B<=
to_color[2]:
red_list.append(target_color)
else:
red_list.append((round(R),round(G),round(B)))

red_image = Image.new(img.mode,img.size)
red_image.putdata(red_list)
return red_image









share|improve this question









New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Your if statement is split over three lines, in a way that I think is not valid in Python. If you put brackets around it, then you can break it up across lines: stackoverflow.com/questions/5253348/…
    – Cris Luengo
    9 hours ago








  • 1




    Also clarify what imports apply. What are Image and round?
    – 200_success
    6 hours ago






  • 1




    @200_success round is a built-in function. For the Image my guess is it comes from pillow.
    – Arthur Havlicek
    5 hours ago

















up vote
4
down vote

favorite












This is what I have so far but it's not perfect... Any input would be very helpful!



def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = list(img.getdata())
red_list=
for i in pixels:
R= i[0]
G=i[1]
B=i[2]

if R >= from_color[0] and R<= to_color[0] and G >=from_color[1]
and G<= to_color[1] and B >= from_color[2] and B<=
to_color[2]:
red_list.append(target_color)
else:
red_list.append((round(R),round(G),round(B)))

red_image = Image.new(img.mode,img.size)
red_image.putdata(red_list)
return red_image









share|improve this question









New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Your if statement is split over three lines, in a way that I think is not valid in Python. If you put brackets around it, then you can break it up across lines: stackoverflow.com/questions/5253348/…
    – Cris Luengo
    9 hours ago








  • 1




    Also clarify what imports apply. What are Image and round?
    – 200_success
    6 hours ago






  • 1




    @200_success round is a built-in function. For the Image my guess is it comes from pillow.
    – Arthur Havlicek
    5 hours ago















up vote
4
down vote

favorite









up vote
4
down vote

favorite











This is what I have so far but it's not perfect... Any input would be very helpful!



def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = list(img.getdata())
red_list=
for i in pixels:
R= i[0]
G=i[1]
B=i[2]

if R >= from_color[0] and R<= to_color[0] and G >=from_color[1]
and G<= to_color[1] and B >= from_color[2] and B<=
to_color[2]:
red_list.append(target_color)
else:
red_list.append((round(R),round(G),round(B)))

red_image = Image.new(img.mode,img.size)
red_image.putdata(red_list)
return red_image









share|improve this question









New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











This is what I have so far but it's not perfect... Any input would be very helpful!



def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = list(img.getdata())
red_list=
for i in pixels:
R= i[0]
G=i[1]
B=i[2]

if R >= from_color[0] and R<= to_color[0] and G >=from_color[1]
and G<= to_color[1] and B >= from_color[2] and B<=
to_color[2]:
red_list.append(target_color)
else:
red_list.append((round(R),round(G),round(B)))

red_image = Image.new(img.mode,img.size)
red_image.putdata(red_list)
return red_image






python python-3.x image






share|improve this question









New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 6 hours ago









200_success

127k15148410




127k15148410






New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 11 hours ago









Victoria

211




211




New contributor




Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Victoria is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    Your if statement is split over three lines, in a way that I think is not valid in Python. If you put brackets around it, then you can break it up across lines: stackoverflow.com/questions/5253348/…
    – Cris Luengo
    9 hours ago








  • 1




    Also clarify what imports apply. What are Image and round?
    – 200_success
    6 hours ago






  • 1




    @200_success round is a built-in function. For the Image my guess is it comes from pillow.
    – Arthur Havlicek
    5 hours ago
















  • 2




    Your if statement is split over three lines, in a way that I think is not valid in Python. If you put brackets around it, then you can break it up across lines: stackoverflow.com/questions/5253348/…
    – Cris Luengo
    9 hours ago








  • 1




    Also clarify what imports apply. What are Image and round?
    – 200_success
    6 hours ago






  • 1




    @200_success round is a built-in function. For the Image my guess is it comes from pillow.
    – Arthur Havlicek
    5 hours ago










2




2




Your if statement is split over three lines, in a way that I think is not valid in Python. If you put brackets around it, then you can break it up across lines: stackoverflow.com/questions/5253348/…
– Cris Luengo
9 hours ago






Your if statement is split over three lines, in a way that I think is not valid in Python. If you put brackets around it, then you can break it up across lines: stackoverflow.com/questions/5253348/…
– Cris Luengo
9 hours ago






1




1




Also clarify what imports apply. What are Image and round?
– 200_success
6 hours ago




Also clarify what imports apply. What are Image and round?
– 200_success
6 hours ago




1




1




@200_success round is a built-in function. For the Image my guess is it comes from pillow.
– Arthur Havlicek
5 hours ago






@200_success round is a built-in function. For the Image my guess is it comes from pillow.
– Arthur Havlicek
5 hours ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Improving the general implementation



Generally we want to avoid appending to a list too many times if we can help it. This is because lists are dynamic arrays, so appends can be unnecessarily expensive (though the dynamic part helps with the efficiency). Instead, when we know the size of the input set (i.e. the number of pixels in the image), we can take advantage of that information. But in this case, that will be unnecessary because Image provides us with some handy helper methods to handle this very situation:



from PIL import Image

def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if all(pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3)):
pixels[i, j] = target_color + (255,)
return img


Assigning pixels to img.load() allows us to take advantage of directly editing the image. Since copying the image into another list was unnecessary originally (to clarify, the img variable doesn't affect the file on disk that img originated from, i.e. the file at location filename), editing directly is the best way.



Another general improvement I've taken advantage of in this implementation is using range to check if each of the RGB values of a pixel are between the two pixel boundaries of the input arguments. all() serves the purpose that the and statements in the original version served: it requires that all the pixels compared in the iterator pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3) are in the required range for the condition to be true.



Going beyond the current form



While I've shown you a way to use your current parameter requirements in a more efficient implementation, I think there are some improvements you can make so picture_reset_pixels has more functionality. I think combining from_color, to_color, and target_color into a single dict parameter color_replacements could make this more versatile (where the key-value pairs would derive something like {(from_color, to_color): target_color}; you could even create a custom ColorRange class to encapsulate the idea of a range of colors).



There's also another issue I find with your current implementation: it can only support certain image types. I tried your algorithm on a GIF file and it failed because the pixels in a GIF file are ints, not four-tuples like PNGs. You might want to consider investigating that further if you want to support multiple image types.






share|improve this answer























  • in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
    – Reinderien
    1 hour ago


















up vote
0
down vote













The code is overall simple and good but could be improved in a few things that would make it a bit shorter and more readable





Inside the for loop, in the if / else, you are repeating append in both of your cases. You could do instead



to_add = (round(R), round(G), round(B))
if condition:
to_add = target_color
red_list.append(to_add)




On the condition, you can also refactor using chained comparison to improve readability, preferably in ascending order. If you break that line, do so aligning conditionals as follow :



if from_color[0] <= R <= to_color[0] and 
from_color[1] <= G <= to_color[1] and
from_color[2] <= B <= to_color[2]:


Alternatively, but sightly less explicit, you can iterate over i.



if all((from_color[j] <= i[j] <= to_color[j] for j in range(3)))




R, G, B initialisation can be made a single line:



R, G, B = i


or, if working with a 4 tuples:



R, G, B, _ = i





share|improve this answer










New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
    – Reinderien
    1 hour ago











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

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: "196"
};
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',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});






Victoria is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207839%2fchange-all-pixels-that-lie-in-a-given-color-range-to-a-new-color%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Improving the general implementation



Generally we want to avoid appending to a list too many times if we can help it. This is because lists are dynamic arrays, so appends can be unnecessarily expensive (though the dynamic part helps with the efficiency). Instead, when we know the size of the input set (i.e. the number of pixels in the image), we can take advantage of that information. But in this case, that will be unnecessary because Image provides us with some handy helper methods to handle this very situation:



from PIL import Image

def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if all(pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3)):
pixels[i, j] = target_color + (255,)
return img


Assigning pixels to img.load() allows us to take advantage of directly editing the image. Since copying the image into another list was unnecessary originally (to clarify, the img variable doesn't affect the file on disk that img originated from, i.e. the file at location filename), editing directly is the best way.



Another general improvement I've taken advantage of in this implementation is using range to check if each of the RGB values of a pixel are between the two pixel boundaries of the input arguments. all() serves the purpose that the and statements in the original version served: it requires that all the pixels compared in the iterator pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3) are in the required range for the condition to be true.



Going beyond the current form



While I've shown you a way to use your current parameter requirements in a more efficient implementation, I think there are some improvements you can make so picture_reset_pixels has more functionality. I think combining from_color, to_color, and target_color into a single dict parameter color_replacements could make this more versatile (where the key-value pairs would derive something like {(from_color, to_color): target_color}; you could even create a custom ColorRange class to encapsulate the idea of a range of colors).



There's also another issue I find with your current implementation: it can only support certain image types. I tried your algorithm on a GIF file and it failed because the pixels in a GIF file are ints, not four-tuples like PNGs. You might want to consider investigating that further if you want to support multiple image types.






share|improve this answer























  • in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
    – Reinderien
    1 hour ago















up vote
1
down vote













Improving the general implementation



Generally we want to avoid appending to a list too many times if we can help it. This is because lists are dynamic arrays, so appends can be unnecessarily expensive (though the dynamic part helps with the efficiency). Instead, when we know the size of the input set (i.e. the number of pixels in the image), we can take advantage of that information. But in this case, that will be unnecessary because Image provides us with some handy helper methods to handle this very situation:



from PIL import Image

def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if all(pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3)):
pixels[i, j] = target_color + (255,)
return img


Assigning pixels to img.load() allows us to take advantage of directly editing the image. Since copying the image into another list was unnecessary originally (to clarify, the img variable doesn't affect the file on disk that img originated from, i.e. the file at location filename), editing directly is the best way.



Another general improvement I've taken advantage of in this implementation is using range to check if each of the RGB values of a pixel are between the two pixel boundaries of the input arguments. all() serves the purpose that the and statements in the original version served: it requires that all the pixels compared in the iterator pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3) are in the required range for the condition to be true.



Going beyond the current form



While I've shown you a way to use your current parameter requirements in a more efficient implementation, I think there are some improvements you can make so picture_reset_pixels has more functionality. I think combining from_color, to_color, and target_color into a single dict parameter color_replacements could make this more versatile (where the key-value pairs would derive something like {(from_color, to_color): target_color}; you could even create a custom ColorRange class to encapsulate the idea of a range of colors).



There's also another issue I find with your current implementation: it can only support certain image types. I tried your algorithm on a GIF file and it failed because the pixels in a GIF file are ints, not four-tuples like PNGs. You might want to consider investigating that further if you want to support multiple image types.






share|improve this answer























  • in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
    – Reinderien
    1 hour ago













up vote
1
down vote










up vote
1
down vote









Improving the general implementation



Generally we want to avoid appending to a list too many times if we can help it. This is because lists are dynamic arrays, so appends can be unnecessarily expensive (though the dynamic part helps with the efficiency). Instead, when we know the size of the input set (i.e. the number of pixels in the image), we can take advantage of that information. But in this case, that will be unnecessary because Image provides us with some handy helper methods to handle this very situation:



from PIL import Image

def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if all(pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3)):
pixels[i, j] = target_color + (255,)
return img


Assigning pixels to img.load() allows us to take advantage of directly editing the image. Since copying the image into another list was unnecessary originally (to clarify, the img variable doesn't affect the file on disk that img originated from, i.e. the file at location filename), editing directly is the best way.



Another general improvement I've taken advantage of in this implementation is using range to check if each of the RGB values of a pixel are between the two pixel boundaries of the input arguments. all() serves the purpose that the and statements in the original version served: it requires that all the pixels compared in the iterator pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3) are in the required range for the condition to be true.



Going beyond the current form



While I've shown you a way to use your current parameter requirements in a more efficient implementation, I think there are some improvements you can make so picture_reset_pixels has more functionality. I think combining from_color, to_color, and target_color into a single dict parameter color_replacements could make this more versatile (where the key-value pairs would derive something like {(from_color, to_color): target_color}; you could even create a custom ColorRange class to encapsulate the idea of a range of colors).



There's also another issue I find with your current implementation: it can only support certain image types. I tried your algorithm on a GIF file and it failed because the pixels in a GIF file are ints, not four-tuples like PNGs. You might want to consider investigating that further if you want to support multiple image types.






share|improve this answer














Improving the general implementation



Generally we want to avoid appending to a list too many times if we can help it. This is because lists are dynamic arrays, so appends can be unnecessarily expensive (though the dynamic part helps with the efficiency). Instead, when we know the size of the input set (i.e. the number of pixels in the image), we can take advantage of that information. But in this case, that will be unnecessary because Image provides us with some handy helper methods to handle this very situation:



from PIL import Image

def picture_reset_pixels(filename, from_color, to_color, target_color):
img = Image.open(filename)
pixels = img.load()
for i in range(img.size[0]):
for j in range(img.size[1]):
if all(pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3)):
pixels[i, j] = target_color + (255,)
return img


Assigning pixels to img.load() allows us to take advantage of directly editing the image. Since copying the image into another list was unnecessary originally (to clarify, the img variable doesn't affect the file on disk that img originated from, i.e. the file at location filename), editing directly is the best way.



Another general improvement I've taken advantage of in this implementation is using range to check if each of the RGB values of a pixel are between the two pixel boundaries of the input arguments. all() serves the purpose that the and statements in the original version served: it requires that all the pixels compared in the iterator pixels[i, j][k] in range(from_color[k], to_color[k] + 1) for k in range(3) are in the required range for the condition to be true.



Going beyond the current form



While I've shown you a way to use your current parameter requirements in a more efficient implementation, I think there are some improvements you can make so picture_reset_pixels has more functionality. I think combining from_color, to_color, and target_color into a single dict parameter color_replacements could make this more versatile (where the key-value pairs would derive something like {(from_color, to_color): target_color}; you could even create a custom ColorRange class to encapsulate the idea of a range of colors).



There's also another issue I find with your current implementation: it can only support certain image types. I tried your algorithm on a GIF file and it failed because the pixels in a GIF file are ints, not four-tuples like PNGs. You might want to consider investigating that further if you want to support multiple image types.







share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









Graham

491112




491112












  • in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
    – Reinderien
    1 hour ago


















  • in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
    – Reinderien
    1 hour ago
















in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
– Reinderien
1 hour ago




in range(), if I understand what you're doing, is a very bad idea. You're essentially constructing a list of numbers and then searching to see if a pixels element is in them. Instead, do a comparison of the form from_colour <= pixel <= to_colour.
– Reinderien
1 hour ago












up vote
0
down vote













The code is overall simple and good but could be improved in a few things that would make it a bit shorter and more readable





Inside the for loop, in the if / else, you are repeating append in both of your cases. You could do instead



to_add = (round(R), round(G), round(B))
if condition:
to_add = target_color
red_list.append(to_add)




On the condition, you can also refactor using chained comparison to improve readability, preferably in ascending order. If you break that line, do so aligning conditionals as follow :



if from_color[0] <= R <= to_color[0] and 
from_color[1] <= G <= to_color[1] and
from_color[2] <= B <= to_color[2]:


Alternatively, but sightly less explicit, you can iterate over i.



if all((from_color[j] <= i[j] <= to_color[j] for j in range(3)))




R, G, B initialisation can be made a single line:



R, G, B = i


or, if working with a 4 tuples:



R, G, B, _ = i





share|improve this answer










New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
    – Reinderien
    1 hour ago















up vote
0
down vote













The code is overall simple and good but could be improved in a few things that would make it a bit shorter and more readable





Inside the for loop, in the if / else, you are repeating append in both of your cases. You could do instead



to_add = (round(R), round(G), round(B))
if condition:
to_add = target_color
red_list.append(to_add)




On the condition, you can also refactor using chained comparison to improve readability, preferably in ascending order. If you break that line, do so aligning conditionals as follow :



if from_color[0] <= R <= to_color[0] and 
from_color[1] <= G <= to_color[1] and
from_color[2] <= B <= to_color[2]:


Alternatively, but sightly less explicit, you can iterate over i.



if all((from_color[j] <= i[j] <= to_color[j] for j in range(3)))




R, G, B initialisation can be made a single line:



R, G, B = i


or, if working with a 4 tuples:



R, G, B, _ = i





share|improve this answer










New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
    – Reinderien
    1 hour ago













up vote
0
down vote










up vote
0
down vote









The code is overall simple and good but could be improved in a few things that would make it a bit shorter and more readable





Inside the for loop, in the if / else, you are repeating append in both of your cases. You could do instead



to_add = (round(R), round(G), round(B))
if condition:
to_add = target_color
red_list.append(to_add)




On the condition, you can also refactor using chained comparison to improve readability, preferably in ascending order. If you break that line, do so aligning conditionals as follow :



if from_color[0] <= R <= to_color[0] and 
from_color[1] <= G <= to_color[1] and
from_color[2] <= B <= to_color[2]:


Alternatively, but sightly less explicit, you can iterate over i.



if all((from_color[j] <= i[j] <= to_color[j] for j in range(3)))




R, G, B initialisation can be made a single line:



R, G, B = i


or, if working with a 4 tuples:



R, G, B, _ = i





share|improve this answer










New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









The code is overall simple and good but could be improved in a few things that would make it a bit shorter and more readable





Inside the for loop, in the if / else, you are repeating append in both of your cases. You could do instead



to_add = (round(R), round(G), round(B))
if condition:
to_add = target_color
red_list.append(to_add)




On the condition, you can also refactor using chained comparison to improve readability, preferably in ascending order. If you break that line, do so aligning conditionals as follow :



if from_color[0] <= R <= to_color[0] and 
from_color[1] <= G <= to_color[1] and
from_color[2] <= B <= to_color[2]:


Alternatively, but sightly less explicit, you can iterate over i.



if all((from_color[j] <= i[j] <= to_color[j] for j in range(3)))




R, G, B initialisation can be made a single line:



R, G, B = i


or, if working with a 4 tuples:



R, G, B, _ = i






share|improve this answer










New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this answer



share|improve this answer








edited 12 mins ago





















New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









answered 5 hours ago









Arthur Havlicek

2713




2713




New contributor




Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Arthur Havlicek is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
    – Reinderien
    1 hour ago


















  • Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
    – Reinderien
    1 hour ago
















Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
– Reinderien
1 hour ago




Your second-last line - if i is a 3-tuple, you can simply write R, G, B = i. It will unpack.
– Reinderien
1 hour ago










Victoria is a new contributor. Be nice, and check out our Code of Conduct.










 

draft saved


draft discarded


















Victoria is a new contributor. Be nice, and check out our Code of Conduct.













Victoria is a new contributor. Be nice, and check out our Code of Conduct.












Victoria is a new contributor. Be nice, and check out our Code of Conduct.















 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207839%2fchange-all-pixels-that-lie-in-a-given-color-range-to-a-new-color%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

Ottavio Pratesi

Tricia Helfer

15 giugno