+ ¿?…jÖãóœ€Rt^RIt^RIt^RIt^RIt^RIt^RItRtRt Rt Rt Rt Rt]R 8Xd ]!4R#R# ] dRtL,i;i) aûNormalize white/black points of all raster images in a PDF. For every embedded image, a shared linear stretch maps a percentile-defined dark point to pure black and a bright point to pure white, clipping nothing in between. A single transform is applied to all color channels so mid-tone color balance is preserved (hue unchanged). The PDF structure, text, vector content, and page layout are left untouched: only the image pixel data is swapped in place. To keep memory bounded on large scanned books, the PDF is processed in three stages and never more than one page's worth of decoded pixels is held at a time: 1. split: each page is extracted to its own temporary PDF file 2. process: each page file's images are stretched and the page is saved 3. merge: all processed page files are joined into the output PDF Note: an image shared by several pages is duplicated into each page during splitting, so the output may be slightly larger than the input. Dependencies: pip install pymupdf numpy NTFcó*€VPwrEp\V^4pVRRV13,P\P4pWg8”dVRVR13,MRp V'd9V^8Xd2\P !W�R R7p \P !W‚R R7p MšV^8Xd.\P !W�4p \P !W‚4p MfRVR,,RVR,,,RVR,,,p \P !WÁ4p \P !WÂ4p \ \P!V 44p \ \P!V 44p Wº, R8dW V R3#\P!WŠ, R Wº, , ,R R 4P\P4p \P!WEV3\PR 7pWÞRRV13&V e WžRVR13&WêV R 3#)z$Return (new_array, lo, hi, changed)..N)Úaxisg‰A`åÐ"Ó?gbX9´Èâ?gÉv¾Ÿ/½?gü©ñÒMbP?Fgào@g©ÚdtypeT)éé).r).r).é) ÚshapeÚminÚastypeÚnpÚfloat32Ú percentileÚfloatÚmaxÚclipÚuint8Úempty)ÚarrÚ black_pctÚ white_pctÚ per_channelÚhÚwÚnÚn_colorÚcolorÚalphaÚloÚhiÚlumaÚoutÚresults&&&& ÚH/mnt/external4/Books/Textbooks/Geometry/Other/normalize/normalize_pdf.pyÚstretchr$(s•€à�i‰i�G€Aˆ!Ü�!�Q‹i€GØ ��X�g�X� Õ × %Ñ %¤b§j¡jÓ 1€EØ"#¤+ˆC��W‘X� Ö °4€Eç�w !”|Ü �]Š]˜5°&Ô 9ˆÜ �]Š]˜5°&Ô 9‰Ø �AŒÜ �]Š]˜5Ó ,ˆÜ �]Š]˜5Ó ,‰à�u˜V•}Õ$ u¨u°V­}Õ'<Õ<¸uÀuÈVÅ}Õ?TÕTˆÜ �]Š]˜4Ó +ˆÜ �]Š]˜4Ó +ˆä Œr�vŠv�b‹zÓ €BÜ Œr�vŠv�b‹zÓ €BØ …w�„~ؘ˜EÐ!Ð!ä �'Š'�5•: %¨2­7Õ"3Õ4°c¸5Ó A× HÑ HÌÏÉÓ R€CÜ �XŠX�q˜Q�i¤r§x¡xÔ 0€F؈3���ˆ=ÑØ ÒØ %ˆs�G‘Hˆ}ÑØ �r˜4Ð Ðócó€VP^,^8Xd\P!VRW^^4pM\P!VRV^^4p\P!VRR4p\P !VRVR^4#)aMildly suppress JPEG2000 ringing/smearing around text edges. NL-means denoise preserves edges while flattening the low-amplitude ringing that the contrast stretch would otherwise amplify, followed by a gentle unsharp that restores edge crispness lost to the source encoding. Ng@g333333ó?)rrgš™™™™™É¿)r Úcv2ÚfastNlMeansDenoisingColoredÚfastNlMeansDenoisingÚ GaussianBlurÚ addWeighted)rÚstrengthr!Úblurs&& r#Ú deartifactr.Gsp€ð ‡y�y�…|�qÔÜ×-Ò-¨c°4¸ÈQÐPRÓS‰ä×&Ò& s¨D°(¸A¸rÓBˆÜ × Ò ˜C ¨Ó -€DÜ �?Š?˜3  T¨4°Ó 3Ð3r%có€VPR,wr#V'd\PM\PpVP^,V'd^M^8„p\P!WCW P 4V4#):NrN)r ÚpymupdfÚcsGRAYÚcsRGBÚPixmapÚtobytes)rÚgrayrrÚcsÚ has_alphas&& r#Ú to_pixmapr8VsQ€Ø �9‰9�R�=�D€Aߌ�ФW§]¡]€BØ— ‘ ˜!• §T¡¨qÑ1€IÜ �>Š>˜" §K¡K£M°9Ó =Ð=r%có:€W,p^;rš\4p VPRR7EF?p V ^,p WÛ9dKV PV 4\P!W 4pVP fKMVP \P 8Xd&\P!\PV4pVP \P8Hp\P!VP\PR7PVPVPVP 4P#4p\%VW#V4wppppV'd\&'d \)VV4pT'g@T ^, p \-RT RTP R TP R TR R TR R 2 RR7EK”\/TT4p\0P2P5TRT R24pTP7TTR7TP9T TR7\0P:!T4T ^, p \-RT RTP R TP RTR R TR RT R2 RR7EKB Wš3# \*d!p\-RT RT R2RR7Rp?EKmRp?ii;i)zVStretch every image on one page of a single-page doc. Returns (n_changed, n_skipped).T)ÚfullNrz xref z: z ; skipped©ÚflushÚxz already full-range (z.0fz..z ); skippedÚimg_z.jpg)Ú jpg_quality)Úfilenamez stretched z -> 0..255 (JPEG qÚ))ÚsetÚ get_imagesÚaddr0r3Ú colorspaceÚcsCMYKr2r1r Ú frombufferÚsamplesrÚreshapeÚheightÚwidthrÚcopyr$ÚHAVE_CV2r.Ú ExceptionÚprintr8ÚosÚpathÚjoinÚsaveÚ replace_imageÚremove)ÚdocÚ page_indexrrrr?Údeartifact_strengthÚtmpdirÚpageÚ n_changedÚ n_skippedÚseenÚimgÚxrefÚpixr5rÚnewrrÚchangedÚeÚnew_pixÚjpgs&&&&&&&& r#Ú process_pagerf]sI€ð �?€DØÐ€IÜ ‹5€DØ�‰ Dˆ×)ˆØ�1�vˆØ Œ<Ù Ø �‰�Œð Ü—.’. Ó+ˆCØ�~‰~Ò%ÙØ�~‰~¤§¡Ô/Ü—n’n¤W§]¡]°CÓ8�Ø—>‘>¤W§^¡^Ñ3ˆDÜ—-’- § ¡ ´2·8±8Ô<×DÑDÀSÇZÁZÐQT×QZÑQZÐ\_×\aÑ\aÓb×gÑgÓiˆCÜ#*¨3° ÀkÓ#RÑ ˆC��R˜ß"§x£xÜ  Ð&9Ó:�÷Ø ˜�NˆIÜ �I˜d˜V 2 c§i¡i [°°#·*±*°Ð=RÐSUÐVYÐRZÐZ\Ð]_Ð`cÐ\dÐdnÐoÐw{Õ |Ú Ü˜C Ó&ˆÜ�g‰g�l‰l˜6 T¨$¨¨tÐ#4Ó5ˆØ� ‰ �S kˆ Ô2Ø ×ј4¨#ÐÔ.Ü � Š �#ŒØ�Q�ˆ Ü � ˜$˜˜r #§)¡) ¨A¨c¯j©j¨\¸ÀRÈÀHÈBÈrÐRUÈhÐVhÐitÐhuÐuvÐwð@D÷ Eð Eñ;*ð< Ð Ðøôô Ü �I˜d˜V 2 a S¨ Ð2¸$Õ ?Þ ûð ús%Á$I/Á5C3I/Å) I/É/ JÉ:JÊJc ó†€\P!\R7pVPRRR7VPRRRR7VPR\RR R 7VPR \R R R 7VPRRRR7VPR\ ^URR 7VPR\ ^RRR7VPRRRR7VP 4pVP'd\'g\R4R#\P!VP4pVPp\RV R2R R!7V^8Xd\R"4R#VP;'g+VPPR#^4^,R$,p\ P"!R%VP$;'gRR&7p^;rgT;_uu_4\'V4EFp\P!4p V P)W(VR'7V^,^8Xd\R(V^, R)V R*2R R!7\+V ^VP,VP.VP0VP2VPVP44wr«Wj, pW{, pV P7\8P:P=VP4R+VR, R-24^R R.7V P?4EK \R/R R!7\P!4p \'V4Fhp\P!\8P:P=VP4R+VR, R-244p V P)V 4V P?4Kj V P7V^R R.7V P?4RRR4VP?4V^8Xd\R0V R124\R2V R3V R4V R52R R!7R# T P?4i;i T P?4i;i T P?4i;i +'giLŽ;i)6)Ú descriptionÚpdfz input PDF)Úhelpz-oz--outputz,output PDF (default: -normalized.pdf)z --black-pctgð?z=lower brightness percentile used as black point (default 1.0))ÚtypeÚdefaultrjz --white-pctgÀX@z>upper brightness percentile used as white point (default 99.0)z --per-channelÚ store_truezTstretch each RGB channel independently (default: shared map preserves color balance))Úactionrjz --jpg-qualityzEJPEG quality for re-encoded images (default 85; lower = smaller file)z --deartifactÚHz{reduce JPEG2000 ringing/smearing: NL-means denoise with strength H (try 3-5; adds ~1.5 s per image, requires opencv-python))rkrlÚmetavarrjz --keep-tempÚDIRzAkeep the per-page temporary files in DIR instead of deleting them)rprjzH--deartifact requires opencv-python (pip install opencv-python-headless)NzPDF: z pagesTr;zEmpty PDF; nothing to do.Ú.z-normalized.pdfz normalize-)ÚprefixÚdir)Ú from_pageÚto_pagez page Ú/z ...Úpage_Ú05dz.pdf)ÚgarbageÚdeflatezMerging pages ...zNo images needed adjustment (z already full-range).zSaved: z (z images replaced, z skipped)) ÚargparseÚArgumentParserÚ__doc__Ú add_argumentrÚintÚ parse_argsr.rMrOr0ÚopenriÚ page_countÚoutputÚrsplitÚtempfileÚTemporaryDirectoryÚ keep_tempÚrangeÚ insert_pdfrfrrrr?ÚnamerSrPrQrRÚclose) ÚapÚargsÚsrcÚn_pagesr!Útmpr[r\ÚiÚpage_docÚcÚsÚmergeds r#Úmainr—…s„€Ü × Ò ¬WÔ 5€B؇O�O�E  €OÔ,؇O�O�D˜*Ð+Y€OÔZ؇O�O�M¬°sØXðôZà‡O�O�M¬°tØYðô[à‡O�O�O¨LØoðôqà‡O�O�O¬#°rØ`ðôbà‡O�O�N¬°aÀðTðôUð‡O�O�M¨5Ø\ðô^à �=‰=‹?€Dà ‡‡€Ÿx›xÜ ÐXÔYÙä �,Š,�t—x‘xÓ €CØ�n‰n€GÜ ˆE�'�˜&Ð !¨Õ.Ø�!„|Ü Ð)Ô*Ùà �+‰+× GÐ G˜Ÿ™Ÿ™¨¨aÓ0°Õ3Ð6GÕG€CÜ × %Ò %¨\Ø*.¯.©.×*@Ð*@¸Dô B€CðЀIß Šä�w—ˆAÜ—|’|“~ˆHð !Ø×#Ñ# C¸aÐ#Ô@Ø�r•6˜Q”;ܘG A a¥C 5¨¨'¨°$Ð7¸tÕDÜ# H¨a°·±ÀÇÁÐQU×QaÑQaØ$(×$4Ñ$4°d·o±oÀsÇxÁxóQ‘�à•� Ø•� Ø— ‘ œbŸg™gŸl™l¨3¯8©8°u¸Q¸s¸GÀ4Ð5HÓIÐSTÐ^b� Ôcà—‘× ñ ô Ð!¨Õ.Ü—’“ˆð ܘ7–^�Ü"Ÿ<š<¬¯©¯ © °S·X±XÀÀqÈÀgÈTÐ?RÓ(SÓT�ð%Ø×%Ñ% hÔ/à—N‘NÖ$ñ $ð �K‰K˜ Q°ˆKÔ 5à �L‰L�N÷9 ð<‡I�I„KØ�A„~Ü Ð-¨i¨[Ð8MÐNÔOÜ ˆG�C�5˜˜9˜+Ð%7¸ °{À)Ð LÐTX×Yøð'—‘Õ ûð—N‘NÕ$ûð �L‰L�Nú÷9 �úsVÇ%P0Ç9CO1Ë7P0ÌAPÍ PÍ1(PÎP0Ï1P ÐP0ÐP ÐPÐP-Ð-P0Ð0 Q Ú__main__)r~r|rPr†Únumpyr r0r'rMÚ ImportErrorr$r.r8rfr—Ú__name__©r%r#Úr�spðñó0Û ÛãÛðÛØ€Hò  ò> 4ò>ò% òPDZðN ˆzÔÙ†FñøðQôØ‚Hðús˜?¿ A Á A