File:Pixel sample size.gif

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search

Pixel_sample_size.gif(256 × 256 pixels, file size: 90 KB, MIME type: image/gif, looped, 29 frames, 5.8 s)

Captions

Captions

animation showing antialiasing with different sized sampler

Summary

[edit]
Description
English: This is an animation I have created showing how shape antialiasing in images is affected by the size of the sampler compared to the pixel size (shown by the bottom right rectangle). When the size is 0 (single sample), the image becomes unantialiased and black-and-white only. As the sampler size increases, antialiasing can be seen to take effect. The ideal size is the exact size of one pixel. When the size is greater than one pixel, the image becomes overly blurred.
Date
Source Own work
Author Drummyfish
Source code
InfoField
# by drummyfish, released under CC0 1.0

from PIL import Image
import math

SIZE = 64
SCALE = 4

IMG_SIZE = SIZE * SCALE

SAMPLES = 10

SAMPLES_TOTAL = SAMPLES * SAMPLES

STEP = 0.1
STEPS = 15

FRAMES = STEPS * 2 - 1

RECT_SIZE = 30
RECT_OFFSET = 20
RECT_POS = IMG_SIZE - RECT_SIZE - RECT_OFFSET
RECT_COLOR = (255,0,0)
SAMPLE_COLOR = (0,255,0)

def isInSet(x,y):
  dx = x - 28
  dy = y - 28
  dist = math.sqrt(dx * dx + dy * dy)

  dist2 = abs(y - (x * 3 - 40))

  return (dist > 23.5 or dist < 20.5) and dist2 > 2

image = Image.new("RGB",(IMG_SIZE,IMG_SIZE),"white")

pixels = image.load()

width = 0.0

for frame in range(FRAMES):
  print("frame " + str(frame))

  step = width / SAMPLES

  for y in range(SIZE):
    for x in range(SIZE):
      hits = 0

      for sy in range(SAMPLES):
        for sx in range(SAMPLES):
          if isInSet(x + sx * step, y + sy * step):
            hits += 1

      color = int(255 * hits / float(SAMPLES_TOTAL))

      c = (color,color,color)

      for i in range(SCALE):
        for j in range(SCALE):
          pixels[(x * SCALE + i, y * SCALE + j)] = c

  sampleWidth = int(width * RECT_SIZE)
  sampleStart = RECT_POS + (RECT_SIZE - sampleWidth) / 2

  for y in range(sampleWidth):
    for x in range(sampleWidth):
      pixels[(sampleStart + x,sampleStart + y)] = SAMPLE_COLOR

  for i in range(RECT_SIZE):
    pixels[(RECT_POS + i,RECT_POS)] = RECT_COLOR
    pixels[(RECT_POS + i,RECT_POS + RECT_SIZE - 1)] = RECT_COLOR
    pixels[(RECT_POS,RECT_POS + i)] = RECT_COLOR
    pixels[(RECT_POS + RECT_SIZE - 1,RECT_POS + i)] = RECT_COLOR

  image.save("sampling" + str(frame + 1).rjust(2,"0") + ".png")

  if frame < STEPS:
    width += STEP
  else:
    width -= STEP

Licensing

[edit]
I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current21:42, 16 May 2020Thumbnail for version as of 21:42, 16 May 2020256 × 256 (90 KB)Drummyfish (talk | contribs)fix a few pixels
21:30, 16 May 2020Thumbnail for version as of 21:30, 16 May 2020256 × 256 (90 KB)Drummyfish (talk | contribs)Uploaded own work with UploadWizard

The following page uses this file:

Metadata