Skip to content

Commit

Permalink
convert-image-format@el-amine-404: Add PDF to image conversion (#520)
Browse files Browse the repository at this point in the history
* Implement the ability to convert PDF files to images, where each page of the PDF is turned into a separate image file.

* Closes #518
  • Loading branch information
el-amine-404 authored Sep 16, 2024
1 parent 3c2441d commit a377bb3
Show file tree
Hide file tree
Showing 14 changed files with 618 additions and 173 deletions.
11 changes: 10 additions & 1 deletion convert-image-format@el-amine-404/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
### 0.0.1

* Initial release
* Initial release

### 0.1.0

* new features:

1. PDF to Image Conversion
* Added a feature that allows converting PDFs into image formats such as PNG, JPG, and TIFF.
* Users can specify additional parameters like resolution (DPI), the range of pages to convert, and the image format.
* Default values will be applied if the user leaves fields empty.
49 changes: 33 additions & 16 deletions convert-image-format@el-amine-404/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
# Convert Image Format

Easily convert image files to various formats directly from the Nemo file manager.
Easily convert image files to various formats or turn PDF files into images directly from the Nemo file manager.

DESCRIPTION
-----------
## DESCRIPTION

This action lets you convert a single or multiple image files to the following formats (currently 15 formats and their variants):
This action allows you to:
- Convert single or multiple image files to a variety of formats (currently 15 formats and their variants).
- Convert PDF files into images where each page is converted into an image. The action will generate a folder named after the PDF file in the current directory, and each page's image will be named using the format `pg_<page_number>.<extension>`.
Example: A PDF file named document.pdf will generate a folder document, containing images pg_1.png, pg_2.png, etc.

## Supported formats

### Image Conversion

| Format | Description |
| ------------------------------------------------------------------ | --------------------------------------- |
Expand All @@ -25,26 +31,37 @@ This action lets you convert a single or multiple image files to the following f
| \*.tiff, \*.tif | Tagged Image File Format |
| *.webp | Web Picture format |

DEPENDENCIES
------------
### PDF to Image Conversion


| Format | Description |
| ------ | --------------------------------------- |
| *.png | Portable Network Graphics |
| *.jpg | JPEG (Joint Photographic Experts Group) |
| *.tiff | Tagged Image File Format |

The tool also supports custom resolution (DPI) and allows specifying the range of pages to convert from the PDF.

## DEPENDENCIES

The following program must be installed for this action to work:

| DEPENDENCY | INSTALLATION |
| ---------------------------------------------------------------------------------------- | -------------------------------------- |
| [convert](https://imagemagick.org/) (from ImageMagick) | `sudo apt-get install -y imagemagick` |
| [zenity](https://help.gnome.org/users/zenity/stable/) | `sudo apt-get install -y zenity` |
| [file](https://man7.org/linux/man-pages/man1/file.1.html) | `sudo apt-get install -y file` |
| [rsvg-convert](https://manpages.ubuntu.com/manpages/kinetic/en/man1/rsvg-convert.1.html) | `sudo apt-get install -y librsvg2-bin` |
| DEPENDENCY | INSTALLATION |
| ---------------------------------------------------------------------------------------- | --------------------------------------- |
| [convert](https://imagemagick.org/) (from ImageMagick) | `sudo apt-get install -y imagemagick` |
| [zenity](https://help.gnome.org/users/zenity/stable/) | `sudo apt-get install -y zenity` |
| [file](https://man7.org/linux/man-pages/man1/file.1.html) | `sudo apt-get install -y file` |
| [rsvg-convert](https://manpages.ubuntu.com/manpages/kinetic/en/man1/rsvg-convert.1.html) | `sudo apt-get install -y librsvg2-bin` |
| [pdftoppm](https://linux.die.net/man/1/pdftoppm) | `sudo apt-get install -y poppler-utils` |
| [xargs](https://linux.die.net/man/1/xargs) | `sudo apt-get install -y findutils` |

or download the 4 in one go:
or download the 6 in one go:

```sh
sudo apt-get update
sudo apt-get install -y imagemagick zenity file librsvg2-bin
sudo apt-get install -y imagemagick zenity file librsvg2-bin poppler-utils findutils
```

CONTRIBUTING
------------
## CONTRIBUTING

Contributions are welcome! If you have suggestions for improvements or new features, please open an issue or submit a pull request on the [project's GitHub repository](https://github.com/linuxmint/cinnamon-spices-actions).
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ _Comment=Convert %F to another format
Exec=<convert-image-format@el-amine-404/convert-image-format.sh %F>
Icon-Name=image
Selection=notnone
Mimetypes=image/*;
Mimetypes=image/*;application/pdf;
Quote=double
Dependencies=zenity;convert;file;
Dependencies=zenity;convert;file;xargs;pdftoppm;
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,52 @@

TEXTDOMAIN="convert-image-format@el-amine-404"
TEXTDOMAINDIR="${HOME}/.local/share/locale"
_TITLE=$"Convert Image(s) Format"
_PROMPT=$"Choose the format to convert the image(s) to:"
_COLUMN_1=$"Select"
_COLUMN_2=$"Format"
_COLUMN_3=$"Description"

# Images

_IMAGE__TITLE=$"Convert Image(s) Format"
_IMAGE__PROMPT=$"Choose the format to convert the image(s) to:"
_IMAGE__COLUMN_1=$"Select"
_IMAGE__COLUMN_2=$"Format"
_IMAGE__COLUMN_3=$"Description"

_PROGRESS_TITLE=$"Converting Images"
_PROGRESS_TEXT=$"Processing..."
_NOT_AN_IMAGE=$"is not an image file and will be skipped"

TITLE="$(/usr/bin/gettext "$_TITLE")"
PROMPT="$(/usr/bin/gettext "$_PROMPT")"
COLUMN_1="$(/usr/bin/gettext "$_COLUMN_1")"
COLUMN_2="$(/usr/bin/gettext "$_COLUMN_2")"
COLUMN_3="$(/usr/bin/gettext "$_COLUMN_3")"
IMAGE__TITLE="$(/usr/bin/gettext "$_IMAGE__TITLE")"
IMAGE__PROMPT="$(/usr/bin/gettext "$_IMAGE__PROMPT")"
IMAGE__COLUMN_1="$(/usr/bin/gettext "$_IMAGE__COLUMN_1")"
IMAGE__COLUMN_2="$(/usr/bin/gettext "$_IMAGE__COLUMN_2")"
IMAGE__COLUMN_3="$(/usr/bin/gettext "$_IMAGE__COLUMN_3")"

PROGRESS_TITLE="$(/usr/bin/gettext "$_PROGRESS_TITLE")"
PROGRESS_TEXT="$(/usr/bin/gettext "$_PROGRESS_TEXT")"
NOT_AN_IMAGE="$(/usr/bin/gettext "$_NOT_AN_IMAGE")"

if ! EXTENSION=$(
/usr/bin/zenity --list --radiolist \
--title="$TITLE" \
--text="$PROMPT" \
--height=320 \
--width=640 \
--column="$COLUMN_1" --column="$COLUMN_2" --column="$COLUMN_3" \
FALSE "apng" "Animated Portable Network Graphics" \
FALSE "avif" "AV1 Image File Format" \
FALSE "bmp" "Bitmap" \
FALSE "cur" "Microsoft Icon" \
FALSE "gif" "Graphics Interchange Format" \
FALSE "heic" "High Efficiency Image Coding" \
FALSE "heif" "High Efficiency Image Format" \
FALSE "ico" "Microsoft Icon" \
FALSE "j2k" "JPEG 2000 Code Stream" \
FALSE "jfi" "JPEG (Joint Photographic Experts Group)" \
FALSE "jfif" "JPEG (Joint Photographic Experts Group)" \
FALSE "jif" "JPEG (Joint Photographic Experts Group)" \
FALSE "jp2" "JPEG 2000 Image" \
FALSE "jpe" "JPEG (Joint Photographic Experts Group)" \
FALSE "jpeg" "JPEG (Joint Photographic Experts Group)" \
FALSE "jpg" "JPEG (Joint Photographic Experts Group)" \
FALSE "pjp" "JPEG (Joint Photographic Experts Group)" \
FALSE "pjpeg" "JPEG (Joint Photographic Experts Group)" \
FALSE "pdf" "Portable Document Format" \
FALSE "png" "Portable Network Graphics" \
FALSE "svg" "plaintext Scalable Vector Graphics" \
FALSE "svgz" "compressed Scalable Vector Graphics" \
FALSE "tif" "Tagged Image File Format" \
FALSE "tiff" "Tagged Image File Format" \
FALSE "webp" "Web Picture format"
); then
exit
fi
# PDF

_PDF__TITLE=$"PDF to Image Conversion"
_PDF__PROMPT=$"Enter the required details for conversion.\n\nNote: Leaving fields empty will apply default values."
_PDF__COMBO_LABEL=$"Format (default: png)"
_PDF__COMBO_VALUES="png|jpg|tiff"
_PDF__ENTRY_1=$"Resolution / DPI (default: 300)"
_PDF__ENTRY_2=$"First Page (default: 1)"
_PDF__ENTRY_3=$"Last Page (default: last page of document)"

PDF__TITLE="$(/usr/bin/gettext "$_PDF__TITLE")"
PDF__PROMPT="$(/usr/bin/gettext "$_PDF__PROMPT")"
PDF__COMBO_LABEL="$(/usr/bin/gettext "$_PDF__COMBO_LABEL")"
PDF__COMBO_VALUES="$(/usr/bin/gettext "$_PDF__COMBO_VALUES")"
PDF__ENTRY_1="$(/usr/bin/gettext "$_PDF__ENTRY_1")"
PDF__ENTRY_2="$(/usr/bin/gettext "$_PDF__ENTRY_2")"
PDF__ENTRY_3="$(/usr/bin/gettext "$_PDF__ENTRY_3")"

_NOT_A_PDF=$"is not a valid PDF file and will be skipped"
_NOT_A_VALID_FORMAT=$"Unsupported format. Possible values are: png(default), jpeg, tiff"

NOT_A_PDF="$(/usr/bin/gettext "$_NOT_A_PDF")"
NOT_A_VALID_FORMAT="$(/usr/bin/gettext "$_NOT_A_VALID_FORMAT")"

# i removed the raw formats bc as far as i know you can not convert from other formats
# to raw formats (the reverse operation is possible) right? RIGHT? 😢
Expand All @@ -68,10 +61,49 @@ fi
# FALSE "pcx" "PC Paintbrush Bitmap Image" \
# FALSE "psd" "Photoshop Document" \

# full path to the selected file
FILE="$1"
# directory where the generated image will be saved
DIRECTORY=$(dirname "$1")

convert_image() {

if ! EXTENSION=$(
/usr/bin/zenity --list --radiolist \
--title="$IMAGE__TITLE" \
--text="$IMAGE__PROMPT" \
--height=320 \
--width=640 \
--column="$IMAGE__COLUMN_1" --column="$IMAGE__COLUMN_2" --column="$IMAGE__COLUMN_3" \
FALSE "apng" "Animated Portable Network Graphics" \
FALSE "avif" "AV1 Image File Format" \
FALSE "bmp" "Bitmap" \
FALSE "cur" "Microsoft Icon" \
FALSE "gif" "Graphics Interchange Format" \
FALSE "heic" "High Efficiency Image Coding" \
FALSE "heif" "High Efficiency Image Format" \
FALSE "ico" "Microsoft Icon" \
FALSE "j2k" "JPEG 2000 Code Stream" \
FALSE "jfi" "JPEG (Joint Photographic Experts Group)" \
FALSE "jfif" "JPEG (Joint Photographic Experts Group)" \
FALSE "jif" "JPEG (Joint Photographic Experts Group)" \
FALSE "jp2" "JPEG 2000 Image" \
FALSE "jpe" "JPEG (Joint Photographic Experts Group)" \
FALSE "jpeg" "JPEG (Joint Photographic Experts Group)" \
FALSE "jpg" "JPEG (Joint Photographic Experts Group)" \
FALSE "pjp" "JPEG (Joint Photographic Experts Group)" \
FALSE "pjpeg" "JPEG (Joint Photographic Experts Group)" \
FALSE "pdf" "Portable Document Format" \
FALSE "png" "Portable Network Graphics" \
FALSE "svg" "plaintext Scalable Vector Graphics" \
FALSE "svgz" "compressed Scalable Vector Graphics" \
FALSE "tif" "Tagged Image File Format" \
FALSE "tiff" "Tagged Image File Format" \
FALSE "webp" "Web Picture format"
); then
exit
fi

local FILE="$1"
if [[ "${FILE##*.}" == "psd" ]]; then
/usr/bin/convert "${FILE}[0]" -set filename:basename "%[basename]" "${DIRECTORY}/%[filename:basename].${EXTENSION}"
Expand All @@ -84,14 +116,81 @@ convert_image() {
fi
}

convert_pdf() {

if [ -z "$1" ]; then
echo "Error: File argument is required."
return 1
fi

if ! PDF_INFO=$(
/usr/bin/zenity --forms \
--title="$PDF__TITLE" \
--text="$PDF__PROMPT" \
--add-combo="$PDF__COMBO_LABEL" --combo-values="$PDF__COMBO_VALUES" \
--add-entry="$PDF__ENTRY_1" \
--add-entry="$PDF__ENTRY_2" \
--add-entry="$PDF__ENTRY_3" \
--separator="|"
); then
exit
fi

local file="$1"

# Parse the information returned from zenity
# xargs is used to strip whitespace :)
format=$(echo "$PDF_INFO" | cut -d "|" -f1 | xargs)
resolution=$(echo "$PDF_INFO" | cut -d "|" -f2 | xargs)
firstpage=$(echo "$PDF_INFO" | cut -d "|" -f3 | xargs)
lastpage=$(echo "$PDF_INFO" | cut -d "|" -f4 | xargs)

# Set default values if arguments are not provided
format="${format:-png}"
resolution="${resolution:-300}"
firstpage="${firstpage:-1}"
# Calculate last page if not provided
if [ -z "$lastpage" ]; then
lastpage=$(pdfinfo "$file" | grep '^Pages:' | awk '{print $2}')
fi

# Create directory based on the filename
# directory where the generated image will be saved
FILE_NAME=$(basename "$file" .pdf)

[ ! -d "${DIRECTORY}/images_${FILE_NAME}" ] && mkdir -pv "${DIRECTORY}/images_${FILE_NAME}"

# Determine format options based on provided format
case "$format" in
"jpg") format_options="-jpeg" ;;
"png") format_options="-png" ;;
"tiff") format_options="-tiff" ;;
*)
/usr/bin/zenity --warning --text="$NOT_A_VALID_FORMAT"
return 1
;;
esac

pdftoppm \
"${format_options}" \
-forcenum \
-sep _ \
-r "$resolution" \
-f "$firstpage" \
-l "$lastpage" \
"$file" \
"${DIRECTORY}/images_${FILE_NAME}/pg"

}

(
if [ "$EXTENSION" == "gif" ]; then
/usr/bin/convert -delay 20 -loop 0 "$@" "${DIRECTORY}/output.gif"
exit 0
fi
) | /usr/bin/zenity --progress \
--title="$PROGRESS_TITLE" \
--text="$PROGRESS_TEXT" \
--title="$IMAGE__PROGRESS_TITLE" \
--text="$IMAGE__PROGRESS_TEXT" \
--pulsate \
--auto-close

Expand All @@ -109,8 +208,14 @@ fi
COUNT=$((COUNT + 1))
echo "$((COUNT * 100 / TOTAL_FILES))"
echo "# Converting $FILE ($COUNT of $TOTAL_FILES)"
elif [[ $MIMETYPE == application/pdf ]]; then
convert_pdf "$FILE"
COUNT=$((COUNT + 1))
echo "$((COUNT * 100 / TOTAL_FILES))"
echo "# Converting $FILE ($COUNT of $TOTAL_FILES)"
else
/usr/bin/zenity --warning --text="$FILE $NOT_AN_IMAGE."
[[ $MIMETYPE == image/* ]] && /usr/bin/zenity --warning --text="$FILE $NOT_AN_IMAGE."
[[ $MIMETYPE == application/pdf ]] && /usr/bin/zenity --warning --text="$FILE $NOT_A_PDF."
fi
done
) | /usr/bin/zenity --progress \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"uuid": "convert-image-format@el-amine-404",
"name": "Convert Image Format",
"author": "el-amine-404",
"version": "0.0.1"
"version": "0.1.0"
}
Loading

0 comments on commit a377bb3

Please sign in to comment.