[PYTHON] Convert PDF files to PNG files with GIMP

Information provided by ministries and agencies related to COVID-19 has many pdf files, and when sharing information, it is difficult to manually convert it to an image in order to visualize a large number of pages, so I created a plug-in.

Share the information below.

    1. About GIMP

GIMP is a free and open source image editing software. https://www.gimp.org/

  1. How to convert PDF to PNG with GIMP

〇 When manually converting from pdf to png (1) Start GIMP. image.png

(2) Drag and drop the pdf file to display the dialog. Click [Import]. (You can also open it by selecting [File]-[Open / Import]) image.png

The pdf will be read as an image. Each page is loaded as a layer. image.png

(3) Export with the extension png from [File]-[Export As ...]. Only the image at the top of the layer is exported here. Repeat this each time, hiding the layer.


〇 When automatically converting from pdf to png (1) First, check if the python plugin is available. https://docs.gimp.org/2.10/ja/gimp-filters-python-fu.html This is also for reference. https://www.ibm.com/developerworks/jp/opensource/library/os-autogimp/index.html

You can use it if you have [Python-Fu] in [Filter]. If not, please do your best. (I'm sorry. Maybe something to do at the time of installation. I was on my own.)

(2) Write the code and save it in the plugin folder.

export_pdf2png.py


#!/usr/bin/env python
# -*- coding: utf-8 -*-
#

from gimpfu import *
import os

def export_pdf2png(img, drw, path):
    img = img.duplicate()
    for layer in img.layers:
        layer.visible = False
    
    for idx, layer in enumerate(img.layers):
        layer.visible = True
        
        filename = '{0}{1}.png'.format('PDF2PNG', idx)
        fullpath = os.path.join(path, filename)

        layer_img = img.duplicate()
        layer_img.flatten()
        
        pdb.file_png_save(img, img.layers[idx], fullpath, filename, 0, 9, 0, 0, 0, 0, 0)

register(
    "python-fu-export-pdf2png",
    "Export PDF to PNG",
    "Exports pdf to png file",
    "J",
    "",
    "",
    "Convert from PDF to PNG...",
    "*",
    [
        (PF_IMAGE, "img", "Input image", None),
        (PF_DRAWABLE, "drw", "Input drawable", None),
        (PF_DIRNAME, "path", "Output directory", os.getcwd())
    ],
    [],
    export_pdf2png,
    menu="<Image>/File/"
    )

main()

The plugin folder is set to [Edit]-[Settings]-[Folder]-[Plugins]. image.png

(3) Restart GIMP and check that it is registered in the [File] menu. image.png

Import the pdf file with GIMP as you would manually.

Select [Convert PDF to PNG ...] from the [File] menu, specify the output destination folder, and execute. image.png

Pages are output like this. image.png

This is the output image (1st page). image.png

I think that it may not work in the environment etc., so I'm sorry in that case. I hope it will be helpful for those in need.

Note: When you start the procedure browser from Help, the procedure information is displayed. image.png

Recommended Posts

Convert PDF files to PNG files with GIMP
Convert HEIC files to PNG files with Python
Convert PDF to image (JPEG / PNG) with Python
Convert PDF to image with ImageMagick
Convert from PDF to CSV with pdfplumber
Convert files written in python etc. to pdf with syntax highlighting
Convert 202003 to 2020-03 with pandas
Convert DICOM to PNG with Ascending and Descending
Convert svg file to png / ico with Python
How to convert SVG to PDF and PNG [Python]
Convert multiple jpg files to one PDF file
Batch convert PSD files in directory to PDF
Beginners try to convert Word files to PDF at once
Create an API to convert PDF files to TIF images with FastAPI and Docker
Convert the image in .zip to PDF with Python
How to convert Web pages to PDF, PNG, JPG with VBA (Excel) (Selenium Basic)
Integrate PDF files with Python
Batch convert image files uploaded to MS Forms / Google Forms to PDF
Convert garbled scanned images to PDF with Pillow and PyPDF
Convert .ipynb to .html (with BatchFile)
Convert PDF to Documents by OCR
Convert markdown to PDF in Python
Convert A4 PDF to A3 every 2 pages
Convert list to DataFrame with python
Convert from pdf to txt 2 [pyocr]
Made it possible to convert PNG to JPG with Pillow of Python
Convert a large number of PDF files to text files using pdfminer
For the time being, I want to convert files with ffmpeg !!
Convert memo at once with Python 2to3
Convert psd file to png in Python
Convert character strings to features with RoBERTa
Convert Excel data to JSON with python
[MNIST] Convert data to PNG for keras
Convert Hiragana to Romaji with Python (Beta)
Convert PDF attached to email to text format
Convert array (struct) to json with golang
Convert Chinese numerals to Arabic numerals with Python
Sample to convert image to Wavelet with Python
[HOW TO] Easy way to debug when generating PDF files with PHP etc.
Library comparison summary to generate PDF with Python
Convert FBX files to ASCII <-> BINARY in Python
Convert data with shape (number of data, 1) to (number of data,) with numpy.
Convert to HSV
Convert PDFs to images in bulk with Python
Convert mp4 to mp3 with ffmpeg (thumbnail embedded version)
[Python] Convert from DICOM to PNG or CSV
Convert Windows epoch values to date with python
Easily convert Jupyter Notebooks to blogs with fastpages
Convert UTF-8 CSV files to read in Excel
Upload files to Google Drive with Lambda (Python)
How to convert (32,32,3) to 4D tensor (1,32,32,1) with ndarray type
[Small story] Easy way to convert Jupyter to PDF
Convert multiple proto files at once with python
Convert strings to character-by-character list format with python
Convert jupyter notebook .ipynb files to python executable .py files
I want to convert an image to WebP with lollipop
Images created with matplotlib shift from dvi to pdf
Procedure to load MNIST with python and output to png
0 Convert unfilled date to datetime type with regular expression
Convert a text file with hexadecimal values to a binary file
How to convert horizontally held data to vertically held data with pandas