Last active
January 25, 2021 03:05
-
-
Save jaames/02130b89fcd2a08f3d4aa87b18e32fe3 to your computer and use it in GitHub Desktop.
python class for processing miiverse drawing images
This file contains 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 | |
SAMPLING_METHODS = { | |
"ANTIALIAS": Image.ANTIALIAS, | |
"BILINEAR": Image.BILINEAR, | |
"BICUBIC": Image.BICUBIC, | |
"NEAREST": Image.NEAREST | |
} | |
class drawingImage: | |
def __init__(self, buffer): | |
if buffer: self.open(buffer) | |
# Check that the image is a drawing by counting the number of colors used | |
# | |
def isDrawing(self): | |
return len(self.image.getcolors()) == 2 | |
# Crop Image | |
# | |
# Args: | |
# bounds - number of pixels to crop from each side, as a 4-tuple: (left, top, right, down) | |
# | |
def crop(self, bounds): | |
w, h = self.image.size | |
left, upper, right, lower = bounds | |
self.image = self.image.crop((left, upper, w-right, h-lower)) | |
# Resize image | |
# | |
# Args: | |
# size - requested size in pixels, as a 2-tuple: (width, height) | |
# | |
# Optional args: | |
# sampling - resize sampling method: "BICUBIC" (default), "BILINEAR", "ANTIALIAS" or "NEAREST" | |
# | |
def resize(self, size, sampling="BICUBIC"): | |
self.image = self.image.resize(size, SAMPLING_METHODS[sampling]) | |
# Generate a thumbnail for the image (pretty much the same as resize, but preserves aspect ratio) | |
# | |
# Args: | |
# size - requested size in pixels, as a 2-tuple: (width, height) | |
# | |
# Optional args: | |
# sampling - resize sampling method: "BICUBIC" (default), "BILINEAR", "ANTIALIAS" or "NEAREST" | |
# | |
def thumbnail(self, size, sampling="BICUBIC"): | |
self.image = self.image.thumbnail(size, SAMPLING_METHODS[sampling]) | |
# Open an image | |
# | |
# Args: | |
# buffer - either a filepath or a file-like object | |
# | |
def open(self, buffer): | |
self.image = Image.open(buffer) | |
# Write the image to outputBuffer | |
# | |
# Args: | |
# buffer - either a filepath or a file-like object | |
# format - "bmp", "gif", "png" or "jpeg" | |
# | |
# Optional args: | |
# bitdepth - (BMP only) use bitdepth=8 (default) for palette mode, or bitdepth=1 for monochrome | |
# optimize - (GIF, PNG and JPEG) if optimize=True, do an extra encoding pass using optimal settings | |
# quality - (JPEG only) set jpeg image quality; value should be between 1 and 95, and defaults to 75 | |
# | |
def write(self, buffer, format, **kwargs): | |
bitdepth = kwargs.get("bitdepth", 8) | |
optimize = kwargs.get("optimize", False) | |
quality = kwargs.get("quality", 75) | |
if format == "bmp": | |
image = self.image.convert("1" if bitdepth == 1 else "P") | |
image.save(buffer, format=format) | |
elif format == "jpeg": | |
image = self.image.convert("RGB") | |
image.save(buffer, format=format, optimize=optimize, quality=quality) | |
elif format in ["gif", "png"]: | |
self.image.save(buffer, format=format, optimize=optimize) | |
if __name__ == "__main__": | |
from sys import argv | |
if (len(argv)) < 2: | |
print("drawingImage.py") | |
print("") | |
print("Command Line Args:") | |
print("") | |
print(" Input - Open an image") | |
print(" <-i | --input> <path>") | |
print("") | |
print(" Resize - (optional) use after opening to resize the image") | |
print(" <-r | --resize> <new width> <new height>") | |
print("") | |
print(" Thumbnail - (optional) use after opening to create a thumbnail for the image") | |
print(" <-t | --thumbnail> <new width> <new height>") | |
print("") | |
print(" Crop - (optional) use after opening to crop the image, pass in the amount of pixels to crop from each side") | |
print(" <-c | --crop> <left> <upper> <right> <bottom>") | |
print("") | |
print(" Output - output the resulting image to file, where type = npf to output an npf, or png / gif / jpeg etc. to output a regular image") | |
print(" <-o | --output> <type> <path>") | |
else: | |
i = 0 | |
while i < len(argv): | |
arg = argv[i] | |
# open image | |
if arg in ["-i", "--input"]: | |
img = drawingImage(argv[i+1]) | |
i += 2 | |
# resize image | |
elif arg in ["-r", "--resize"]: | |
img.resize((int(argv[i+1]), int(argv[i+2]))) | |
i += 3 | |
# generate image thumbnail | |
elif arg in ["-t", "--thumbnail"]: | |
img.thumbnail((int(argv[i+1]), int(argv[i+2]))) | |
i += 3 | |
# crop image | |
elif arg in ["-c", "--crop"]: | |
img.crop((int(argv[i+1]), int(argv[i+2]), int(argv[i+3]), int(argv[i+4]))) | |
i += 5 | |
# output file | |
elif arg in ["-o", "--output"]: | |
with open(argv[i+2], "wb") as outFile: | |
img.write(outFile, argv[i+1]) | |
i += 3 | |
else: | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment