Created
December 12, 2020 02:26
-
-
Save NorimasaNabeta/8a2c8b091d59b57a1947173d55a35063 to your computer and use it in GitHub Desktop.
image output with the framed tag like polaroid film.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- mode: python; coding: utf-8-unix -*- | |
# | |
# Time-stamp: <2020-12-12 11:15:14 norim> | |
# | |
# usage: | |
# import framed | |
# ... | |
# img = cv2.imread(input_file) | |
# framed.framed(img, "sample", "working") | |
# | |
# | |
import os | |
from datetime import datetime | |
from optparse import OptionParser | |
import cv2 | |
import numpy as np | |
## | |
# @breif output image with the framed tag. | |
# @param img : target image data | |
# @param title : for naming the output file | |
# @param outdir : if outdir is None, suppressing. | |
# @return image with the frame decoration. | |
# | |
def framed(img, title, outdir=None): | |
borderType = cv2.BORDER_CONSTANT | |
colorWhite = [255,255,255] | |
top = left = right = 5 | |
bottom = 50 | |
framed_img = cv2.copyMakeBorder(img, top, bottom, left, right, borderType,value=colorWhite) | |
font = cv2.FONT_HERSHEY_SIMPLEX | |
# font = cv2.FONT_HERSHEY_PLAIN | |
fontScale = 1.0 | |
fontColor = [0,0,0] | |
thickness = 1 | |
lineType = cv2.LINE_AA | |
labelSize=cv2.getTextSize(title, font, fontScale, lineType) | |
bottomLeftCornerOfText = (5, img.shape[0]+10+ labelSize[0][1]) | |
print (img.shape, bottomLeftCornerOfText) | |
cv2.putText(framed_img, | |
title, bottomLeftCornerOfText, | |
font, fontScale, fontColor, | |
thickness, lineType) | |
if not outdir is None: | |
if not os.path.exists(outdir): | |
os.mkdir(outdir) | |
cv2.imwrite(os.path.join(outdir, title +".png"), framed_img) | |
return (framed_img) | |
# | |
# | |
# | |
if __name__ == '__main__': | |
parser = OptionParser() | |
parser.add_option("-q", "--quiet", | |
action="store_false", dest="verbose", default=True, | |
help="don't print status messages to stdout") | |
(options, args) = parser.parse_args() | |
outdir = datetime.now().strftime("framed%Y%m%d_%H%M%S") | |
for arg in args: | |
bug = [] | |
input_file = arg | |
basename = os.path.splitext(os.path.basename(input_file))[0] | |
# img = cv2.imread(input_file, cv2.IMREAD_GRAYSCALE) | |
img = cv2.imread(input_file) | |
bug.append(framed(img, basename+"_src", outdir)) | |
grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
framed_grey = framed(grey, basename+"_grey", outdir) | |
bgr_grey = cv2.cvtColor(framed_grey, cv2.COLOR_GRAY2BGR) | |
bug.append(bgr_grey) | |
rslt_img = np.hstack(bug) | |
cv2.imwrite(os.path.join(outdir, "_ContactSheet.png"), rslt_img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment