Mobile app version of vmapp.org
Login or Join
Lengel450

: Export multi page pdf with gimp I understand that it may be possible using imageMagicks: convert image.mng out.pdf however is there a gimp way of doing this? I also saw this script/plugin

@Lengel450

Posted in: #Gimp #Python

I understand that it may be possible using imageMagicks:

convert image.mng out.pdf

however is there a gimp way of doing this?

I also saw this script/plugin which uses convert:
www.dropbox.com/s/jzdkgv2f0jrbw6i/export-layers-to-pdf.py?dl=0
#!/usr/bin/env python
#
# Author: helour
# Copyright: 2013-2015 helour
# Based on the cr33dog's script Export Layers as PNG (http://registry.gimp.org/node/18440)
# License: GPL v3+
#
# Version: 0.7
#
# GIMP plugin to export layers as a multiple pages PDF file
#
#
# Note for Windows users:
#
# You need add the ImageMagic directory (which consists the 'convert.exe' executable file)
# to the GIMP environment PATH variable into the file:
# C:Program FilesGIMP 2libgimp2.0environdefault.env
#
# like in the example here:
# PATH=${gimp_installation_dir}bin;${gimp_installation_dir}32bin;C:Program FilesImageMagick-6.9.1-Q16
# PYTHONPATH=${gimp_installation_dir}32libgimp2.0python


import os
import gtk
from subprocess import check_call
from tempfile import mkstemp

from gimpfu import *

def mktmpfile(suffix):
fd, filename = mkstemp(suffix=suffix)
fptr = os.fdopen(fd)
return filename

def get_layers_to_export(layers, only_visible, gimp_version):
result = []
for layer in layers:
if gimp_version >= 2.8 and pdb.gimp_item_is_group(layer):
result += get_layers_to_export(layer.children, only_visible, gimp_version)
else:
if only_visible:
if layer.visible:
result.append(layer)
else:
result.append(layer)
return result

def combine_images_into_pdf(img_files, pdf_file):
try: # Run on shell because of conflict with windows system command 'convert.exe'
check_call(['convert'] + img_files + [pdf_file], shell = True if os.name == 'nt' else False)
except Exception as e:
pdb.gimp_message("Error while executing 'convert' command:n" +
str(e) +
"nnHave you installed the ImageMagic packagenand/ornset the GIMP environment PATH variable?")

def export_layers(image, only_visible, quality):
if not image.filename:
pdb.gimp_message("Please save your file first!")
return

chooser = gtk.FileChooserDialog(title = None, action = gtk.FILE_CHOOSER_ACTION_SAVE,
buttons = (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
chooser.set_current_folder(os.path.dirname(image.filename))
chooser.set_current_name(os.path.splitext(image.filename)[0] + '.pdf')
if chooser.run() != gtk.RESPONSE_OK:
return
filename = chooser.get_filename()
chooser.destroy()

version = gimp.version[0:2]
gimp_version = float(version[0]) + float(version[1]) / 10.0

layers_to_export = get_layers_to_export(image.layers, only_visible, gimp_version)
img_files = []
try:
for layer in layers_to_export:
ext = '.jpg' if quality < 100 else '.png'
fullpath = mktmpfile(ext)
img_files.append(fullpath)
pic_filename = os.path.basename(fullpath)
if quality < 100:
pdb.file_jpeg_save(image, layer, fullpath, pic_filename, quality / 100.0, 0, 1, 0, "", 0, 1, 0, 2)
else:
pdb.file_png_save(image, layer, fullpath, pic_filename, 0, 9, 1, 1, 1, 1, 1)
combine_images_into_pdf(img_files, filename)
finally:
for img in img_files:
try:
os.remove(img)
except:
pass

register(
"export-layers-to-pdf", #name
"Export layers to a multiple pages PDF file", #description
"Export all layers to a single multiple pages PDF file", #help
"helour", #author
"helour", #copyright
"2015", #year
"Export layers to PDF", #menu label
"*", # image format
[ #input args. Format (type, name, description, default [, extra])
(PF_IMAGE, "image", "Image", None),
(PF_BOOL, "only_visible", "Only Visible Layers?", True),
(PF_SLIDER, "quality", "Image quality", 100, (10, 100, 1)),
],
[], #results. Format (type, name, description)
export_layers, #callback
menu=("<Image>/File/Export/"),
)

main()


However, I am not seeing a /Applications/GIMP.app/Contents/Resources/share/gimp/2.0/plug-ins folder, so i made one and put the python script inside of it as well as ran chmod +x on the script.

I also saw something in the python-fu browser, but have no idea how to use it:



"images" is undefined:



Gimp 2.8.14 Mac osx

I also followed some advice to look in /Library but didn't find anything:

➜ /Library sudo find . | grep -i gimp
Password:
./Caches/Homebrew/brew-cask--git/Casks/gimp.rb
./Caches/Homebrew/brew-cask--git/Casks/lisanet-gimp.rb
./Caches/Homebrew/Formula/gimp.brewing


Is there anyway to easily install plugins that I am missing here?

update

With the help of @tmanni in irc i was able to find the script path /Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/plug-ins inside preferences > plug-ins:



However, the plugin would exit saying convert is not installed.

I then tried to add /usr/local/bin to the Plug-Ins path in the menu but it then crashes with an exit code status of -5.

I then tried to directly ln -s /usr/local/bin/convert /Applications/GIMP.app/Contents/Resources/bin/convert but no dice there either.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Lengel450

3 Comments

Sorted by latest first Latest Oldest Best

 

@Welton168

I fixed this very easily by adding the absolute path to the convert executable inside the script. On my machine it is:

/opt/local/bin/convert

This function becomes:

def combine_images_into_pdf(img_files, pdf_file):
try: # Run on shell because of conflict with windows system command 'convert.exe'
check_call(['/opt/local/bin/convert'] + img_files + [pdf_file], shell = True if os.name == 'nt' else False)
except Exception as e:
pdb.gimp_message("Error while executing 'convert' command:n" +
str(e) +
"nnHave you installed the ImageMagic packagenand/ornset the GIMP environment PATH variable?")

10% popularity Vote Up Vote Down


 

@Odierno310

I spent my afternoon contending with the same issue.

You can fully alleviate the problem by changing the plugin's combine_images_into_pdf function:

def combine_images_into_pdf(img_files, pdf_file):
try: # Run on shell because of conflict with windows system command 'convert.exe'
my_env = os.environ.copy()
my_env["DYLD_LIBRARY_PATH"] = "/usr/local/lib:"
p = subprocess.Popen(['convert'] + img_files + [pdf_file], env=my_env)
p.wait()
except Exception as e:
pdb.gimp_message("Error while executing 'convert' command:n" +
str(e) + "nnHave you installed the ImageMagic packagenand/ornset the GIMP environment PATH variable?")


You will also need to change the imports -- either import subprocess or from subprocess import Popen instead of from subprocess import check_call (in the code block above I used import subprocess).

See here for info about Popen: docs.python.org/2/library/subprocess.html#subprocess.Popen https://stackoverflow.com/questions/2231227/python-subprocess-popen-with-a-modified-environment

The DYLD_LIBRARY_PATH environment variable gets set in the GIMP startup script located at /Applications/GIMP.app/Contents/MacOS/GIMP. The default DYLD_LIBRARY_PATH is needed for GIMP to start itself up and compress PNG images (as the errors with libfreetype, libpng, and liblzma show), but you can allow convert to use the proper libraries by setting this environment variable to wherever the proper libraries are when you call Popen. Since you're using homebrew like me, I assume that using /usr/local/lib/ for that directory will work.

This way you don't need to delete anything to get the plugin to fully work -- just install all the dependencies and edit the plugin script, and you'll be good to go. The code above doesn't alter GIMP's environment itself, so it won't cause any errors if you go back and edit your images after exporting them.

10% popularity Vote Up Vote Down


 

@Lengel450

After spending the better part of a week fiddling around with this:

➜ gimp_export_layers_to_pdf git:(master) brew --config
HOMEBREW_VERSION: 0.9.5
ORIGIN: github.com/Homebrew/homebrew HEAD: b51d0bc7f7b3575227538f84151b3fa4318f2774
Last commit: 42 minutes ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_BOTTLE_DOMAIN: homebrew.bintray.com CPU: 8-core 64-bit haswell
OS X: 10.10.5-x86_64
Xcode: 7.1.1
CLT: 7.1.0.0.1.1444952191
Clang: 7.0 build 700
X11: N/A
System Ruby: 2.0.0-p481
Perl: /usr/bin/perl
Python: /usr/local/bin/python => /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/bin/python2.7
Ruby: /usr/bin/ruby => /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby
Java: 1.6.0_65-b14-468


Solution:

I opened a git repo for this script, I was a bit surprised about how difficult the plugin registry for gimp was, however I got it to a point where it will work if the pdf is exported with a quality less than 100:

github.com/jmunsch/gimp_export_layers_to_pdf

The longer story:

Basically what was happening was gimp was using the default libraries that are included with gimp (sounds like a good idea) until I open gimp from terminal:

➜ ~ /Applications/GIMP.app/Contents/MacOS/GIMP ~/Desktop/Export_These_Layers.xcf
...
dyld: Library not loaded: /usr/local/lib/libfreetype.6.dylib
Referenced from: /usr/local/bin/convert
Reason: Incompatible library version: convert requires version 19.0.0 or later, but libfreetype.6.dylib provides version 18.0.0


convert is installed?:

➜ ~ ls -la /usr/local/bin/convert
lrwxr-xr-x 1 jmunsch admin 41 Nov 26 15:47 /usr/local/bin/convert -> ../Cellar/imagemagick/6.9.2-6/bin/convert


what libraries is convert using

➜ ~ otool -L /usr/local/bin/convert
/usr/local/bin/convert:
/usr/local/Cellar/imagemagick/6.9.2-6/lib/libMagickCore-6.Q16.2.dylib (compatibility version 3.0.0, current version 3.0.0)
/usr/local/Cellar/imagemagick/6.9.2-6/lib/libMagickWand-6.Q16.2.dylib (compatibility version 3.0.0, current version 3.0.0)
/usr/local/opt/freetype/lib/libfreetype.6.dylib (compatibility version 19.0.0, current version 19.0.0)
/usr/local/opt/xz/lib/liblzma.5.dylib (compatibility version 8.0.0, current version 8.2.0)
/usr/lib/libbz2.1.0.dylib (compatibility version 1.0.0, current version 1.0.5)
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
/usr/local/opt/libtool/lib/libltdl.7.dylib (compatibility version 11.0.0, current version 11.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)


Everything appears to be up to date?

So how many places is that library?:

➜ ~ locate libfreetype
/Applications/GIMP.app/Contents/Resources/lib/libfreetype.6.dylib
/Applications/Steam.app/Contents/MacOS/libfreetype.dylib
/Volumes/Gimp 2.8.14/GIMP.app/Contents/Resources/lib/libfreetype.6.dylib
/usr/X11/lib/libfreetype.6.dylib
/usr/local/Cellar/freetype/2.6_1/lib/libfreetype.6.dylib
/usr/local/Cellar/freetype/2.6_1/lib/libfreetype.a
/usr/local/Cellar/freetype/2.6_1/lib/libfreetype.dylib
/usr/local/lib/libfreetype.6.dylib
/usr/local/lib/libfreetype.a
/usr/local/lib/libfreetype.dylib


Oh, I see:

➜ ~ otool -L /Applications/GIMP.app/Contents/Resources/lib/libfreetype.6.dylib
/Applications/GIMP.app/Contents/Resources/lib/libfreetype.6.dylib:
/usr/local/lib/libfreetype.6.dylib (compatibility version 19.0.0, current version 18.0.0)


So I copy it over:

➜ ~ cp /usr/local/opt/freetype/lib/libfreetype.6.dylib /Applications/GIMP.app/Contents/Resources/lib


And Retry:

➜ ~ /Applications/GIMP.app/Contents/MacOS/GIMP ~/Desktop/Export_These_Layers.xcf
Setting up environment...
Enabling internal Python...
Launching GIMP...
dyld: Library not loaded: /usr/local/lib/libpng16.16.dylib
Referenced from: /Applications/GIMP.app/Contents/Resources/lib/libfreetype.6.dylib
Reason: Incompatible library version: libfreetype.6.dylib requires version 34.0.0 or later, but libpng16.16.dylib provides version 29.0.0
[1] 69814 trace trap /Applications/GIMP.app/Contents/MacOS/GIMP ~/Desktop/Export_These_Layers.xcf


so I try libpng and copy it over to gimp:

➜ ~ locate libpng16
.
.
.
➜ ~ cp /usr/local/Cellar/libpng/1.6.18/lib/libpng16.16.dylib /Applications/GIMP.app/Contents/Resources/lib


Restart the gimp from terminal and retry using the plugin:

dyld: Library not loaded: /usr/local/opt/xz/lib/liblzma.5.dylib
Referenced from: /usr/local/bin/convert
Reason: Incompatible library version: convert requires version 8.0.0 or later, but liblzma.5.dylib provides version 6.0.0


okay where is it pulling this library from:

➜ ~ otool -L /usr/local/lib/liblzma.5.dylib
/usr/local/lib/liblzma.5.dylib:
/usr/local/opt/xz/lib/liblzma.5.dylib (compatibility version 8.0.0, current version 8.2.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1213.0.0)

➜ ~ otool -L /Applications/GIMP.app/Contents/Resources/lib/liblzma.5.dylib
liblzma.5.dylib:
/Users/gimpdev/gimp/10.6/inst/lib/liblzma.5.dylib (compatibility version 6.0.0, current version 6.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)


And so I copy that file over as well:

cp /usr/local/lib/liblzma.5.dylib /Applications/GIMP.app/Contents/Resources/lib


Yet another issue after linking the new libraries:

convert: unable to load module `/usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders/png.la': file not found @ error/module.c/OpenModule/1300.
convert: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/501.
convert: unable to load module `/usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders/png.la': file not found @ error/module.c/OpenModule/1300.
convert: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/501.
convert: unable to load module `/usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders/png.la': file not found @ error/module.c/OpenModule/1300.
convert: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/501.
convert: unable to load module `/usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders/png.la': file not found @ error/module.c/OpenModule/1300.
convert: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/501.


I retry but build from source this time:

brew install imagemagick --build-from-source


Retry and same issue with PNG module, So I run brew doctor:

echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc


So I retry installing libpng:

➜ ~ brew uninstall imagemagick; brew install imagemagick;brew install libpng; brew link libpng
Warning: libpng-1.6.19 already installed
Warning: Already linked: /usr/local/Cellar/libpng/1.6.19
To relink: brew unlink libpng && brew link libpng

➜ ~ brew unlink libpng && brew link libpng
Unlinking /usr/local/Cellar/libpng/1.6.19... 18 symlinks removed
Linking /usr/local/Cellar/libpng/1.6.19... 18 symlinks created


And xz:

brew uninstall xz
brew uninstall imagemagick; brew install imagemagick --build-from-source


And finally go for some harder rebuilding:

brew uninstall --force imagemagick; brew install -v imagemagick --build-from-source


I looked at the docs for convert's ReadImage:

open /usr/local/Cellar/imagemagick//6.9.1-10/share/doc/ImageMagick-6/www/api/constitute.html


I read the source code for ReadImage.

I stop, and look back at the python script.
I force the script to use jpeg instead of png.

It works.

Here is the working history that I attempted:

cd /Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/plug-ins
chown jmunsch:admin export-layers-to-pdf.py


cd /var/folders/w1

/Applications/GIMP.app/Contents/MacOS/GIMP ~/Desktop/Export_These_Layers.xcf

otool -L /usr/local/bin/convert

brew update && brew upgrade
brew reinstall imagemagick

ls /usr/local/opt/freetype/lib/libfreetype.6.dylib
stat /usr/local/opt/freetype/lib/libfreetype.6.dylib

otool -L /usr/local/bin/convert

cp /usr/local/opt/freetype/lib/libfreetype.6.dylib /Applications/GIMP.app/Contents/Resources/lib


locate libpng16
cp /usr/local/Cellar/libpng/1.6.18/lib/libpng16.16.dylib /Applications/GIMP.app/Contents/Resources/lib

locate liblzma.5.dylib
otool -L /usr/local/lib/liblzma.5.dylib
otool -L /Applications/GIMP.app/Contents/Resources/lib/liblzma.5.dylib
cp /usr/local/lib/liblzma.5.dylib /Applications/GIMP.app/Contents/Resources/lib

which convert
ls -la /usr/local/bin/convert

ls /usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/
ls /usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders
/usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders/png.la
stat /usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders/png.la

convert '/var/folders/w1/gg1rwbxd17x07kv9swxwv84h0000gn/T/tmpsPHsvG.png' '/var/folders/w1/gg1rwbxd17x07kv9swxwv84h0000gn/T/tmpXGKL0S.png' '/var/folders/w1/gg1rwbxd17x07kv9swxwv84h0000gn/T/tmp196lyv.png' '/var/folders/w1/gg1rwbxd17x07kv9swxwv84h0000gn/T/tmprNZdlG.png' '/Users/jmunsch/Desktop/hggfd.pdf'

brew doctor
echo 'export PATH="/usr/local/sbin:$PATH"' >> ~/.zshrc

ls /usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16
ls /usr/local/Cellar/imagemagick/6.9.2-6/lib/ImageMagick//modules-Q16/coders
cd ~/Desktop
python test.py

open /usr/local/Cellar/imagemagick//6.9.1-10/share/doc/ImageMagick-6/www/api/constitute.html

brew uninstall imagemagick; brew install imagemagick;brew install libpng; brew link libpng
brew --config
brew uninstall xz
brew uninstall imagemagick; brew install imagemagick --build-from-source
brew uninstall --force imagemagick; brew install -v imagemagick --build-from-source


cp /usr/local/lib/liblzma.5.dylib /Applications/GIMP.app/Contents/Resources/lib

brew uninstall --force imagemagick

brew update && brew upgrade && brew uninstall --force imagemagick && brew install -v imagemagick --build-from-source
which convert
ls -la /usr/local/bin/convert

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme