0

I'm writing a python program that read the colour of each pixel of an image, and then make a file with an ASCII character with a similar brightness of that pixel (using a dot of the originale pixel is dark, and a "#" if is bright). I realised that ASCII characters are much larger than single pixels: the resulting ASCII art would be too large to display on a normal monitor.

Now I want to try to apply this process only count one in every 10 pixels. How should I do this? How can set the step of my for cycle !=1 (default value)?

Calaf
  • 1,133
  • 2
  • 9
  • 22
Gurnoor
  • 133
  • 2
  • 7
  • Try using PIL - https://stackoverflow.com/a/138260/13213725 – astroanax Jul 27 '20 at 04:23
  • 1
    How are the pixels expressed? If you can get them by index and you know the full length, you can use `range(0, some_len, 10)` to index by 10s and perhaps average pixels 0-9 for each interval. – tdelaney Jul 27 '20 at 04:29

3 Answers3

2

Actually, range() takes 3 positional arguments, and 2 of them are set to default as 0 and 1. The arguments are: start = from which number to start, stop = in which number to stop and step = by which number to increase the count each time. By default, start is equl to 0, and step is equal to 1. And when you write range(number), you assign stop to your number. So, if i want each 10 numbers, i just write:

range(0, 100, 10)
Michael
  • 657
  • 4
  • 29
1

You can use the three-argument form of range for that:

for i in range(0, width, 10):
    for j in range(0, height, 10):
        ...

Although just sampling 1% of the pixels is very likely to give pretty bad results. You can improve on it by making each output character depend on the average of each 10x10 pixel block, for example.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
0

The other solution is obviously

for mul in range(width//10):
   do_thing(mul*10)
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • why divide by 10 if anyway while using we multiply it again by 10? – Michael Jul 27 '20 at 05:13
  • 1
    A common reason to want to do this is e.g. to split an integer into fractions; you can't do `range(0,1,0.1)` but you can do `range(10)` and divide by 10. – tripleee Jul 27 '20 at 05:17