Finalizes the Superpose Assembler

This commit is contained in:
Clément 2021-02-24 17:46:54 +01:00
parent fa43eeb60d
commit f92dc419e5

View File

@ -21,14 +21,9 @@ class Superpose extends Assembler
*/ */
public function saveTo($file) public function saveTo($file)
{ {
$image_to_superpose = new \Imagick($this->pool->getImage()); $this->current_image = new \Imagick();
$image_x = $image_to_superpose->getImageWidth(); $this->current_image->setSize($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'));
$image_y = $image_to_superpose->getImageHeight(); $this->current_image->newImage($this->config->get('assembler.sizex'), $this->config->get('assembler.sizey'), 'none');
if ($image_x < 32 || $image_y < 32) {
throw new \Exception('Image too small.');
}
$this->current_image = $image_to_superpose;
$nb_images = $this->config->get('assembler.superpose.min_num_images', 5); $nb_images = $this->config->get('assembler.superpose.min_num_images', 5);
for ($i = 0; $i < $nb_images; $i++) { for ($i = 0; $i < $nb_images; $i++) {
@ -65,6 +60,10 @@ class Superpose extends Assembler
# (while keeping its ratio) # (while keeping its ratio)
$image_x = $image_to_superpose->getImageWidth(); $image_x = $image_to_superpose->getImageWidth();
$image_y = $image_to_superpose->getImageHeight(); $image_y = $image_to_superpose->getImageHeight();
if ($image_x < 32 || $image_y < 32) {
throw new \Exception('Image too small.');
}
if ($image_x > $this->config->get('assembler.sizex') || $image_y > $this->config->get('assembler.sizey')) { if ($image_x > $this->config->get('assembler.sizex') || $image_y > $this->config->get('assembler.sizey')) {
try { try {
$image_to_superpose->scaleImage($this->config->get('assembler.sizex') / 2, $this->config->get('assembler.sizey') / 2, true); $image_to_superpose->scaleImage($this->config->get('assembler.sizex') / 2, $this->config->get('assembler.sizey') / 2, true);
@ -73,13 +72,12 @@ class Superpose extends Assembler
} }
# Scale down/up image if required. # Scale down/up image if required.
/*scaleValue = self.CONFIG["assembler.superpose.scale"] $scale_value = $this->config->get('assembler.superpose.scale', 1);
if str(scaleValue) != "1.0": if ($scale_value !== 1) {
try: $image_to_superpose->thumbnailImage($image_x * $scale_value, $image_y * $scale_value);
imageToSuperpose.thumbnail((int(float(imagex)*scaleValue),int(float(imagey)*scaleValue)),Image.ANTIALIAS) }
except TypeError: #TypeError: unsubscriptable object ; Spurious exception in PIL. :-( $image_x = $image_to_superpose->getImageWidth();
raise BadImage $image_y = $image_to_superpose->getImageHeight();
(imagex,imagey) = imageToSuperpose.size*/
# Compensate for poorly-contrasted images on the web # Compensate for poorly-contrasted images on the web
/*try: /*try:
@ -93,44 +91,51 @@ class Superpose extends Assembler
# We try to dectect them by summing the value of the pixels # We try to dectect them by summing the value of the pixels
# on the borders. # on the borders.
# If the image is considered "white", we invert it. # If the image is considered "white", we invert it.
/*pixelcount = 1 # 1 to prevent divide by zero error. $pixel_count = 1; // to prevent divide by zero error.
valuecount = 0 $value_count = 0;
try: for ($x = 0; $x < $image_x; $x += 20) {
for x in range(0,imagex,20): $pixel = $image_to_superpose->getImagePixelColor($x, 5);
(r,g,b) = imageToSuperpose.getpixel((x,5)) $value_count += array_sum($pixel->getColor());
valuecount += r+g+b $pixel = $image_to_superpose->getImagePixelColor($x, $image_y - 5);
(r,g,b) = imageToSuperpose.getpixel((x,imagey-5)) $value_count += array_sum($pixel->getColor());
valuecount += r+g+b $pixel_count += 2;
pixelcount += 2 }
for y in range(0,imagey,20): for ($y = 0; $y < $image_y; $y += 20) {
(r,g,b) = imageToSuperpose.getpixel((5,y)) $pixel = $image_to_superpose->getImagePixelColor(5, $y);
valuecount += r+g+b $value_count += array_sum($pixel->getColor());
(r,g,b) = imageToSuperpose.getpixel((imagex-5,y)) $pixel = $image_to_superpose->getImagePixelColor($image_x - 5, $y);
valuecount += r+g+b $value_count += array_sum($pixel->getColor());
pixelcount += 2 $pixel_count += 2;
except TypeError: #unsubscriptable object Arrggghh... not again ! }
raise BadImage # Aggrrreeeuuuu...
# If the average r+g+b of the border pixels exceed this value, # If the average r+g+b of the border pixels exceed this value,
# we consider the image is too white, and we invert it. # we consider the image is too white, and we invert it.
if (100*(valuecount/(255*3))/pixelcount)>60: # Cut at 60%. (100% is RGB=(255,255,255)) if (100 * $value_count / (255 * 3) / $pixel_count > 60) {
imageToSuperpose = ImageOps.invert(imageToSuperpose)*/ $image_to_superpose->negateImage(false);
}
$paste_coords_x = random_int(-$image_x, $this->config->get('assembler.sizex')); $paste_coords_x = random_int(-($image_x/2), $this->config->get('assembler.sizex')-$image_x);
$paste_coords_y = random_int(-$image_y, $this->config->get('assembler.sizey')); $paste_coords_y = random_int(-($image_y/2), $this->config->get('assembler.sizey')-$image_y);
# Darken image borders # Darken image borders
$image_to_superpose = $this->darkenImageBorder($image_to_superpose, 30); $image_to_superpose = $this->darkenImageBorder($image_to_superpose);
if ($this->config->get('assembler.superpose.randomrotation', false)) { if ($this->config->get('assembler.superpose.randomrotation', false)) {
$image_to_superpose->rotateImage('none', random_int(0, 359)); $image_to_superpose->rotateImage('none', random_int(0, 359));
$image_to_superpose = $this->darkenImageBorder($image_to_superpose, 30); $image_to_superpose = $this->darkenImageBorder($image_to_superpose);
} }
// mask_image = ImageOps.autocontrast(imageToSuperpose.convert('L')) // help for mask : https://phpimagick.com/Tutorial/backgroundMasking
$image_to_superpose->setImageMatte(true);
$mask_image = clone $image_to_superpose;
$mask_image->setImageType(Imagick::IMGTYPE_GRAYSCALE);
$mask_image->transformImageColorSpace(Imagick::COLORSPACE_GRAY);
// if (self.CONFIG["assembler.superpose.variante"]==1) and (random.randint(0,100)<5): # Invert the transparency of 5% of the images (Except if we are in variante 1 mode) # Invert the transparency of 5% of the images (Except if we are in variante 1 mode)
// mask_image = ImageOps.invert(mask_image) if ($this->config->get('assembler.superpose.variante') === 1 && random_int(0, 100) < 5) {
$mask_image->negateImage(true);
}
$image_to_superpose->compositeImage($mask_image, \Imagick::COMPOSITE_COPYOPACITY, 0, 0);
$current_image->compositeImage($image_to_superpose, \Imagick::COMPOSITE_DEFAULT, $paste_coords_x, $paste_coords_y); $current_image->compositeImage($image_to_superpose, \Imagick::COMPOSITE_DEFAULT, $paste_coords_x, $paste_coords_y);
if ($this->config->get('assembler.superpose.variante') === 0) { if ($this->config->get('assembler.superpose.variante') === 0) {