diff --git a/kindlecomicconverter/color.py b/kindlecomicconverter/color.py deleted file mode 100644 index 1e097ddd..00000000 --- a/kindlecomicconverter/color.py +++ /dev/null @@ -1,86 +0,0 @@ -def colorCheck(image, original_color_mode, opt): - if original_color_mode in ("L", "1"): - return False - if opt.webtoon: - return True - if calculate_color(image, opt): - return True - return False - -# cut off pixels from both ends of the histogram to remove jpg compression artifacts -# for better accuracy, you could split the image in half and analyze each half separately -def histograms_cutoff(cb_hist, cr_hist, cutoff=(2, 2)): - if cutoff == (0, 0): - return cb_hist, cr_hist - - for h in cb_hist, cr_hist: - # get number of pixels - n = sum(h) - # remove cutoff% pixels from the low end - cut = int(n * cutoff[0] // 100) - for lo in range(256): - if cut > h[lo]: - cut = cut - h[lo] - h[lo] = 0 - else: - h[lo] -= cut - cut = 0 - if cut <= 0: - break - # remove cutoff% samples from the high end - cut = int(n * cutoff[1] // 100) - for hi in range(255, -1, -1): - if cut > h[hi]: - cut = cut - h[hi] - h[hi] = 0 - else: - h[hi] -= cut - cut = 0 - if cut <= 0: - break - return cb_hist, cr_hist - -def color_precision(cb_hist_original, cr_hist_original, cutoff, diff_threshold, opt): - cb_hist, cr_hist = histograms_cutoff(cb_hist_original.copy(), cr_hist_original.copy(), cutoff) - - cb_nonzero = [i for i, e in enumerate(cb_hist) if e] - cr_nonzero = [i for i, e in enumerate(cr_hist) if e] - cb_spread = cb_nonzero[-1] - cb_nonzero[0] - cr_spread = cr_nonzero[-1] - cr_nonzero[0] - - # bias adjustment, don't go lower than 7 - SPREAD_THRESHOLD = 7 - if opt.forcecolor: - if any([ - cb_nonzero[0] > 128, - cr_nonzero[0] > 128, - cb_nonzero[-1] < 128, - cr_nonzero[-1] < 128, - ]): - return True, True - elif cb_spread < SPREAD_THRESHOLD and cr_spread < SPREAD_THRESHOLD: - return True, False - - DIFF_THRESHOLD = diff_threshold - if any([ - cb_nonzero[0] <= 128 - DIFF_THRESHOLD, - cr_nonzero[0] <= 128 - DIFF_THRESHOLD, - cb_nonzero[-1] >= 128 + DIFF_THRESHOLD, - cr_nonzero[-1] >= 128 + DIFF_THRESHOLD, - ]): - return True, True - - return False, None - -def calculate_color(image, opt): - img = image.convert("YCbCr") - _, cb, cr = img.split() - cb_hist_original = cb.histogram() - cr_hist_original = cr.histogram() - - # you can increase 22 but don't increase 10. 4 maybe can go higher - for cutoff, diff_threshold in [((0, 0), 22), ((.2, .2), 10), ((3, 3), 4)]: - done, decision = color_precision(cb_hist_original, cr_hist_original, cutoff, diff_threshold, opt) - if done: - return decision - return False \ No newline at end of file diff --git a/kindlecomicconverter/image.py b/kindlecomicconverter/image.py index b7a0a9cb..96206760 100755 --- a/kindlecomicconverter/image.py +++ b/kindlecomicconverter/image.py @@ -27,7 +27,6 @@ from PIL import Image, ImageOps, ImageFile, ImageChops, ImageDraw from .rainbow_artifacts_eraser import erase_rainbow_artifacts from .page_number_crop_alg import get_bbox_crop_margin_page_number, get_bbox_crop_margin from .inter_panel_crop_alg import crop_empty_inter_panel -from .color import colorCheck from .shared import get_contain_resolution AUTO_CROP_THRESHOLD = 0.015 @@ -326,6 +325,94 @@ class ComicPageParser: self.image = crop_empty_inter_panel(self.image, direction, background_color=self.page_background_color) +def colorCheck(image, original_color_mode, opt): + if original_color_mode in ("L", "1"): + return False + if opt.webtoon: + return True + if calculate_color(image, opt): + return True + return False + +# cut off pixels from both ends of the histogram to remove jpg compression artifacts +# for better accuracy, you could split the image in half and analyze each half separately +def histograms_cutoff(cb_hist, cr_hist, cutoff=(2, 2)): + if cutoff == (0, 0): + return cb_hist, cr_hist + + for h in cb_hist, cr_hist: + # get number of pixels + n = sum(h) + # remove cutoff% pixels from the low end + cut = int(n * cutoff[0] // 100) + for lo in range(256): + if cut > h[lo]: + cut = cut - h[lo] + h[lo] = 0 + else: + h[lo] -= cut + cut = 0 + if cut <= 0: + break + # remove cutoff% samples from the high end + cut = int(n * cutoff[1] // 100) + for hi in range(255, -1, -1): + if cut > h[hi]: + cut = cut - h[hi] + h[hi] = 0 + else: + h[hi] -= cut + cut = 0 + if cut <= 0: + break + return cb_hist, cr_hist + +def color_precision(cb_hist_original, cr_hist_original, cutoff, diff_threshold, opt): + cb_hist, cr_hist = histograms_cutoff(cb_hist_original.copy(), cr_hist_original.copy(), cutoff) + + cb_nonzero = [i for i, e in enumerate(cb_hist) if e] + cr_nonzero = [i for i, e in enumerate(cr_hist) if e] + cb_spread = cb_nonzero[-1] - cb_nonzero[0] + cr_spread = cr_nonzero[-1] - cr_nonzero[0] + + # bias adjustment, don't go lower than 7 + SPREAD_THRESHOLD = 7 + if opt.forcecolor: + if any([ + cb_nonzero[0] > 128, + cr_nonzero[0] > 128, + cb_nonzero[-1] < 128, + cr_nonzero[-1] < 128, + ]): + return True, True + elif cb_spread < SPREAD_THRESHOLD and cr_spread < SPREAD_THRESHOLD: + return True, False + + DIFF_THRESHOLD = diff_threshold + if any([ + cb_nonzero[0] <= 128 - DIFF_THRESHOLD, + cr_nonzero[0] <= 128 - DIFF_THRESHOLD, + cb_nonzero[-1] >= 128 + DIFF_THRESHOLD, + cr_nonzero[-1] >= 128 + DIFF_THRESHOLD, + ]): + return True, True + + return False, None + +def calculate_color(image, opt): + img = image.convert("YCbCr") + _, cb, cr = img.split() + cb_hist_original = cb.histogram() + cr_hist_original = cr.histogram() + + # you can increase 22 but don't increase 10. 4 maybe can go higher + for cutoff, diff_threshold in [((0, 0), 22), ((.2, .2), 10), ((3, 3), 4)]: + done, decision = color_precision(cb_hist_original, cr_hist_original, cutoff, diff_threshold, opt) + if done: + return decision + return False + + class ComicPage: def __init__(self, options, mode, path, image, page_background_color, fill): self.opt = options