This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimgutils.py
58 lines (45 loc) · 1.8 KB
/
imgutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from math import floor as fl
from random import randint as rint
from tkinter import Image
from PIL import Image
def grayscalify(path: str):
"""Make an image grayscale.
Args:
path (str): Path of the file to grayscale.
"""
Image.open(path).convert('LA').save(path)
def tint(path: str, RGB: tuple):
"""Tint an image a color with RGB values. Floors RGB values to integers.
Args:
path (str): Path of the file to tint.
RGB (tuple): RGB values to tint the image with.
"""
image = Image.open(path).convert('RGBA')
output = []
for pix in image.getdata():
# If the alpha value is 0, then the pixel is transparent, so we don't want to change it.
if pix[3] in list(range(1, 256)):
output.append((pix[0] + fl(RGB[0]), pix[1] +
fl(RGB[1]), pix[2] + fl(RGB[2])))
else:
output.append(pix)
image.putdata(output)
image.save(path)
def spin(path: str, chance: int = 3):
"""Randomly rotate an image with a chance to do nothing.
Args:
path (str): Path of the file to randomly rotate.
chance (int, optional): Chance to do nothing, for example, a chance of 1 would cause this function to do nothing 50% of the time. Defaults to 3.
"""
if rint(0, chance) == 0 and chance != 0:
return
Image.open(path).transpose(rint(2, 4)).save(path)
def flip(path: str, chance: int = 3):
"""Randomly flip an image with a chance to do nothing.
Args:
path (str): Path of the file to randomly flip.
chance (int, optional): Chance to do nothing, for example, a chance of 1 would cause this function to do nothing 50% of the time. Defaults to 3.
"""
if rint(0, chance) == 0 and chance != 0:
return
Image.open(path).transpose(0).save(path)