-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_generation_dslr_edit.py
129 lines (101 loc) · 3.17 KB
/
content_generation_dslr_edit.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# This script takes
# - input directory or file; batch mode / individual mode
# - output directory
#
# If crop configs are present it will crop the input
# It will also do post-processing on the images required to get them to final
# standard.
from skimage import io, exposure
import skimage.color as color
import matplotlib.pyplot as plt
import cv2 as cv
import pycommon.image as image
import numpy as np
import time
import argparse
import yaml
import os
def process_image(raw, crop_config=None):
cropped = raw.copy()
# crop
if crop_config is not None:
x1 = crop_config['left_abs']
x2 = crop_config['right_abs']
y1 = crop_config['top_abs']
y2 = crop_config['bottom_abs']
cropped = cropped[y1:y2,x1:x2,:]
image.add_overlay(cropped)
post = cropped.copy()
print(post.shape)
# post = exposure.adjust_gamma(post, 0.5)
# equalise
# post = exposure.equalize_hist(post)
# post = exposure.equalize_adapthist(post, clip_limit=0.01)
#! Best go to the source, should the shutter time/aperture size be greater?
# saturation_factor=1.1
# hsv = color.rgb2hsv(post)
# hsv[:,:,1] = hsv[:,:,1] * saturation_factor
# post = color.hsv2rgb(hsv)
# any other post
return cropped, post
def handle_directory(in_path, out_path):
# for each image in directory:
# handle_image(path)
return
def handle_image(in_file, out_dir):
raw = load_image(in_file)
crop_config = load_crop_config(args.input)
print(crop_config)
cropped, post = process_image(raw, crop_config=crop_config)
if out_dir is None:
preview_image(raw, cropped, post)
else:
save_image(in_file, out_dir, post)
return
def preview_image(raw, cropped, post):
# print(raw[0,0,0])
# print(post[0,0,0])
after = cv.hconcat([cropped, post])
print(raw.shape)
print(post.shape)
print(after.shape)
if raw.shape[1] > after.shape[1]:
after = np.pad(after, ((0,0),(0,raw.shape[1]-after.shape[1]), (0,0)))
else:
raw = np.pad(raw, ((0,0),(0,after.shape[1]-raw.shape[1]), (0,0)))
res = cv.vconcat([raw, after])
cv.namedWindow("win", cv.WINDOW_NORMAL)
cv.imshow("win", res)
while cv.waitKey() != ord('q'):
time.sleep(0.01)
cv.destroyAllWindows()
def load_image(path):
print(f"load {path}")
img = cv.imread(path, cv.IMREAD_UNCHANGED)
img = cv.rotate(img, cv.ROTATE_180)
return img / 255.0
def save_image(in_file, out_dir, image):
name = os.path.basename(in_file)
write_path = os.path.join(out_dir, name)
print("saving...", cv.imwrite(write_path, image*255))
def load_crop_config(path):
d = os.path.dirname(path)
config = None
try:
with open(os.path.join(d, "crop_dslr.yml"), 'r') as f:
config = yaml.load(f, Loader=yaml.FullLoader)
except FileNotFoundError as err:
print(err)
pass
return config
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", action="store", help="input file or directory to process")
parser.add_argument("-o", "--output", action="store", help="output directory for processed images")
parser.add_argument("-v", "--verbose", action="store_true", help="verbose output, print debug information")
args = parser.parse_args()
if False: #is_directory(args.input):
handle_directory(args.input)
else:
handle_image(args.input, args.output)
print("done")