Package: preview-generator-ivc
Presentationpreview-generator is a library for generating preview - thumbnails, pdf, text and json overview
for all your file-based content. This module gives you access to jpeg, pdf, text, html and json
preview of virtually any kind of file. It also includes a cache mechanism so you do not have to
care about preview storage.By creating this module, the goal was to delegate the responsibility of building preview
of files managed bytracim.Supported file formatsHere is an overview of supported file formats:MIME typeExtensionImages - based on WAND (image magick)application/postscript.psimage/x-jg.artimage/x-ms-bmp.bmptext/plain.kshimage/x-canon-cr2.cr2image/x-canon-crw.crwapplication/dicom.dcmapplication/x-director.dcrimage/x-epson-erf.erfimage/gif.giftext/x-chdr.htext/html.htmimage/vnd.microsoft.icon.icoapplication/x-info.infoimage/x-jng.jngimage/jp2.jp2image/jpeg.jpegimage/jpm.jpmapplication/json.jsonchemical/x-mopac-input.mopimage/x-nikon-nef.nefimage/x-olympus-orf.orfapplication/font-sfnt.otfimage/x-portable-bitmap.pbmimage/pcx.pcxchemical/x-pdb.pdbapplication/pdf.pdfapplication/x-font.pfaimage/x-portable-graymap.pgmimage/png.pngimage/x-portable-anymap.pnmimage/x-portable-pixmap.ppmimage/x-photoshop.psdimage/x-cmu-raster.rasimage/x-rgb.rgbapplication/x-silverlight.scrtext/scriptlet.sctimage/tiff.tiffapplication/vnd.visio.vsdimage/vnd.wap.wbmp.wbmpimage/x-xbitmap.xbmapplication/x-xcf.xcfimage/x-xpixmap.xpmimage/x-xwindowdump.xwdBitmap images - based on Pillowimage/png.pngapplication/postscript.psimage/x-epsImages - based on convert command (Image magick)application/postscript.psimage/x-jg.artimage/x-ms-bmp.bmptext/plain.kshimage/x-canon-cr2.cr2image/x-canon-crw.crwapplication/dicom.dcmapplication/x-director.dcrimage/x-epson-erf.erfimage/gif.giftext/x-chdr.htext/html.htmimage/vnd.microsoft.icon.icoapplication/x-info.infoimage/x-jng.jngimage/jp2.jp2image/jpeg.jpegimage/jpm.jpmapplication/json.jsonchemical/x-mopac-input.mopimage/x-nikon-nef.nefimage/x-olympus-orf.orfapplication/font-sfnt.otfimage/x-portable-bitmap.pbmimage/pcx.pcxchemical/x-pdb.pdbapplication/pdf.pdfapplication/x-font.pfaimage/x-portable-graymap.pgmimage/png.pngimage/x-portable-anymap.pnmimage/x-portable-pixmap.ppmimage/x-photoshop.psdimage/x-cmu-raster.rasimage/x-rgb.rgbapplication/x-silverlight.scrtext/scriptlet.sctimage/tiff.tiffapplication/vnd.visio.vsdimage/vnd.wap.wbmp.wbmpimage/x-xbitmap.xbmapplication/x-xcf.xcfimage/x-xpixmap.xpmimage/x-xwindowdump.xwdArchive filesapplication/x-compressedapplication/x-zip-compressedapplication/zip.zipmultipart/x-zipapplication/x-tar.tarapplication/x-gzipapplication/x-gtar.gtarapplication/x-tgzVector images - based on Inkscapeimage/svg+xml.svgDocuments - based on LibreOfficeimage/wmfapplication/x-hwp.hwpapplication/x-aportisdocapplication/vnd.sun.xml.chartapplication/vnd.ms-excel.sheet.binary.macroEnabled.12application/docbook+xmlapplication/vnd.sun.xml.writer.global.sxgimage/x-xpixmap.xpmapplication/x-gnumeric.gnumericapplication/vnd.apple.pagesimage/x-emfapplication/vnd.stardivision.calc.sdctext/spreadsheetapplication/mathml+xmlimage/x-sgfapplication/x-sony-bbebimage/x-portable-graymap.pgmapplication/wps-office.docapplication/x-starwriterapplication/vnd.oasis.opendocument.spreadsheet.odsapplication/clarisworksapplication/vnd.sun.xml.impress.sxiapplication/x-iwork-numbers-sffnumbersapplication/vnd.ms-powerpoint.slide.macroEnabled.12application/vnd.oasis.opendocument.text-master.odmapplication/vnd.sun.xml.writer.template.stwapplication/x-iwork-pages-sffpagesapplication/x-iwork-keynote-sffkeyapplication/vnd.oasis.opendocument.graphics-flat-xml.fodgapplication/vnd.openxmlformats-officedocument.presentationml.slideshow.ppsxapplication/x-abiword.abwimage/x-targaapplication/xhtml+xml.xhtmlapplication/vnd.ms-excel.xlsimage/x-photo-cdapplication/vnd.stardivision.draw.sdaimage/x-portable-bitmap.pbmapplication/visio.drawingapplication/vnd.oasis.opendocument.graphics.odgimage/vnd.adobe.photoshopapplication/vnd.sun.xml.calc.template.stcapplication/vnd.lotus-1-2-3application/vnd.sun.xml.writer.webapplication/vnd.oasis.opendocument.databaseimage/cgmapplication/vnd.sun.xml.math.sxmapplication/vnd.openxmlformats-officedocument.presentationml.template.potxapplication/rtf.rtfapplication/vnd.apple.keynoteimage/x-wpgapplication/vnd.ms-excel.template.macroEnabled.12application/x-pagemakerapplication/vnd.ms-powerpoint.pptapplication/x-mspublisherapplication/vnd.visio.vsdapplication/vnd.oasis.opendocument.presentation.odpapplication/vnd.sun.xml.writer.sxwapplication/wps-office.pptapplication/vnd.sun.xml.calc.sxcimage/x-pictapplication/vnd.sun.xml.impress.template.stiapplication/wps-office.pptximage/x-sun-rasterimage/x-freehandapplication/prs.pluckerapplication/x-pocket-wordtext/csv.csvapplication/vnd.openxmlformats-officedocument.presentationml.presentation.pptximage/x-wmfapplication/vnd.sun.xml.draw.sxdapplication/vnd.oasis.opendocument.presentation-flat-xml.fodptext/html.htmapplication/vnd.oasis.opendocument.graphics-template.otgapplication/vnd.oasis.opendocument.spreadsheet-flat-xml.fodsapplication/vnd.corel-drawapplication/x-qproapplication/vnd.ms-excel.sheet.macroEnabled.12application/vnd.visio.xmlimage/x-pcximage/x-svmapplication/vnd.ms-word.template.macroEnabled.12application/vnd.oasis.opendocument.chart.odcapplication/x-fictionbook+xmlapplication/msword.dotapplication/vnd.oasis.opendocument.text.odtapplication/vnd.ms-worksimage/vnd.dxfapplication/vnd.oasis.opendocument.text-web.othapplication/vnd.openxmlformats-officedocument.wordprocessingml.template.dotximage/x-epsapplication/vnd.stardivision.writer.sdwtext/rtfapplication/vnd.oasis.opendocument.formula.odfapplication/x-stardrawapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet.xlsxapplication/vnd.ms-powerpoint.presentation.macroEnabled.12application/wordperfect5.1image/emfapplication/x-starcalcapplication/vnd.oasis.opendocument.text-master-template.otmapplication/vnd.oasis.opendocument.text-template.ottapplication/vnd.palmapplication/vnd.oasis.opendocument.baseapplication/wps-office.docxapplication/x-t602application/vnd.openxmlformats-officedocument.wordprocessingml.document.docximage/x-xbitmap.xbmapplication/vnd.oasis.opendocument.formula-templateapplication/vnd.oasis.opendocument.presentation-template.otpapplication/vnd.oasis.opendocument.chart-templateimage/x-metapplication/macwriteiiapplication/x-dbaseimage/tiff.tiffapplication/vnd.oasis.opendocument.spreadsheet-template.otsapplication/vnd.sun.xml.draw.template.stdapplication/wps-office.xlsapplication/vnd.wordperfect.wpdapplication/vnd.ms-powerpoint.slideshow.macroEnabled.12application/vnd.openxmlformats-officedocument.spreadsheetml.template.xltxapplication/vnd.openxmlformats-officedocument.presentationml.slide.sldximage/x-portable-pixmap.ppmapplication/vnd.visio2013image/x-cmxapplication/vnd.sun.xml.base.odbapplication/wps-office.xlsxapplication/vnd.oasis.opendocument.text-flat-xml.fodtimage/x-cmu-raster.rasapplication/vnd.apple.numbersapplication/vnd.ms-powerpoint.template.macroEnabled.12image/tifapplication/vnd.lotus-wordproapplication/vnd.ms-word.document.macroEnabled.12Plain text filestext/plain.kshtext/html.htmapplication/xml.xslapplication/javascript.jsPDF documents - based on PyPDF2application/pdf.pdfInstallationDependencies:apt-getinstallzlib1g-devlibjpeg-devpython3-pythonmagickinkscape xvfbpoppler-utilslibfile-mimeinfo-perlqpdfAt the moment there are issues with the exiftool package on debian, so you’ll need to install it manually:#Exiftoolwget https://sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.11.tar.gz
gzip -dc Image-ExifTool-11.11.tar.gz | tar -xf -
cd Image-ExifTool-11.11
perl Makefile.PL
sudo make installAfter installing dependencies, you can install preview-generator usingpip:pip install preview-generatorOptional dependencies:To handle previews for office documents you will needLibreOffice, if you don’t have it already:apt-get install libreofficeTo check dependencies, you can run:preview --check-dependenciesUsageHere are some examples of codeBasic UsageMost basic usage, create a jpeg from a png, default size 256x256frompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'file_to_preview_path='/tmp/an_image.png'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(file_to_preview_path)Preview an image with a specific sizeYou can choose the size of your image using params width and height.frompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'file_to_preview_path='/tmp/an_image.png'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(file_to_preview_path,width=1000,height=500)Preview a pdf or an office document as a jpegfrompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'pdf_or_odt_to_preview_path='/tmp/a_pdf.pdf'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(pdf_or_odt_to_preview_path)By default it will generate the preview of the first page of the document.
Using paramspage, you can you pick the page you want to preview.page number starts at 0, if you want to preview the second page of your document then the argument will be 1 `page=1`frompreview_generator.managerimportPreviewManagercache_path='/tmp/preview_cache'pdf_or_odt_to_preview_path='/tmp/a_pdf.pdf'manager=PreviewManager(cache_path,create_folder=True)path_to_preview_image=manager.get_jpeg_preview(pdf_or_odt_to_preview_path,page=1)Generate a pdf preview of a libreoffice text documentfrompreview_generator.managerimportPreviewManagermanager=PreviewManager('/tmp/cache/',create_folder=True)pdf_file_path=manager.get_pdf_preview('/home/user/Documents/report.odt',page=2)print('Preview created at path : ',thumbnail_file_path)For Office types into PDF :cache_path='/tmp/previews'preview_manager=PreviewManager(cache_path,create_folder=True)path_to_preview=preview_manager.get_pdf_preview(file_path,page=page_id)-> Will create a preview from an office file into a pdf fileargs :file_path : the String of the path where is the file you want to get the previewpage : the int of the page you want to get. If not mentioned all the pages will be returned. First page is page 0returns :str: path to the preview fileFor images(GIF, BMP, PNG, JPEG, PDF) into jpeg :cache_path='/tmp/previews'preview_manager=PreviewManager(cache_path,create_folder=True)path_to_preview=preview_manager.get_jpeg_preview(file_path,height=1024,width=526)-> Will create a preview from an image file into a jpeg file of size 1024 * 526args :file_path : the String of the path where is the file you want to get the previewheight : height of the preview in pixelswidth : width of the preview in pixels. If not mentioned, width will be the same as heightreturns :str: path to the preview fileOther conversions :The principle is the same as aboveZip to text or html :will build a list of files into texte/html inside the jsonOffice to jpeg :will build the pdf out of the office file and then build the jpeg.Text to text :mainly just a copy stored in the cacheCommand LineFor test purposes, you can usepreviewfrom the command line,
giving the file to preview as a parameter:preview demo.pdfOr multiple files:preview *.pdfCache mechanismNaming :The name of the preview generated in the cache directory will be :{file_name}-[{size}-]{file_md5sum}[({page})]{extension}file_name = the name of the file you asked for a preview without the extension.size = the size you asked for the preview. In case of a Jpeg preview.file_md5sum = the md5sum of the entire path of the file. To avoid conflicts like files that have the same name but are in different directory.page = the page asked in case of pdf or office document preview.extensions = the extension of the preview (.jpeg for a jpeg, .txt for a text, etc)Example :These scripts :GIF to JPEG :importosfrompreview_generator.managerimportPreviewManagercurrent_dir=os.path.dirname(os.path.abspath(__file__))+'/'manager=PreviewManager(path=current_dir+'cache')path_to_preview=manager.get_jpeg_preview(file_path=current_dir+'the_gif.gif',height=512,width=512,)print('Preview created at path : ',path_to_preview)will printPreview created at path : the_gif-512x512-60dc9ef46936cc4fff2fe60bb07d4260.jpegODT to JPEG :importosfrompreview_generator.managerimportPreviewManagercurrent_dir=os.path.dirname(os.path.abspath(__file__))+'/'manager=PreviewManager(path=current_dir+'cache')path_to_file=manager.get_jpeg_preview(file_path=current_dir+'the_odt.odt',page=1,height=1024,width=1024,)print('Preview created at path : ',path_to_preview)will printPreview created at path : the_odt-1024x1024-c8b37debbc45fa96466e5e1382f6bd2e-page1.jpegZIP to Text :importosfrompreview_generator.managerimportPreviewManagercurrent_dir=os.path.dirname(os.path.abspath(__file__))+'/'manager=PreviewManager(path=current_dir+'cache')path_to_file=manager.get_text_preview(file_path=current_dir+'the_zip.zip',)print('Preview created at path : ',path_to_file)will printPreview created at path : the_zip-a733739af8006558720be26c4dc5569a.txtAdding new feature :Before all, I’d be glad if you could share your new feature with everybody. So if you want to, you can fork it on github (https://github.com/algoo/preview-generator) (seeDeveloper’s Kit) and submit new features.If you want to add a new preview builder to handle documents of typefoointojpeg(for example) here is how to proceed :WarningIf you need to look at other builders to find out how to proceed, avoid looking at any of the Office to something. It is a particular case and could misslead you.Create a new class FooPreviewBuilder in a file foo_preview.py in preview_generator/previewMake him inherit from the logical PreviewBuilder classif it handles several pages it will beclass FooPreviewBuilder(PreviewBuilder)for single page it will beclass FooPreviewBuilder(OnePagePreviewBuilder)…define your ownbuild_jpeg_preview(…)(in the case we want to makefoointojpeg) based on the same principle as other build_{type}_preview(…)Inside this build_jpeg_preview(…) you will call a method file_converter.foo_to_jpeg(…)Define your foo_to_jpeg(…) method in preview_generator.preview.file_converter.pyinputs must be a stream of bytes and optional informations like a number of pages, a size, …output must also be a stream of bytesMaybe you’ll need to redefine some methods likeget_page_number()orexists_preview()in your FooPreviewBuilder classDeveloper’s KitInstallation (dev) :From scratch on a terminal :create your project directory (we will name it “the_project” but you can name it the way you want) :mkdir the_projectcd the_projectgit clone https://github.com/algoo/preview-generatorbuilding your environment :install python virtualenv builder :sudo apt install python3-venvbuild your virtual env (env will be called “myenv”, you can name it the way you want):python3 -m venv myenvif it’s not already, activate it :source myenv/bin/activate. (deactivateto deactivate)install dependencies :Exiftool - Follow instruction on the main website:https://sno.phy.queensu.ca/~phil/exiftool/apt-get install zlib1g-devapt-get install libjpeg-devapt-get install python3-pythonmagickapt-get install inkscapeapt-get install xvfbapt-get install poppler-utilsapt-get install qpdfapt-get install libfile-mimeinfo-perlpip install wandpip install Pillowpip install PyPDF2pip install python-magicpip install pyexifinfopip install packagingpip install xvfbwrapperpip install pdf2imagepip install pathlibif you use python 3.5 or lesspip install typing#generaldependenciesapt-get install zlib1g-dev libjpeg-dev python3-pythonmagick inkscape xvfb poppler-utils qpdf libfile-mimeinfo-perl
pip install wand Pillow PyPDF2 python-magic pyexifinfo packaging xvfbwrapper pdf2image pathlib#Exiftoolwget https://sno.phy.queensu.ca/~phil/exiftool/Image-ExifTool-11.11.tar.gz
gzip -dc Image-ExifTool-11.11.tar.gz | tar -xf -
cd Image-ExifTool-11.11
perl Makefile.PL
sudo make installIf you need to preview scribus.slafiles you will need scribus >= 1.5.
If it’s not available in your distribution you can use an AppImage.Download the last AppImage from the official websitehttps://www.scribus.net/downloads/unstable-branch/mv /path/to/image/scribus-x.y.appimage /usr/local/bin/scribus
chmod +x /usr/local/bin/scribusCode Convention :When using subclass of generic abstract class, convention is to prefix it with name
of the generic abstract class. For example:ImagePreviewBuilderIMConvert(ImagePreviewBuilder)Running Tests :Pytest is a motor for unit testingpip install -e .[testing]go into the “tests” directory :cd path/to/you/project/directory/testsrunpytestOthers checks :Run mypy checks:mypy –ignore-missing-imports –disallow-untyped-defs .Code formatting using black:black -l 100 preview_generator setup.py build_supported_mimetypes_table_rst.py testsSorting of import:isort tests/**/*.py preview_generator/**/*.py setup.py build_supported_mimetypes_table_rst.pyFlake8 check(unused import, variable and many other checks):flake8 preview_generator setup.py build_supported_mimetypes_table_rst.py testsContribute :install preview_generator with dev dependencies (contains tests dependencies)pip install -e ‘.[dev]install pre-commit hooks:pre-commit installLaunch test :pytestYou now can commit and see if pre-commit is ok with your change.LicenseMIT licensed.https://opensource.org/licenses/MIT