-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcrop_image.py
More file actions
executable file
·41 lines (35 loc) · 1.56 KB
/
crop_image.py
File metadata and controls
executable file
·41 lines (35 loc) · 1.56 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
import numpy as np
import imageio
def crop_image(inimage,outimage,margin=0):
"""
% (C) Nick Holschuh - Penn State University - 2015 (Nick.Holschuh@gmail.com)
% This function takes an image file and removes solid white margins.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The inputs are as follows:
%
% inimage -- the filename for the image to crop
% outimage -- the filename to write the cropped image to
% margin=0 -- the number of additional rows/columns to add as buffer.
% higher numbers here increases the addition of white margin
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The outputs are as follows:
%
% [Nothing is returned by this function]
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
"""
frame_aggregate = imageio.imread(inimage)
######### Find the rows that are solid white
row_margin = np.where(np.mean(frame_aggregate,0) == 255)[0]
######### Find where the indeces of those rows jumps (as in, where the image is)
middle = np.where(np.diff(row_margin) > 1)[0]
x = row_margin[middle[0]]-margin
w = row_margin[middle[-1]+1]-row_margin[middle[0]]+2*margin
row_margin = np.where(np.mean(frame_aggregate,1) == 255)[0]
middle = np.where(np.diff(row_margin) > 1)[0]
y = row_margin[middle[0]]-margin
h = row_margin[middle[-1]+1]-row_margin[middle[0]]+2*margin
crop_frame = frame_aggregate[y:y+h, x:x+w]
imageio.imwrite(outimage,crop_frame)