41 lines
1.5 KiB
Python
41 lines
1.5 KiB
Python
from PIL import Image, ImageFont, ImageDraw
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
import os
|
|
|
|
def randomGaussianImage(scale, size):
|
|
return np.random.normal(loc = 0, scale = scale, size = size)
|
|
|
|
# `cmap` source: https://matplotlib.org/3.8.0/api/_as_gen/matplotlib.pyplot.imshow.html
|
|
def showImageWithMatplotlib(npArray, title = None, cmap = 'viridis'):
|
|
if title is not None:
|
|
plt.title(title)
|
|
plt.imshow(npArray, cmap = cmap)
|
|
plt.show()
|
|
|
|
def toPilImage(npArray):
|
|
return Image.fromarray(npArray)
|
|
|
|
def getPrnuShownAsSuch(size, gaussianNoise = 0):
|
|
# Supports `WIDTH` > `HEIGHT` and conversely.
|
|
WIDTH, HEIGHT = size
|
|
TEXT = 'PRNU'
|
|
|
|
imagePil = Image.new('L', size)
|
|
draw = ImageDraw.Draw(imagePil)
|
|
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)
|
|
imageNpArray = np.array(imagePil)
|
|
gaussianNoiseNpArray = randomGaussianImage(gaussianNoise, size[::-1])
|
|
#prnuShownAsSuch = imageNpArray + gaussianNoiseNpArray
|
|
prnuShownAsSuch = imageNpArray
|
|
for y in range(HEIGHT):
|
|
for x in range(WIDTH):
|
|
if prnuShownAsSuch[y, x] != 0:
|
|
prnuShownAsSuch[y, x] += gaussianNoiseNpArray[y, x]
|
|
return prnuShownAsSuch |