Python - image rows rearrangement and displaying afterwards

Viewed 47

I am trying to write a code in Python to display an altered image according to some conditionals. It is a part of LED strip displaying, but I am not experienced with image processing. Maybe someone would help me.

The image with fixed dimensions [height x weight] is loaded and code has to change the rows rearrangement in two ways:

For the first half of rows, For example for max_height = 10: [1,2,3,4,5] --> [1,3,5,7,9]
i=0
for height_pixel =< max_height/2
height_pixel=height_pixel+1
i+=1
For the second half of rows, for example for max_height = 10: [6,7,8,9,10] --> [10,8,6,4,2]
i=4
for height_pixel > max_height/2
height_pixel=(max_height/2)+1+i
i-=2

The columns are not changed. After that printing/showing/... new image.

The code I have so far, based on adafruit library:

from PIL import Image
import adafruit_dotstar as dotstar

NUMPIXELS = 10  #Length of strip
FILENAME = "image.png"  # Image file to load


# Load image and get dimensions:
IMG = Image.open(FILENAME).convert("RGB")
PIXELS = IMG.load()
WIDTH = IMG.size[0]
HEIGHT = IMG.size[1]

HEIGHT = min(HEIGHT, NUMPIXELS)

######################################################
# CONDITIONAL PART
pixelMap = IMG.load()
img = Image.new( IMG.mode, IMG.size)
pixelsNew = img.load()
for i in range(img.size[0]):
    if ...
    else...

#######################################################

img.show() #altered image


#here I allocating list of lists one for each column of image
COLUMN = [0 for x in range(WIDTH)]
for x in range(WIDTH):
    COLUMN[x] = [[0, 0, 0, 0] for _ in range(HEIGHT)]

#converts it into 2D list [column x row]
for x in range(WIDTH):  # For each column of image
    for y in range(HEIGHT):  # For each pixel in column
        value = PIXELS[x, y]  # Read RGB pixel in image
        COLUMN[x][y][0] = value[0]]  # R
        COLUMN[x][y][1] = value[1]]  # G
        COLUMN[x][y][2] = value[2]]  # B
        COLUMN[x][y][3] = 1.0  # Brightness

#and display in a loop
#here the columns will changed after pressing the button, I will add it later, for now it is in loop
while True:  
    for x in range(WIDTH):  # For each column of image...
        DOTS[0 : DOTS.n] = COLUMN[x]  
        DOTS.show() 

Maybe someone would give hints or help me with the code.

Best regards

1 Answers

I would suggest to generate a mapping array, which maps the orignal column indices to the newly ordered ones (and by the way, indices should start at 0):

first_half = [2*idx + 1 for idx in range(5)] # 1,3,5,7,9
second_half = [8-2*idx for idx in range(5)]  # 8,6,4,2,0
mapping = first_half + second_half

for idx in range(HEIGHT):
     new_img[idx] = IMG[mapping[idx]] # introduced new_img to avoid confusion between img and IMG

And then you should be almost done. As far as I can see you do not need to generate this COLUMN list of lists.

for column in new_img:
   DOTS[0 : DOTS.n] = column # if thats the DOTS notation, I am not familiar with that part of your code
   DOTS.show()
Related