使用Pillow和libraqm渲染高棉文字时进行裁剪

时间:2018-07-03 12:07:44

标签: python python-imaging-library pillow freetype2 harfbuzz

我正在使用Python库Pillow将高棉文字绘制到图像上。我正在使用libraqm,它将FreeType和HarfBuzz包装起来以提供复杂的布局支持(def pow2db_tf(X): amin=1e-10 top_db=80.0 ref_value = 1.0 log10 = 2.302585092994046 log_spec = (10.0/log10) * tf.log(tf.maximum(amin, X)) log_spec -= (10.0/log10) * tf.log(tf.maximum(amin, ref_value)) pow2db = tf.maximum(log_spec, tf.reduce_max(log_spec) - top_db) return pow2db def librosa_feature_like_tf(x, sr=16000, n_fft=2048, n_mfcc=20): mel_basis = librosa.filters.mel(sr, n_fft).astype(np.float32) mel_basis = mel_basis.reshape(1, int(n_fft/2+1), -1) tf_stft = tf.contrib.signal.stft(x, frame_length=n_fft, frame_step=hop_length, fft_length=n_fft) print ("tf_stft", tf_stft.shape) tf_S = tf.matmul(tf.abs(tf_stft), mel_basis); print ("tf_S", tf_S.shape) tfdct = tf.spectral.dct(pow2db_tf(tf_S), norm='ortho'); print ("tfdct", tfdct.shape) print ("tfdct before cut", tfdct.shape) tfdct = tfdct[:,:,:n_mfcc]; print ("tfdct afer cut", tfdct.shape) #tfdct = tf.transpose(tfdct,[0,2,1]);print ("tfdct afer traspose", tfdct.shape) return tfdct x = tf.placeholder(tf.float32, shape=[None, 16000], name ='x') tf_feature = librosa_feature_like_tf(x) print("tf_feature", tf_feature.shape) mfcc_rosa = librosa.feature.mfcc(wav, sr).T print("mfcc_rosa", mfcc_rosa.shape) )。

渲染时,某些文本会右移,因此会被边界框剪切。比较下面的第一个渲染(使用Pillow)和第二个渲染(在MS Word中)。我用红色显示了边界框。

Khmer rendered by MS Word

JavaRoutingDsl

用于重现剪裁效果的代码:

layout_engine=ImageFont.LAYOUT_RAQM

我在Ubuntu 14.04.3 LTS上使用以下版本:

  • Python 3.4.3
  • 枕头5.3.0.dev0
  • libfreetype6:2.5.2-1ubuntu2.8
  • libharfbuzz0b:0.9.27-1ubuntu1.1

0 个答案:

没有答案