Overview# I had the opportunity to create a transparent text PDF from a PDF using Google Cloud Vision API, so this is a personal note for future reference.
Below is an example of searching for simple.
Background# This time, we target PDFs consisting of a single page.
Procedure# Creating the Image# Create an image to be used as the OCR target.
With the default settings, the resulting image was blurry, so I set the resolution to 2x and performed position alignment considering the resolution in the process described below.
Install the following packages.
P P y i M l u l P o D w F
i f i f i # i o # p p # # z # m p i i m r m r m n u d a o a i m m p o p o p 入 p t 入 f g ペ p o 解 t x g g o m o m o 力 u p 力 _ e ー i m 像 . r r r P t u P d ジ x 度 = = = s t P t t t D _ t D o = を = を a I q F p _ F c 画 = 上 f p I v f L j d i フ d p フ u p 像 2 げ i a m e i s m o ァ f d ァ m d と p . る t g a ( t i o イ _ f イ e f し a 0 た z e g " z m n i ル p _ ル n _ て g め . . e . p m と a p を t d レ e に M g . o p 出 t a 開 o ン . ズ a e i # r o 力 h t き = c ダ g ー t t p m t r P h 、 u リ e ム r _ e a P t D = 単 f m ン t 設 i p n g y I F = 一 i e グ _ 定 x i ( e M m t フ " ペ t n し p ( x i . u a q ァ . " ー z t 、 i z m o p P g d イ / . ジ . [ O x o a . n D e m ル i / を o 0 C m o p B g F n o 読 p ] R a m ( y " p u み e で p , m t ) u t 込 n テ ( a e t p み ( # キ ) z t s . u i ス o r I p t n 最 ト o i O d . p 初 を # m x ( f p u の 抽 ) = p " d t ペ 出 解 m i f _ ー 像 a x " p ジ 度 t . # d を 3 ) t f 選 0 o 単 _ 択 0 b 一 p y ペ a D t ー t P e ジ h I s の ) で ( P レ " D ン p F ダ n フ リ g ァ ン " イ グ ) ル ) ) Google Cloud Vision API# Apply the Google Cloud Vision API to the output image.
{ " t e x t { } { } { } A , , , n n o " } " " " } " " } " t b , d l b , d b , d a o e o o e o e t u s c u s u s i n " ] c a n " ] c n " ] c o d v r l d v r d v r n i e i e i e i i e i s n r p " n r p n r p " g t { } { } { } { } t : g t { } { } { } { } t g t { } { } { } { } t : P i , , , i P i , , , i P i , , , i o c o " o c o o c o [ l e n l l e n l e n y s " " " " " " " " " a y s " " " " " " " " " y s " " " " " " " " " " " x y x y x y x y : " " " x y x y x y x y : " " x y x y x y x y : : : " " " " " " " " : : " " " " " " " " : : " " " " " " " " : : : : : : : : " : : : : : : : : " : : : : : : : : " { [ S { [ S { [ P 1 1 1 1 1 1 1 1 a 1 1 3 1 3 2 1 2 a 3 1 5 1 5 2 3 2 D 4 5 0 5 0 4 4 4 m 4 5 6 5 6 1 4 1 m 8 5 0 5 0 1 8 1 F 1 2 8 2 8 1 1 1 p 1 9 3 6 4 6 2 9 p 2 6 6 4 7 3 3 5 " , 2 2 0 , 0 l , , , , l , , , , , , e e " P D F . . . " ,
Save the output JSON file with a name such as ./google_ocr.json.
Then, retrieve the OCR results as follows.
j # w t s i e o J t x n S h t _ O r s p N o e a フ p s = t ァ e p h イ n o r ル ( n e = か j s s ら s e p " O o o . C n = n / R _ s g テ p j e o キ a s [ o ス t o " g ト h n t l デ , . e e ー l x _ タ " o t o を r a A c 読 " d n r み ) ( n . 込 f o j む a ) t s s a o t n f i " : o n s " ]
Creating the Transparent Text# Apply the results to the PDF using the following script. The key point is that it was necessary to “adjust the font size to check if it fits.”
# r # i # p p e f d d ペ c O 変 f f ー t C t 更 _ _ ジ R e f し d d サ = テ x o た o o イ キ t r P c c ズ p ス s D u u を a ト : t v x y x y x y x y # b c # f f # w i F m m 取 g を e e _ _ _ _ _ _ _ _ b o o i h f を e e 得 e 透 x r m m m m m m m m バ o n 初 n t フ i 保 n n . 明 t t i i a a i i a a ウ x t 期 t s ォ l n 存 t t r テ i n n x x n n x x ン _ e フ s ン e r i f o p . . e キ i c デ r n ォ i = ト e f o t r s c c ス n e = = = = = = = = ィ e t ン z サ f s n i a l t ト s ン c ト e F イ o r t f n v o と t m m m m x y x y グ t = サ a ズ n = e f b s i t e s し q = i i a a _ _ _ _ ボ イ = l を t s i r i t ( ( e て d n n x x m m m m ッ = t ズ s 調 s p t e z s f o ( 追 m t ( ( ( ( i i a a ク e 1 e 整 i a > s a e : " u ) 加 ( e [ [ [ [ n n x x ス f x 0 し z g = k ' t t x v v v v を i t て e e = - { p e t [ [ [ [ / / / / 定 t [ 収 . 0 = c u x [ " " " " 義 z " ま > i : T o t t " x y x y z z z z . d る n r 1 n _ s b " " " " o o o o R e か 0 s u t p [ o ] ] ] ] o o o o e s 確 : e e e d 1 u m m m m c c 認 r # n f : n f f f f t r t t _ ] d o o o o ( i _ フ } p ) i r r r r x p t ォ ' a : n _ t e ン t g v v v v m i x ト c h P i o t サ o ) # o i i i i n n b イ u l n n n n , " o ズ l t y ] x を d e " v v v v y ( 縮 x ] e e e e _ b 小 n t [ r r r r m b o s " t t t t i o t [ v i i i i n x 0 e c c c c , _ f ] r e e e e r i は t s s s s x e t ペ i _ c ー c i i i i m t i ジ e f f f f a , n 全 s x 体 " v v v v , c t の ] ] ] ] ] o h テ ) ) ) ) y n e キ _ t ス m e r ト a n e な x t c の ) , t で a ス f n キ o g ッ n l プ t e s . i " z ) e = f o n t s i z e , c o l o r = ( 0 , 0 , 0 , 0 ) , r e n d e r _ m o d e = 3 , a l i g n = 1 )
Results# We target the PDF available at the following link.
https://pdfobject.com/pdf/sample.pdf
As a result, we were able to create a transparent text PDF as shown below.
Summary# I hope this article serves as a useful reference when OCR is needed for specific pages only.