Skip to content

Instantly share code, notes, and snippets.

@alexras
Last active January 11, 2025 20:44
Show Gist options
  • Select an option

  • Save alexras/4720743 to your computer and use it in GitHub Desktop.

Select an option

Save alexras/4720743 to your computer and use it in GitHub Desktop.
Nearest-neighbor image scaling with PIL
#!/usr/bin/env python
from PIL import Image
import sys
im = Image.open(sys.argv[1])
def scale_to_width(dimensions, width):
height = (width * dimensions[1]) / dimensions[0]
return (width, height)
im.resize(scale_to_width(im.size, int(sys.argv[2])), Image.NEAREST).save("scaled.png")
@bartlomiejduda
Copy link
Copy Markdown

In pillow 10.4.0 it's called Resampling.NEAREST, not Image.NEAREST.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment