-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgb2grayscale.py
41 lines (29 loc) · 1.11 KB
/
rgb2grayscale.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
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 21 14:57:30 2020
@author: katrin
"""
import glob, os
from PIL import Image
from numpy import asarray
def grayscale():
# create new folder
os.makedirs("./new_foldername_grayscale")
for infile in glob.glob("./filename/*.jpg"):
# grayscale
img_file = Image.open(infile).convert('L') #grayscale
# convert image to array
data = asarray(img_file)
print("Datatype: " + str(type(data)))
print("Shape: " + str(data.shape)) # Channel
# convert array to image
image = Image.fromarray(data)
print("Mode: " + str(image.mode))
print("Size: " + str(image.size)) # Bildgröße
# split the file, to get the name of the image
file = infile.split('/')[-1]
name = file.split('.')[-2] # Name des Bildes ohne file und .jpg
print(str(name))
#safe grayscale image in new folder
img_file.save("./new_foldername_grayscale/" + str(name) + "_grayscale" + ".jpg" , "JPEG")
grayscale()