TCAX 字幕特效制作工具官方论坛 | ASS | TCAS | Python | Aegisub | Lua

 找回密码
 新人加入
查看: 9307|回复: 5

[其他代码] PyCairo模块简单示例代码 [复制链接]

Administrator

TCAX Dev.

Rank: 7Rank: 7Rank: 7

发表于 2011-12-20 06:09:32 |显示全部楼层
说明

对于TCAX 1.0.0以前的版本, 需要先添加PyCairo模块, 下载地址 http://www.tcax.org/forum.php?mod=viewthread&tid=209, 推荐直接升级到1.0.0
附件为完整工程.
本例子仅作为一个展示用例. 实际上cairo是很强大的2D矢量绘图工具
  1. from tcaxPy import *
  2. from util.cairo import *

  3. def tcaxPy_Init():
  4.     global Font
  5.     _Fs = GetVal(val_FontSize)
  6.     _FileName = GetVal(val_FontFileName)
  7.     _FaceID = GetVal(val_FaceID)
  8.     Font = InitFont(_FileName, _FaceID, _Fs * 4, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(0, 0, 0), 0, False)

  9. def tcaxPy_Fin():
  10.     FinFont(Font)

  11. def tcaxPy_User():
  12.     file_name = GetVal(val_OutFile) + '.tcas'
  13.     fx_width = GetVal(val_ResolutionX)
  14.     fx_height = GetVal(val_ResolutionY)
  15.     fx_fps = GetVal(val_FXFPS)
  16.     TCAS_FILE = CreateTcasFile(file_name, fx_width, fx_height, fx_fps)
  17.     TCAS_BUF = []
  18.     surface = ImageSurface(FORMAT_ARGB32, fx_width, fx_height)
  19.     ctx = Context(surface)
  20.     # object 1
  21.     ctx.set_line_width(2)
  22.     ctx.set_source_rgb(0.69, 0.19, 0)
  23.     ctx.arc(fx_width / 2, fx_height / 2, fx_height / 20, 0, 2 * pi)
  24.     ctx.stroke_preserve()
  25.     ctx.set_source_rgb(0.3, 0.4, 0.6)
  26.     ctx.fill()
  27.     # object 2
  28.     ctx.set_line_join(LINE_JOIN_ROUND)
  29.     ctx.set_line_cap(LINE_CAP_ROUND)
  30.     ctx.translate(300, 200)
  31.     ctx.scale(10, 10)
  32.     AssDraw(ctx, 'm 20 0  b  20 10  30 20  40 20  b  30 20  20 30  20 40  b  20 30  10 20  0 20  b  10 20  20 10  20 0')
  33.     ctx.set_source_rgb(0.69, 0.19, 1)
  34.     ctx.stroke()
  35.     # object 3
  36.     ctx.scale(1 / 10, 1 / 10)
  37.     ctx.translate(-100, -100)
  38.     AssDraw(ctx, 'm 69 67 l 69 67 l 69 67 l 69 63 l 69 59 l 70 56 l 72 58 l 76 49 l 74 48 l 76 44 l 77 43 l 71 34 l 67 21 l 67 17 l 71 19 l 77 29 l 81 40 l 87 28 l 85 23 l 84 16 l 86 15 l 89 19 l 90 25 l 95 14 l 103 5 l 100 16 l 95 26 l 99 26 l 103 28 l 99 31 l 94 30 l 86 43 l 95 42 l 106 45 l 103 49 l 93 49 l 86 48 l 86 50 l 84 53 l 82 52 l 73 68 l 76 69 l 88 68 l 88 66 l 94 66 l 96 67 l 98 58 l 108 48 l 111 51 l 110 56 l 104 63 l 100 68 l 113 68 l 114 64 l 119 60 l 120 62 l 120 67 l 117 69 l 128 69 l 137 72 l 134 73 l 127 74 l 117 74 l 120 79 l 120 83 l 117 82 l 114 79 l 113 74 l 100 75 l 105 84 l 108 84 l 109 86 l 108 86 l 111 95 l 111 100 l 106 97 l 102 91 l 95 78 l 92 78 l 90 75 l 73 75 l 82 91 l 83 90 l 86 94 l 86 96 l 86 94 l 97 95 l 100 96 l 101 98 l 99 102 l 96 104 l 94 104 l 94 103 l 85 101 l 92 113 l 97 113 l 98 115 l 98 116 l 94 116 l 98 126 l 100 131 l 100 134 l 97 131 l 90 119 l 87 123 l 85 122 l 86 117 l 88 116 l 82 104 l 79 112 l 80 114 l 78 117 l 70 119 l 69 114 l 76 102 l 78 101 l 75 95 l 77 94 l 69 79 l 67 82 l 69 85 l 66 90 l 63 88 l 61 92 l 61 96 l 59 101 l 57 100 l 60 102 l 64 109 l 68 117 l 68 123 l 64 122 l 61 119 l 59 119 l 57 116 l 59 114 l 55 104 l 52 110 l 50 116 l 48 118 l 51 120 l 52 124 l 52 126 l 49 125 l 46 122 l 42 129 l 40 134 l 35 136 l 36 131 l 37 127 l 43 119 l 38 119 l 36 117 l 37 116 l 41 115 l 44 116 l 51 102 l 42 103 l 40 106 l 38 106 l 36 105 l 36 103 l 33 101 l 31 99 l 35 96 l 39 96 l 48 96 l 50 98 l 52 98 l 52 96 l 54 92 l 56 92 l 58 86 l 56 84 l 60 80 l 61 80 l 66 74 l 61 75 l 58 78 l 55 78 l 53 77 l 53 75 l 46 75 l 47 76 l 46 77 l 42 77 l 36 89 l 34 93 l 29 95 l 27 92 l 27 88 l 27 85 l 28 83 l 32 83 l 37 75 l 24 75 l 24 76 l 21 81 l 18 82 l 17 80 l 18 77 l 20 74 l 10 74 l 2 72 l 6 70 l 20 70 l 18 67 l 17 64 l 18 61 l 20 62 l 22 65 l 24 68 l 23 69 l 37 70 l 32 62 l 28 62 l 27 60 l 28 56 l 27 52 l 29 49 l 31 49 l 35 53 l 41 65 l 41 67 l 45 68 l 53 69 l 53 67 l 54 65 l 58 66 l 60 68 l 64 69 l 61 66 l 58 64 l 57 58 l 54 52 l 52 53 l 51 50 l 51 49 l 46 50 l 38 50 l 31 47 l 34 42 l 36 41 l 38 39 l 41 39 l 42 42 l 45 42 l 50 43 l 44 31 l 44 32 l 38 32 l 35 30 l 37 27 l 41 27 l 44 28 l 38 20 l 35 12 l 35 6 l 38 10 l 43 16 l 46 24 l 48 20 l 51 19 l 52 23 l 50 27 l 48 27 l 55 40 l 57 32 l 56 29 l 58 27 l 60 26 l 61 24 l 65 25 l 67 28 l 63 39 l 61 43 l 59 43 l 61 50 l 61 51 l 65 58 l 66 56 l 68 56 l 68 60 l 67 62')
  39.     ctx.set_source_rgb(0.3, 0.4, 0.6)
  40.     ctx.fill_preserve()
  41.     ctx.set_source_rgb(0.3, 0, 0.6)
  42.     ctx.stroke()
  43.     # object 4
  44.     ctx.scale(1 / 64, 1 / 64)
  45.     AssDraw(ctx, TextOutlineDraw(Font, 'あ', 600, 300))
  46.     ctx.set_source_rgb(0.6, 0.4, 0.1)
  47.     ctx.fill_preserve()
  48.     ctx.set_line_width(0.5 * 64)
  49.     ctx.set_source_rgb(1, 0.1, 0.4)
  50.     ctx.stroke()
  51.     # save the result
  52.     surface.write_to_png(abspath('p1.png'))
  53.     PIX = surface.get_pix()
  54.     PIX = PixBlur(PIX, 10)
  55.     SavePix(abspath('p2.png'), PIX)
  56.     tcas_main(TCAS_BUF, PIX, 0, 100000, 0, 0, 0)
  57.     WriteTcasFile(TCAS_FILE, TCAS_BUF)     # write the buffer in memory to the file
  58.     FinTcasFile(TCAS_FILE)
复制代码

tcas_test_cairo.rar

2.56 KB, 下载次数: 3301

完整工程

Administrator

TCAX Dev.

Rank: 7Rank: 7Rank: 7

发表于 2011-12-20 06:23:39 |显示全部楼层
效果截图 (点击可放大)

原始

原始

增加模糊效果

增加模糊效果


Administrator

TCAX Dev.

Rank: 7Rank: 7Rank: 7

发表于 2011-12-20 09:09:57 |显示全部楼层
补充示例
  1. from tcaxPy import *
  2. from util.cairo import *

  3. def tcaxPy_Init():
  4.     global Font
  5.     global _Fs
  6.     _Fs = GetVal(val_FontSize)
  7.     _FileName = GetVal(val_FontFileName)
  8.     _FaceID = GetVal(val_FaceID)
  9.     Font = InitFont(_FileName, _FaceID, _Fs * 10, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(0, 0, 0), 0, False)

  10. def tcaxPy_Fin():
  11.     FinFont(Font)

  12. def tcaxPy_User():
  13.     file_name = GetVal(val_OutFile) + '.tcas'
  14.     fx_width = GetVal(val_ResolutionX)
  15.     fx_height = GetVal(val_ResolutionY)
  16.     fx_fps = GetVal(val_FXFPS)
  17.     TCAS_FILE = CreateTcasFile(file_name, fx_width, fx_height, fx_fps)
  18.     FD = 1000 / fx_fps
  19.     TCAS_BUF = []
  20.     # using cairo to draw a text
  21.     surface = ImageSurface(FORMAT_ARGB32, fx_width, fx_height)
  22.     ctx = Context(surface)
  23.     outline = TextOutlineDraw(Font, 'あ', 0, 0)
  24.     ctx.scale(1 / 64, 1 / 64)
  25.     AssDraw(ctx, outline)
  26.     ctx.set_source_rgb(1, 0, 0)  # red
  27.     ctx.fill()
  28.     PIX = surface.get_pix()
  29.     tcas_main(TCAS_BUF, PIX, 0, 10000, 100, 100, 0)
  30.     # using tcaxLib to draw a text
  31.     PIX = TextPix(Font, 'あ')
  32.     PIX = PixColorRGB(PIX, MakeRGB(0, 255, 0))  # green
  33.     tcas_main(TCAS_BUF, PIX, 0, 10000, 400, 100, 0)
  34.     WriteTcasFile(TCAS_FILE, TCAS_BUF)     # write the buffer in memory to the file
  35.     FinTcasFile(TCAS_FILE)
  36.    
  37.     # ASS alternative
  38.     file_name = GetVal(val_OutFile) + '.ass'
  39.     ass_header = GetVal(val_AssHeader)
  40.     ASS_FILE = CreateAssFile(file_name, ass_header)
  41.     ASS_BUF = []
  42.     ass_main(ASS_BUF, SubL(0, 1000), an(7) + pos(700, 100) + fs(_Fs * 10) + bord(0) + color1('FF0000'), 'あ')    # blue
  43.     WriteAssFile(ASS_FILE, ASS_BUF)     # write the buffer in memory to the file
  44.     FinAssFile(ASS_FILE)
复制代码

tcas_test_cairo_2.rar

1.67 KB, 下载次数: 3226

完整工程

Administrator

TCAX Dev.

Rank: 7Rank: 7Rank: 7

发表于 2011-12-20 09:12:01 |显示全部楼层
效果截图 (点击可放大)

p3.png

Administrator

TCAX Dev.

Rank: 7Rank: 7Rank: 7

发表于 2011-12-20 11:47:11 |显示全部楼层
补充示例, 字体平滑缩放 (动态)
  1. from tcaxPy import *
  2. from util.cairo import *

  3. def tcaxPy_Init():
  4.     global Font
  5.     global _Fs
  6.     _Fs = GetVal(val_FontSize) * 2
  7.     _FileName = GetVal(val_FontFileName)
  8.     _FaceID = GetVal(val_FaceID)
  9.     Font = InitFont(_FileName, _FaceID, _Fs, GetVal(val_Spacing), GetVal(val_SpaceScale), MakeRGB(0, 0, 0), 0, False)

  10. def tcaxPy_Fin():
  11.     FinFont(Font)

  12. def tcaxPy_User():
  13.     file_name = GetVal(val_OutFile) + '.tcas'
  14.     fx_width = GetVal(val_ResolutionX)
  15.     fx_height = GetVal(val_ResolutionY)
  16.     fx_fps = GetVal(val_FXFPS)
  17.     TCAS_FILE = CreateTcasFile(file_name, fx_width, fx_height, fx_fps)
  18.     FD = 1000 / fx_fps
  19.     # using cairo to scale a text
  20.     surface = ImageSurface(FORMAT_ARGB32, fx_width, fx_height)
  21.     ctx = Context(surface)
  22.     outline = TextOutlineDraw(Font, 'あ', 0, 0)
  23.     metrics = TextMetrics(Font, 'あ')
  24.     ctx.translate(300 - metrics[4] / 2, 300 - _Fs / 2)
  25.     fac = 1.5
  26.     n = 200
  27.     for i in range(n):
  28.         TCAS_BUF = []
  29.         ctx.translate(-_Fs * fac / 2 / n, -_Fs * fac / 2 / n)
  30.         f = 1/ 64 * (1 + fac * i / n)
  31.         ctx.scale(f, f)
  32.         AssDraw(ctx, outline)
  33.         ctx.set_source_rgb(1, 0, 0)
  34.         ctx.fill()
  35.         PIX = surface.get_pix()
  36.         #PIX = PixStrip(PIX)    # need the latest tcaxLib.pyd module
  37.         PIX = PixColorRGB(PIX, MakeRGB(255, 0, 255))
  38.         ctx.scale(1 / f, 1 / f)
  39.         clear_surface(ctx)
  40.         tcas_main(TCAS_BUF, PIX, 300 + FD * i, 300 + FD * (i + 1), 0, 0, 0)
  41.         WriteTcasFile(TCAS_FILE, TCAS_BUF)     # write the buffer in memory to the file
  42.         progress(i + 1, n)
  43.     FinTcasFile(TCAS_FILE)
  44.     # ASS alternative
  45.     file_name = GetVal(val_OutFile) + '.ass'
  46.     ass_header = GetVal(val_AssHeader)
  47.     ASS_FILE = CreateAssFile(file_name, ass_header)
  48.     ASS_BUF = []
  49.     ass_main(ASS_BUF, SubL(30, 30 + FD * n / 10), an(5) + pos(300, 300) + fs(_Fs) + t(fs(_Fs * (1 + fac))) + bord(0) + color1('FF0000'), 'あ')    # blue
  50.     ass_main(ASS_BUF, SubL(30, 30 + FD * n / 10), an(5) + pos(500, 300) + fs(_Fs) + t(fs(_Fs * (1 + fac))) + bord(0) + color1('0000FF'), 'あ')    # red
  51.     WriteAssFile(ASS_FILE, ASS_BUF)     # write the buffer in memory to the file
  52.     FinAssFile(ASS_FILE)


  53. def clear_surface(ctx):
  54.     ctx.save()
  55.     ctx.set_source_rgba(0, 0, 0, 0)
  56.     ctx.set_operator(OPERATOR_SOURCE)
  57.     ctx.paint()
  58.     ctx.restore()
复制代码

tcas_test_cairo_3.rar

0 Bytes, 下载次数: 3180

完整工程

Administrator

TCAX Dev.

Rank: 7Rank: 7Rank: 7

发表于 2012-1-4 21:07:23 |显示全部楼层
补充示例, 参考模板
  1. from tcaxPy import *
  2. from util.cairo import *

  3. def tcaxPy_User():
  4.     GetHelp()   # get the description of the global variables, comment them if you don't need
  5.     # initilization
  6.     file_name = GetVal(val_OutFile) + '.tcas'
  7.     fx_width = GetVal(val_ResolutionX)
  8.     fx_height = GetVal(val_ResolutionY)
  9.     fx_fps = GetVal(val_FXFPS)
  10.     TCAS_FILE = CreateTcasFile(file_name, fx_width, fx_height, fx_fps)
  11.     surface = ImageSurface(FORMAT_ARGB32, fx_width, fx_height)
  12.     ctx = Context(surface)
  13.     Font = InitFont(GetVal(val_FontFileName), GetVal(val_FaceID), GetVal(val_FontSize), GetVal(val_Spacing), GetVal(val_SpaceScale), HexToDec(GetVal(val_1C)), GetVal(val_Bord), False)
  14.     # retrieve basic information
  15.     FD         = 1000 / fx_fps
  16.     fontSize   = GetVal(val_FontSize)
  17.     marginX    = GetVal(val_OffsetX)
  18.     marginY    = GetVal(val_OffsetY)
  19.     spacing    = GetVal(val_Spacing)
  20.     lineNum    = GetVal(val_nLines)
  21.     textNum    = GetVal(val_nTexts)
  22.     start      = GetVal(val_BegTime)
  23.     end        = GetVal(val_EndTime)
  24.     kar        = GetVal(val_KarTime)
  25.     elapKar    = GetVal(val_KarTimeDiff)
  26.     text       = GetVal(val_Text)
  27.     textLength = GetVal(val_TextLength)
  28.     width      = GetVal(val_TextWidth)
  29.     height     = GetVal(val_TextHeight)
  30.     advance    = GetVal(val_TextAdvance)
  31.     advDiff    = GetVal(val_TextAdvanceDiff)
  32.     # main loop
  33.     for i in range(lineNum):
  34.         initPosX = (fx_width - textLength[i]) / 2 + marginX        # if marginX = 0, then it's just on the middle
  35.         initPosY = fx_height - fontSize - marginY
  36.         for j in range(textNum[i]):
  37.             if text[i][j] == '' or text[i][j] == ' ' or text[i][j] == ' ':
  38.                 continue
  39.             posX = initPosX + advDiff[i][j] + advance[i][j] / 2
  40.             posY = initPosY
  41.             # draw by Cairo Toy Font API, color is pink, without border
  42.             ctx.select_font_face(GetVal(val_FontFaceName))
  43.             ctx.set_font_size(fontSize)
  44.             ctx.move_to(posX, posY + fontSize + GetVal(val_Descender))
  45.             ctx.text_path(text[i][j])
  46.             ctx.set_source_rgb(1, 0, 1)
  47.             ctx.fill()
  48.             # draw by tcaxLib TextOutlineDraw API, color is red, without border
  49.             ctx.save()
  50.             ctx.scale(1 / 64, 1 / 64)
  51.             outline = TextOutlineDraw(Font, text[i][j], posX, posY)
  52.             AssDraw(ctx, outline)
  53.             ctx.set_source_rgb(1, 0, 0)
  54.             ctx.fill()
  55.             ctx.restore()
  56.             # draw by tcaxLib TextPix API, color is blue, with border = 1 (according to the setting of the TCC file)
  57.             TCAS_BUF = []
  58.             PIX = TextPix(Font, text[i][j])
  59.             PIX = PixColorRGB(PIX, MakeRGB(0, 0, 0))
  60.             tcas_main(TCAS_BUF, PIX, start[i] * 10, end[i] * 10, posX, posY, 0)
  61.             WriteTcasFile(TCAS_FILE, TCAS_BUF)
  62.             # you can also draw the text by ASS, through various methods, such as direct text, draw by ASS drawing commands or draw by pixels, and hopefully note that all kinds of text drawing will likely to produce the same result, cool huh :)
  63.             # the result of this script is that lines of red texts with blue borders, let me explain, the pink texts are covered by the red one, since they comes later and on the same layer, the blue ones are written to the TCAS file earliest, but they have border! So you can see them
  64.         TCAS_BUF = []
  65.         PIX = surface.get_pix()
  66.         surface_clear(ctx)
  67.         PIX = PixStrip(PIX)
  68.         tcas_main(TCAS_BUF, PIX, start[i] * 10, end[i] * 10, 0, 0, 0)
  69.         WriteTcasFile(TCAS_FILE, TCAS_BUF)     # write the buffer in memory to the file
  70.         progress(i + 1, lineNum)
  71.     FinFont(Font)
  72.     FinTcasFile(TCAS_FILE)
复制代码

tcas_OverPy.rar

3.55 KB, 下载次数: 3129

您需要登录后才可以回帖 登录 | 新人加入

GitHub|TCAX 主页

GMT+8, 2024-12-9 06:56

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部
RealH