From 2d96bdc225092efb6cc09479cb8696c931f7a522 Mon Sep 17 00:00:00 2001 From: Benjamin Loison Date: Fri, 29 Mar 2024 13:11:24 +0100 Subject: [PATCH] Not correct generalized `SPLIT_N_X_N` due to `imageWithoutPrnuNpArrayTile` --- algorithms/image_utils/image_utils.py | 8 +++++--- datasets/noise_free_test_images/estimate_prnu.py | 10 ++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/algorithms/image_utils/image_utils.py b/algorithms/image_utils/image_utils.py index 625f611..5d81ff6 100644 --- a/algorithms/image_utils/image_utils.py +++ b/algorithms/image_utils/image_utils.py @@ -23,9 +23,11 @@ def getPrnuShownAsSuch(size): image = Image.new('L', size) draw = ImageDraw.Draw(image) - # I guess that the maximal character height is higher than the maximal character width. Hence, the `TEXT` may not be spanned on the full width. - fontSize = min(HEIGHT, WIDTH // len(TEXT)) - font = ImageFont.truetype(os.path.expanduser('~/.local/share/fonts/impact.ttf'), fontSize) + fontPath = os.path.expanduser('~/.local/share/fonts/impact.ttf') + for fontSize in range(1, HEIGHT + 1): + font = ImageFont.truetype(fontPath, fontSize) + if font.getlength(TEXT) > WIDTH: + break # Center vertically, especially in the case `HEIGHT` > `WIDTH`. draw.text((0, HEIGHT // 2 - fontSize // 2), TEXT, 255, font = font) return np.array(image) \ No newline at end of file diff --git a/datasets/noise_free_test_images/estimate_prnu.py b/datasets/noise_free_test_images/estimate_prnu.py index 6833b24..6d5dc1f 100644 --- a/datasets/noise_free_test_images/estimate_prnu.py +++ b/datasets/noise_free_test_images/estimate_prnu.py @@ -25,7 +25,7 @@ datasetPath = 'no_noise_images' # In addition to the range difference, note that the distribution in the first set of images was a Gaussian and here is very different and specific. PRNU_FACTOR = 0.01 NOISE_FACTOR = 0.1 -SPLIT_N_X_N = 1 +SPLIT_N_X_N = 4 IMAGE_SIZE_SHAPE = [dimension // SPLIT_N_X_N for dimension in (704, 469)] @@ -33,7 +33,6 @@ np.random.seed(0) #prnuNpArray = 255 * randomGaussianImage(scale = PRNU_FACTOR, size = IMAGE_SIZE_SHAPE) prnuNpArray = getPrnuShownAsSuch(IMAGE_SIZE_SHAPE) * PRNU_FACTOR -showImageWithMatplotlib(prnuNpArray) def isIn256Range(x): return 0 <= x and x <= 255 @@ -46,11 +45,14 @@ for imageName in os.listdir(datasetPath): imageWithoutPrnuPil = Image.open(imagePath).convert('F') imageWithoutPrnuNpArray = np.array(imageWithoutPrnuPil) - m = imageWithoutPrnuNpArray.shape[0] // SPLIT_N_X_N - n = imageWithoutPrnuNpArray.shape[1] // SPLIT_N_X_N + m = IMAGE_SIZE_SHAPE[1] + n = IMAGE_SIZE_SHAPE[0] imageWithoutPrnuNpArrayTiles = [imageWithoutPrnuNpArray[x : x + m, y : y + n] for x in range(0, imageWithoutPrnuNpArray.shape[0], m) for y in range(0, imageWithoutPrnuNpArray.shape[1], n)] for imageWithoutPrnuNpArrayTile in imageWithoutPrnuNpArrayTiles: + #print(imageWithoutPrnuNpArrayTile.shape, tuple(IMAGE_SIZE_SHAPE[::-1])) + #if imageWithoutPrnuNpArrayTile.shape != tuple(IMAGE_SIZE_SHAPE[::-1]): + # continue imageNoise = randomGaussianImage(scale = 255 * NOISE_FACTOR, size = imageWithoutPrnuNpArrayTile.shape) imageWithPrnuNpArray = imageWithoutPrnuNpArrayTile + prnuNpArray + imageNoise #assert all([isIn256Range(extreme) for extreme in [imageWithPrnuNpArray.max(), imageWithPrnuNpArray.min()]]), 'Adding the PRNU resulted in out of 256 bounds image'