Skip to content

Instantly share code, notes, and snippets.

@nitori
Created February 4, 2023 23:48
Show Gist options
  • Select an option

  • Save nitori/f1bb61a42b0dd8e8d5ce43437c555a5e to your computer and use it in GitHub Desktop.

Select an option

Save nitori/f1bb61a42b0dd8e8d5ce43437c555a5e to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw
import math
def main():
im = Image.open('city.png').convert('RGB')
out = Image.new('RGB', im.size)
dotw = 16
doth = 16
dot_radius = min(dotw, doth) // 2
dots_horiz = math.ceil(im.width / dotw)
dots_vert = math.ceil(im.height / doth)
im.thumbnail((dots_horiz, dots_vert), Image.Resampling.NEAREST)
im = im.quantize(128).convert('RGB')
draw = ImageDraw.Draw(out)
for y in range(im.height):
for x in range(im.width):
pixel = im.getpixel((x, y))
cx = x * dotw + dotw // 2
cy = y * doth + doth // 2
cr = dot_radius * 0.8
draw.ellipse((cx - cr, cy - cr, cx + cr, cy + cr), fill=pixel)
# out.show()
out.save('city-out.png')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment