From 60e05c78eab4e8361f5e0658b4f7e0d389d95cef Mon Sep 17 00:00:00 2001 From: wizardforcel <562826179@qq.com> Date: Mon, 27 Jul 2020 12:31:53 +0800 Subject: [PATCH] 2020-07-27 12:31:53 --- imgyaso/adathres.py | 10 +++++++--- imgyaso/dither.py | 21 ++++++++++++++------- imgyaso/quant.py | 9 +++++++-- imgyaso/trunc.py | 10 +++++++--- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/imgyaso/adathres.py b/imgyaso/adathres.py index 91edd2c..6470c49 100644 --- a/imgyaso/adathres.py +++ b/imgyaso/adathres.py @@ -7,11 +7,15 @@ from os import path import sys def adathres_bts(img, win=9, beta=0.9): - img = cv2.imdecode(np.frombuffer(img, np.uint8), cv2.IMREAD_GRAYSCALE) + img = np.frombuffer(img, np.uint8) + img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) if img is None: return None img = adathres(img, win, beta).astype(np.uint8) - img = bytes(cv2.imencode('.png', img, [cv2.IMWRITE_PNG_BILEVEL, 1])[1]) - return img + img = cv2.imencode( + '.png', img, + [cv2.IMWRITE_PNG_BILEVEL, 1] + )[1] + return bytes(img) def adathres(img, win=9, beta=0.9): if win % 2 == 0: win = win - 1 diff --git a/imgyaso/dither.py b/imgyaso/dither.py index 4de3119..2e12135 100644 --- a/imgyaso/dither.py +++ b/imgyaso/dither.py @@ -109,11 +109,15 @@ def grid(img): return img def grid_bts(img): - img = cv2.imdecode(np.frombuffer(img, np.uint8), cv2.IMREAD_GRAYSCALE) + img = np.frombuffer(img, np.uint8) + img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) if img is None: return None img = grid(img).astype(np.uint8) - img = bytes(cv2.imencode('.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 9])[1]) - return img + img = cv2.imencode( + '.png', img, + [cv2.IMWRITE_PNG_COMPRESSION, 9] + )[1] + return bytes(img) def noise(img): assert img.ndim == 2 @@ -130,12 +134,15 @@ def noise(img): return img def noise_bts(img): - img = cv2.imdecode(np.frombuffer(img, np.uint8), cv2.IMREAD_GRAYSCALE) + img = np.frombuffer(img, np.uint8) + img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) if img is None: return None img = noise(img).astype(np.uint8) - img = bytes(cv2.imencode('.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 9])[1]) - return img - + img = cv2.imencode( + '.png', img, + [cv2.IMWRITE_PNG_COMPRESSION, 9] + )[1] + return bytes(img) def main(): fname = sys.argv[1] diff --git a/imgyaso/quant.py b/imgyaso/quant.py index b330974..fb7cc4e 100644 --- a/imgyaso/quant.py +++ b/imgyaso/quant.py @@ -6,9 +6,14 @@ from io import BytesIO from .util import * def pngquant(img, ncolors=8): - img = bytes(cv2.imencode('.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 9])[1]) + img = cv2.imencode( + '.png', img, + [cv2.IMWRITE_PNG_COMPRESSION, 9] + )[1] + img = bytes(img) img = pngquant_bts(img, ncolors) - return cv2.imdecode(np.frombuffer(img, np.uint8), cv2.IMREAD_UNCHANGED) + img = np.frombuffer(img, np.uint8) + return cv2.imdecode(img, cv2.IMREAD_UNCHANGED) def pngquant_bts(img, ncolors=8): img = conv2png(img) diff --git a/imgyaso/trunc.py b/imgyaso/trunc.py index 2d83e99..1da6272 100644 --- a/imgyaso/trunc.py +++ b/imgyaso/trunc.py @@ -3,11 +3,15 @@ import cv2 import numpy as np def trunc_bts(img, l=4): - img = cv2.imdecode(np.frombuffer(img, np.uint8), cv2.IMREAD_GRAYSCALE) + img = np.frombuffer(img, np.uint8) + img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE) if img is None: return None img = trunc(img, l).astype(np.uint8) - img = bytes(cv2.imencode('.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 9])[1]) - return img + img = cv2.imencode( + '.png', img, + [cv2.IMWRITE_PNG_COMPRESSION, 9] + )[1] + return bytes(img) def trunc(img, l=4): -- GitLab