-
-
Save nomissbowling/b4702062404a0515af19b3c885316a1c to your computer and use it in GitHub Desktop.
_test_OpenCV_jp_file_.py
This file contains hidden or 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
#!/usr/local/bin/python | |
# -*- coding: utf-8 -*- | |
'''_test_OpenCV_jp_file_ | |
''' | |
import sys, os | |
import numpy as np | |
from PIL import Image | |
from matplotlib import pyplot as plt | |
import cv2 | |
FNAME_J = '_4色型色覚テスト.png' | |
FNAME_E = '_4colors_sense_test.png' | |
def imwrite_via_numpy(fn, ext, im): | |
if im.ndim == 3: im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR) | |
r, buf = cv2.imencode(ext, im) | |
if r: | |
with open(fn, mode='wb') as f: buf.tofile(f) | |
else: | |
print('error') | |
def imread_via_numpy(fn): | |
flg = cv2.IMREAD_COLOR # cv2.IMREAD_UNCHANGED | |
img = np.fromfile(fn, dtype=np.uint8) | |
im = cv2.imdecode(img, flg) # not use cv2.imread for jp file | |
if im.ndim == 3: im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB) | |
return im | |
def imwrite_via_PIL(fn, ext, im): | |
# Image.fromarray(im.astype(np.uint8)).save(fn) # when im is np.float64 etc | |
# Image.fromarray((im * 255).astype(np.uint8)).save(fn) # 0.0 <= im <= 1.0 | |
Image.fromarray(im).save(fn) # , quality=(1 to 95 default 75) when .jpg | |
def imread_via_PIL(fn): | |
img = Image.open(fn) | |
# im = np.array(img, dtype=np.uint8) # OK copied | |
im = np.asarray(img, dtype=np.uint8) # OK readonly | |
return im | |
def test_jp_file(fnj, fne): | |
fig = plt.figure(figsize=(16, 9), dpi=96) | |
ax = fig.add_subplot(1, 1, 1) | |
''' | |
cv2.initFont(font_struct=struct, font_face=1, hscale=1.0, vscale=1.0, | |
shear=0.0, thickness=1, line_type=8) | |
cv2.FONT_HERSHEY_SIMPLEX | |
cv2.FONT_HERSHEY_PLAIN | |
cv2.FONT_HERSHEY_DUPLEX | |
cv2.FONT_HERSHEY_COMPLEX | |
cv2.FONT_HERSHEY_TRIPLEX | |
cv2.FONT_HERSHEY_COMPLEX_SMALL | |
cv2.FONT_HERSHEY_SCRIPT_SIMPLEX | |
cv2.FONT_HERSHEY_SCRIPT_COMPLEX | |
| cv2.FONT_ITALIC (no effect ?) | |
''' | |
ff = cv2.FONT_HERSHEY_DUPLEX | cv2.FONT_ITALIC # font face | |
fs = 1.2 # font scale | |
col = (240, 192, 32) # color | |
tn = 3 # thickness default 1 | |
lt = cv2.LINE_AA # lineType cv2.LINE(_8 or _4 or _AA anti aliased) default _8 | |
blo = False # bottomLeftOrigin default False (character top bottom when True) | |
if False: | |
im = imread_via_numpy(fnj) | |
im = cv2.putText(im, 'R: numpy', (10, 300), ff, fs, col, tn, lt, blo) | |
else: | |
im = imread_via_PIL(fnj) | |
im = cv2.putText(im, 'R: PIL', (10, 300), ff, fs, col, tn, lt, blo) | |
if False: | |
im = cv2.putText(im, 'W: numpy', (10, 250), ff, fs, col, tn, lt, blo) | |
imwrite_via_numpy(fne, '.png', im) # size > via_PIL | |
else: | |
im = cv2.putText(im, 'W: PIL', (10, 250), ff, fs, col, tn, lt, blo) | |
imwrite_via_PIL(fne, '.png', im) # size < via_numpy | |
ax.imshow(im) | |
plt.show() | |
if __name__ == '__main__': | |
test_jp_file(FNAME_J, FNAME_E) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment