Last active
January 11, 2025 20:44
-
-
Save alexras/4720743 to your computer and use it in GitHub Desktop.
Nearest-neighbor image scaling with PIL
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
| #!/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") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In pillow
10.4.0it's calledResampling.NEAREST, notImage.NEAREST.