Last active
April 30, 2022 19:37
-
-
Save Wunkolo/1087c4e5f8b3dbf1338bc52ec4acdfe3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from PIL import Image | |
from PIL import ImageDraw | |
def qHilbertSOA(Width, Distances): | |
Level = 1 | |
PositionsX = [0] * len(Distances) | |
PositionsY = [0] * len(Distances) | |
CurDistances = Distances | |
for i in range(Width.bit_length() - 1): | |
# Determine Regions | |
RegionsX = [0b1 & (Dist // 2) for Dist in CurDistances] | |
RegionsY = [0b1 & (Dist ^ RegionsX[i]) for i,Dist in enumerate(CurDistances)] | |
# Flip if RegX == 0 RegY==1 | |
PositionsX = [ | |
(Level-1 - PosX) if RegionsX[i] == 1 and RegionsY[i] == 0 else PosX \ | |
for i,PosX in enumerate(PositionsX) | |
] | |
PositionsY = [ | |
(Level-1 - PosY) if RegionsX[i] == 1 and RegionsY[i] == 0 else PosY \ | |
for i,PosY in enumerate(PositionsY) | |
] | |
# Swap X and Y if RegX == 0 | |
PositionsX,PositionsY = zip( | |
*[ reversed(ele) if RegionsY[i] == 0 else ele for i,ele in enumerate(zip(PositionsX,PositionsY))]\ | |
) | |
# Integrate | |
PositionsX = [ PosX + Level * RegionsX[i] for i,PosX in enumerate(PositionsX)] | |
PositionsY = [ PosY + Level * RegionsY[i] for i,PosY in enumerate(PositionsY)] | |
CurDistances = [ Dist // 4 for Dist in CurDistances ] | |
Level *= 2 | |
return list(zip(PositionsX,PositionsY)) | |
def GenHilbertFrames(Width, Scale): | |
img = Image.new('RGB',(Width*Scale,Width*Scale)) | |
Points = qHilbertSOA(Width,list(range(0,Width**2))) | |
draw = ImageDraw.Draw(img) | |
PointCount = Width ** 2 | |
for i,Line in enumerate(zip(Points,Points[1:])): | |
Scaled = ( (Line[0][0] * Scale,Line[0][1] * Scale), (Line[1][0] * Scale,Line[1][1]* Scale)) | |
draw.line( | |
Scaled, | |
fill=0xFFFFFF | |
) | |
print( | |
"%6d/%6d %02.02f%%\r" % (i,PointCount,100*(i/PointCount)), | |
end='' | |
) | |
# save every frame | |
# scale it x2 | |
img.resize( | |
(img.width * 2,img.height * 2), | |
Image.NEAREST | |
).save("Hilbert" + str(i).zfill(6) + ".png") | |
del draw | |
GenHilbertFrames(64,3) | |
#ffmpeg -f image2 -framerate 50 -i Hilbert%6d.png Hilbert.gif -t 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment