Reputation: 461
I'm new to coding and I'm really confused. From the code below, I'm trying to print out every single colour in the RGB spectrum from (0,0,0) to (255,255,255) so technically there should be 256^3 images printed with all the colours. I've kind of done it below which only does it for the red or blue or green spectrum and not the combinations. What I'm trying to do with all the colours that are printed out is to stitch them all together pixel by pixel in this format. I don't want to print out all the images but get the combined version at the end. Any help would be greatly appreciated! Thank you so much for the help!
I would like to get all the combinations of the 3 variables from 0 to 255. For example, the output from print axis
would be:
(0,0,0)
(1,0,0)
(2,0,0)
...
(255,0,0)
(0,1,0)
(0,2,0)
...
(0,255,0)
(0,0,1)
(0,0,2)
...
(0,0,255)
(1,0,1)
...
so basically all the different combinations of the 3 numbers until 255.
I tried using the one below but what I'm trying to do is a bit different where each pixel I want is different and stitched together in a pixel by pixel format by looping through the values.
from PIL import Image
import sys
im = Image.new("RGB", (1, 1))
pix = im.load()
red=0
green=0
blue=0
j=0
while red<255 and green<255 and blue<255:
j+=1
red +=1
#green +=1
#blue+=1
for x in range(1):
for y in range(1):
axis = (red,green,blue)
pix[x,y] = axis
print axis
im.save('Test {}.png'.format(j), "PNG")
#stitching images together
images = map(Image.open, ['Test 1.png', 'Test 255.png', 'Test 100.png'])
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
new_im = Image.new('RGB', (total_width, max_height))
x_offset = 0
for im in images:
new_im.paste(im, (x_offset,0))
x_offset += im.size[0]
new_im.save('test.jpg')
Upvotes: 2
Views: 3241
Reputation: 59416
You need three nested for-loops to accomplish this:
for r in range(256):
for g in range(256):
for b in range(256):
do_your_thing_with(r, g, b)
What you want to do still was a bit unclear, but I want to warn you: You seem to want to nest loops for x and y within these loop again. You will very quickly do something which would take ages to complete. After all, 256×256×256 already is ~16.7 million. If you now go through every pixel in an image of 1000×1000 pixels, then you'll have 16.7 trillion iterations.
Consider what you want to do and maybe reduce your question and example code to a necessary minimum to understand where your problem lies.
Concerning your comment:
I'd propose this to create an image with lots of colors:
def each_color():
for r in range(256):
for g in range(256):
for b in range(256):
yield r, g, b
color_provider = each_color()
for x in range(1000):
for y in range(1000):
set_pixel(x, y, color=next(color_provider))
This uses a generator to create lots of different colors (each_color()
). Using next()
on it returns one color after the other. Then you can use it in a nested loop to color each pixel.
Notice that the generate would yield 16.7 million values but you will ask for only 1000000 pixels using this code (which is fine, generators need not be exhausted), so the majority of colors will not be used in this image.
Upvotes: 3