Libvips: É possível adicionar preenchimento e manter a proporção de 1 para 1?

Criado em 30 out. 2017  ·  5Comentários  ·  Fonte: libvips/libvips

Olá,

Comecei recentemente a usar esta base de código e gostaria de afirmar que está funcionando muito bem até agora! Muito obrigado.

Gostaria de ver se já é possível ou se está em andamento adicionar preenchimento a uma imagem e preservar a proporção. Usamos uma ferramenta que faz isso, mas tende a ser muito lenta. Como você pode ver, na segunda imagem, ele adiciona o preenchimento e encontra a cor mais ponderada para usar.

Por exemplo:
Imagem original:
cobain1

Imagem preenchida com proporção de 1 para 1:

cobain2

Obrigado e espero que tudo isso faça sentido :)

question

Comentários muito úteis

Criei um pequeno programa pyvips para fazer isso:

import sys
import pyvips

im = pyvips.Image.new_from_file(sys.argv[1])

# the margin of pixel we extract to get the average edge
margin = 10 

# paste black over the centre, take the histogram of the whole image
square = pyvips.Image.black(im.width - 2 * margin, im.height - 2 * margin)
hist = im.insert(square, margin, margin).hist_find()

# zap the 0 column to remove the black square
onepx = pyvips.Image.black(1, 1)
hist = hist.insert(onepx, 0, 0) 

# then the histogram peak is the most common value in each band
bg = [x.maxpos()[1] for x in hist.gaussblur(1).bandsplit()]

# extend image out with that background
size = max(im.width, im.height)
im = im.embed((size - im.width) / 2, (size - im.height) / 2, size, size,
              extend='background', background=bg)

im.write_to_file(sys.argv[2])

Execute assim:

john<strong i="9">@kiwi</strong>:~/try$ python extend_avg.py ~/pics/greybg.png x.png

Para sua imagem de teste, recebo:

x

Todos 5 comentários

Não seria difícil em algo como pyvips / ruby-vips / etc.

Suponho que eu cortaria as bordas, pegaria um histograma 3D, encontraria o pico (que seria a cor mais comum nas áreas das bordas) e, em seguida, usaria essa cor para expandir o original.

@jcupitt Obrigado por sua resposta rápida!

Eu estaria pedindo muito para que isso fosse adicionado como um recurso por acaso? Eu estarei procurando para ver o que posso encontrar / realizar. Não posso dizer que sou um especialista, mas vou tentar!

Criei um pequeno programa pyvips para fazer isso:

import sys
import pyvips

im = pyvips.Image.new_from_file(sys.argv[1])

# the margin of pixel we extract to get the average edge
margin = 10 

# paste black over the centre, take the histogram of the whole image
square = pyvips.Image.black(im.width - 2 * margin, im.height - 2 * margin)
hist = im.insert(square, margin, margin).hist_find()

# zap the 0 column to remove the black square
onepx = pyvips.Image.black(1, 1)
hist = hist.insert(onepx, 0, 0) 

# then the histogram peak is the most common value in each band
bg = [x.maxpos()[1] for x in hist.gaussblur(1).bandsplit()]

# extend image out with that background
size = max(im.width, im.height)
im = im.embed((size - im.width) / 2, (size - im.height) / 2, size, size,
              extend='background', background=bg)

im.write_to_file(sys.argv[2])

Execute assim:

john<strong i="9">@kiwi</strong>:~/try$ python extend_avg.py ~/pics/greybg.png x.png

Para sua imagem de teste, recebo:

x

@jcupitt você é o cara, muito obrigado por fazer isso! +1 +1 +1

Não tem problema, vou fechar.

Esta página foi útil?
0 / 5 - 0 avaliações

Questões relacionadas

BorntraegerMarc picture BorntraegerMarc  ·  3Comentários

gencer picture gencer  ·  4Comentários

nattfodd picture nattfodd  ·  5Comentários

adamu picture adamu  ·  3Comentários

Kyle-Kyle picture Kyle-Kyle  ·  4Comentários