Loading

Paste #pevbhmgp1

  1. Changes + some indication of timing for a non-representative image
  2.  
  3. * Base version 2:52    (1024x1024, with 80% or so lot of transparent pixels)
  4. * Print only the row number 2:31
  5. * Do not perform superfluous checks 2:32    (also drops math.sqrt)
  6. * Factor subtraction out 1:41
  7. * Iterate over the palette directly 1:17
  8.  
  9. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
  10.  
  11. diff --git a/convertor_v4.py b/convertor_v4.py
  12. --- a/convertor_v4.py
  13. +++ b/convertor_v4.py
  14. @@ -48,11 +48,12 @@ def rgb2palette(inputImage, frameNumber)
  15.  
  16.  
  17.    for y in range (0, i.height):
  18. +    print("row {}".format(y))
  19.      for x in range (0, i.width):
  20.        winnerDistance = 100000000
  21.        winnerID = 0
  22.  
  23. -      pixelNumber = x + (y * i.width)
  24. +      #pixelNumber = x + (y * i.width)
  25.        pix = i.getpixel((x,y))
  26.        pixRed = pix[0]
  27.        pixGreen = pix[1]
  28. @@ -63,49 +64,45 @@ def rgb2palette(inputImage, frameNumber)
  29.        if pixAlpha < 128:
  30.          finalAlpha = 0
  31.          colorOffset = 0
  32. -      if pixAlpha >= 128 and pixAlpha < 178:
  33. +      elif pixAlpha < 178:
  34.          finalAlpha = 255
  35.          colorOffset = 1
  36. -      if pixAlpha >= 178 and pixAlpha < 230:
  37. +      elif pixAlpha < 230:
  38.          finalAlpha = 255
  39.          colorOffset = 2
  40. -      if pixAlpha >= 230:
  41. +      else:
  42.          finalAlpha = 255
  43.          colorOffset = 0
  44.  
  45.        if pixAlpha >= 128:
  46. -        for z in range(0, 255):
  47. -          colorArray = p[z]
  48. +        for z, (cr, cg, cb) in enumerate(p):
  49.            #print("current z is ..." + str(colorArray) )
  50. -          paletteR = colorArray[0]
  51. -          paletteG = colorArray[1]
  52. -          paletteB = colorArray[2]
  53. -          distance = math.sqrt( ((pixRed-paletteR)*(pixRed-paletteR)) + ((pixGreen-paletteG)*(pixGreen-paletteG)) + ((pixBlue-paletteB)*(pixBlue-paletteB) ))
  54. +          dr = pixRed - cr
  55. +          dg = pixGreen - cg
  56. +          db = pixBlue - cb
  57. +          distance = dr*dr + dg*dg + db*db
  58.            #print("Pixel " + str(pixelNumber) + ": " + str(distance) + " ,Alpha: " + str(pixAlpha) )
  59.  
  60.            #taking the distance and comparing it to previous "round"
  61. -          temporaryDistance = distance
  62. -          temporaryID = z
  63. -          #print("temporaryID is ..." + str(temporaryID) )
  64. -          if temporaryDistance < winnerDistance:
  65. -            winnerDistance = temporaryDistance
  66. -            winnerID = temporaryID
  67. +          if distance < winnerDistance:
  68. +            winnerDistance = distance
  69. +            winnerID = z
  70.              #print("new winner is ..." + str(winnerID) + "!!!")
  71.  
  72.        #colorOffset changes based on Value of the input
  73.  
  74.        if pixRed >= pixGreen and pixRed >= pixBlue:
  75.          highestValue = pixRed
  76. -      if pixGreen >= pixRed and pixGreen >= pixBlue:
  77. +      elif pixGreen >= pixRed and pixGreen >= pixBlue:
  78.          highestValue = pixGreen
  79. -      if pixBlue >= pixRed and pixBlue >= pixGreen:
  80. +      else:
  81.          highestValue = pixBlue
  82.  
  83.        if highestValue < 128:
  84.          negation = -1
  85.          colorOffset = colorOffset * negation
  86.  
  87. -      print("colorOffset is ... " + str(colorOffset) )
  88. +      #print("colorOffset is ... " + str(colorOffset) )
  89.        finalColor = p[winnerID - colorOffset]
  90.  
  91.        finalR = finalColor[0]
  92. @@ -114,7 +111,7 @@ def rgb2palette(inputImage, frameNumber)
  93.        #finalAlpha taken from the if output above
  94.  
  95.        imageOutput.putpixel((x,y),(finalR,finalG,finalB,finalAlpha))
  96. -      print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= "  + str(finalAlpha)   )
  97. +      #print("Pixel " + str(pixelNumber) + ": R= " + str(finalR) + ", G= " + str(finalG) + ", B= " + str(finalB) + ", A= "  + str(finalAlpha)   )

Comments