Repository navigation
Expand file tree
/
Copy pathtransformImage.py
More file actions
72 lines (55 loc) · 1.67 KB
/
Copy pathtransformImage.py
File metadata and controls
72 lines (55 loc) · 1.67 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
'''This Example opens an Image and transform the image into grayscale, halftone, dithering, and primary colors.
You need PILLOW (Python Imaging Library fork) and Python 3.5
-Isai B. Cicourel'''
from PIL import Image
# Open an Image
def open_image(path):
newImage = Image.open(path)
return newImage
# Save Image
def save_image(image, path):
image.save(path, 'png')
# Create a new image with the given size
def create_image(i, j):
image = Image.new("RGB", (i, j), "white")
return image
# Get the pixel from the given image
def get_pixel(image, i, j):
# Inside image bounds?
width, height = image.size
if i > width or j > height:
return None
# Get Pixel
pixel = image.getpixel((i, j))
return pixel
# Create a Grayscale version of the image
def transform_image(image):
# Get size
width, height = image.size
# Create new Image and a Pixel Map
new = create_image(width, height)
pixels = new.load()
# Transform to grayscale
for i in range(width):
for j in range(height):
# Get Pixel
pixel = get_pixel(image, i, j)
# Get R, G, B values (This are int from 0 to 255)
red = pixel[0]
green = pixel[1]
blue = pixel[2]
# Transform to grayscale
gray = ???
# Set Pixel in new image
pixels[i, j] = (???, ????, ????)
# Return new image
return new
# Main
if __name__ == "__main__":
# Load Image (JPEG/JPG needs libjpeg to load)
input_path = raw_input("Please type the path of your image: ")
original = open_image(input_path)
# Convert and save
new = transform_image(original)
output_path = raw_input("Please type the output path of the grayscale image: ")
save_image(new, output_path)