2017-12-28 11 views
1

나는 한 번에 한 문자 씩 이미지 캡션을 작성하여 텍스트 애니메이션 프레임을 만드는 Wand on Python에 자동화 된 스크립트를 작성하려고합니다.완드에있는 텍스트 애니메이션을위한 프레임 빌드

문제는 caption 명령을 사용하여 한 글자를 쓸 때 (설명서는 http://docs.wand-py.org/en/0.4.4/wand/image.html) 거대한 글자를 쓰는 반면 전체 텍스트를 쓰면 이미지에 멋지게 들어간다는 것입니다.

나는 가능한 해결책을 생각했다 : 첫 번째 글자 색을 채우고 나머지는 투명하게 만들어야한다. 그러나 캡션 명령은 내가 아는 한 여러 가지 색의 텍스트를 처리 할 수 ​​없다.

누군가 다른 옵션을 제안 할 수 있다면 감사 할 것입니다.

imgname = random.choice(os.listdir('/home/gionny/Downloads/HighResImg')) 
text = 'Hello, world! This is a slightly longer sentence.' 
fontname = random.choice(os.listdir('/home/gionny/Downloads/font')) 
with Image(filename='HighResImg/'+imgname) as i:  
    font = Font(path = 'font/'+fontname, color = Color('#fff')) 
    textWidth = i.width*2/3 
    textHeight = i.height*2/3 
    offsetLeft = (i.width - textWidth)/2 
    offsetTop = (i.height - textHeight)/2 
    with Image(filename='logo.gif') as l: 
     l.resize(80,80) 
     l.transparentize(0.7) 
     with Drawing() as draw: 
      draw.composite(operator='atop', left=i.width-90, top=i.height-90, width=l.width, height=l.height, image=l) 
      for c in range(0, len(text)): 
       caption = i.caption(text = text[c], left = offsetLeft, top = offsetTop, width=textWidth, height=textHeight, font = font, gravity = 'center') 
       print(caption) 
       cl = i.clone() 
       cl.format = 'jpeg' 
       cl.save(filename='Text/text'+str(c)+'.jpg') 
       cl.destroy() 

답변

1

사람 경우 : 나는

내 코드는 다음과 같습니다 ... 그러나 때 자동으로 내가 아는 한 다음 줄에 가야 계산하지 않는, draw.text 사용할 수 있습니다 나는 또 다른 선택권을 제안 할 수 있었다. 나는 감사 할 것이다. draw.text를 사용할 수는 있지만, 알고있는 한 다음 행을 언제 자동으로 계산하지는 않습니다 ...

x 주위를 빨리 계산할 책임이 있습니다. y 좌표를 반복합니다. 특히 혼합 글꼴을 임의로 사용하는 경우.

wand.drawing.Drawing.get_font_metrics 방법이 이런 종류의 제공됩니다. 각 반복마다 누적 기 &을 업데이트하기 만하면됩니다. leftOffset 캔버스 폭보다 큰 경우

output.gif

from wand.image import Image 
from wand.color import Color 
from wand.drawing import Drawing 


with Image(width=400, height=250, background=Color("skyblue")) as background: 
    leftOffset = 35 # <= Starting position. 
    topOffset = background.height/2; 
    for letter in "Hello World": 
     with Drawing() as ctx: 
      ctx.font = "TimesNewRoman" 
      ctx.font_size = 64.0 
      metrics = ctx.get_font_metrics(background, letter) 
      ctx.text(leftOffset, int(topOffset+metrics.text_height/4), letter) 
      with Image(width=background.width, 
         height=background.height, 
         background=Color("transparent")) as frame: 
       ctx.draw(frame) 
       background.sequence.append(frame) 
      leftOffset += int(metrics.text_width) # <= Adjust for next iteration. 
    background.save(filename="output.gif") 

이제 다음 줄 과정을 반복 위해, 단지 폰트 메트릭스에 의해 text_heighttopOffset을 높일 수 있습니다.

+0

코드에 감사드립니다. 정말 유용했습니다! 나는 또한 다음 라인 프로세스를 구현했다. 그러나 단어를 깨뜨리고 캡션이 자동으로 이미지의 크기를 균일하게 채우기 위해 글꼴 크기를 계산했기 때문에 약간의 연마가 필요하다. 나는 이것도 알아 내야 할 것 같다;) – GionnyBanana

+0

또한 5GB의 여유 디스크 공간과 임시 파일로 많은 양을 먹어서 HD 이미지를 사용하면 스크립트가 다운되는 것을 깨달았습니다. – GionnyBanana

+0

나머지는 알아낼 수 있다고 생각합니다. 그것은 모두 기본 기하학이며, 붙어 있다면 새로운 질문을 게시 할 수 있습니다. HD 이미지의 경우 많은 시간과 리소스가 필요하며 OpenCL/OpenMP 및 HDRI 지원으로 컴파일 된 ImageMagick을 탐색 해 볼 가치가 있습니다. 그러나 솔직히, 나는 위에 열거 된 기본 기능들로 코드 완성이 끝날 때까지 그 문제를 무시할 것이다. – emcconville