-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_conversion.py
36 lines (26 loc) · 1.17 KB
/
image_conversion.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
# Conversion of images taken by S & P robot
def get_modified_image(image, outp, pic_fname, xoff=500, yoff=300):
from pathlib import Path
import os, os.path
from skimage import io,util
# xoff/yoff = number of pixels that will be cropped from the picture
# Output folder is created only if it doesn't already exist
Path(outp).mkdir(parents=True, exist_ok=True)
# Picture is cropped
## I've added +80 because the plate is not perfectly centered, which means there's more to crop on the left than on the right
y1,y2,x1,x2=yoff,image.shape[0]-yoff,xoff,image.shape[1]-xoff+80
cropped = image[y1:y2,x1:x2]
# Picture is converted into levels of gray
from skimage.color import rgb2gray
grayscale = rgb2gray(cropped)
# Levels of gray are inverted: gray on white background equals growth
grayscale=util.invert(grayscale)
out=util.img_as_ubyte(grayscale)
# Converted picture is saved
io.imsave(outp+pic_fname, out)
return out, outp+pic_fname
# Batch conversion
for f in os.listdir(p):
fname = os.path.basename(f)
image = io.imread(p+fname)
get_modified_image(image, outp, pic_fname = fname)