- Changes + some indication of timing for a non-representative image
- * Base version 2:52 (1024x1024, with 80% or so lot of transparent pixels)
- * Print only the row number 2:31
- * Do not perform superfluous checks 2:32 (also drops math.sqrt)
- * Factor subtraction out 1:41
- * Iterate over the palette directly 1:17
- @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
- diff --git a/convertor_v4.py b/convertor_v4.py
- --- a/convertor_v4.py
- +++ b/convertor_v4.py
- @@ -48,11 +48,12 @@ def rgb2palette(inputImage, frameNumber)
- for y in range (0, i.height):
- + print("row {}".format(y))
- for x in range (0, i.width):
- winnerDistance = 100000000
- winnerID = 0
- - pixelNumber = x + (y * i.width)
- + #pixelNumber = x + (y * i.width)
- pix = i.getpixel((x,y))
- pixRed = pix[0]
- pixGreen = pix[1]
- @@ -63,49 +64,45 @@ def rgb2palette(inputImage, frameNumber)
- if pixAlpha < 128:
- finalAlpha = 0
- colorOffset = 0
- - if pixAlpha >= 128 and pixAlpha < 178:
- + elif pixAlpha < 178:
- finalAlpha = 255
- colorOffset = 1
- - if pixAlpha >= 178 and pixAlpha < 230:
- + elif pixAlpha < 230:
- finalAlpha = 255
- colorOffset = 2
- - if pixAlpha >= 230:
- + else:
- finalAlpha = 255
- colorOffset = 0
- if pixAlpha >= 128:
- - for z in range(0, 255):
- - colorArray = p[z]
- + for z, (cr, cg, cb) in enumerate(p):
- #print("current z is ..." + str(colorArray) )
- - paletteR = colorArray[0]
- - paletteG = colorArray[1]
- - paletteB = colorArray[2]
- - distance = math.sqrt( ((pixRed-paletteR)*(pixRed-paletteR)) + ((pixGreen-paletteG)*(pixGreen-paletteG)) + ((pixBlue-paletteB)*(pixBlue-paletteB) ))
- + dr = pixRed - cr
- + dg = pixGreen - cg
- + db = pixBlue - cb
- + distance = dr*dr + dg*dg + db*db
- #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) )
- #taking the distance and comparing it to previous "round"
- - temporaryDistance = distance
- - temporaryID = z
- - #print("temporaryID is ..." + str(temporaryID) )
- - if temporaryDistance < winnerDistance:
- - winnerDistance = temporaryDistance
- - winnerID = temporaryID
- + if distance < winnerDistance:
- + winnerDistance = distance
- + winnerID = z
- #print("new winner is ..." + str(winnerID) + "!!!")
- #colorOffset changes based on Value of the input
- if pixRed >= pixGreen and pixRed >= pixBlue:
- highestValue = pixRed
- - if pixGreen >= pixRed and pixGreen >= pixBlue:
- + elif pixGreen >= pixRed and pixGreen >= pixBlue:
- highestValue = pixGreen
- - if pixBlue >= pixRed and pixBlue >= pixGreen:
- + else:
- highestValue = pixBlue
- if highestValue < 128:
- negation = -1
- colorOffset = colorOffset * negation
- - print("colorOffset is ... " + str(colorOffset) )
- + #print("colorOffset is ... " + str(colorOffset) )
- finalColor = p[winnerID - colorOffset]
- finalR = finalColor[0]
- @@ -114,7 +111,7 @@ def rgb2palette(inputImage, frameNumber)
- #finalAlpha taken from the if output above
- imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha))
- - print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= " + str(finalAlpha) )
- + #print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= " + str(finalAlpha) )