Files
AFM_thumbs/AFM_thumbs/PDF.py
2022-04-08 17:39:57 +02:00

268 lines
11 KiB
Python
Executable File

# ##### BEGIN GPL LICENSE BLOCK #####
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
#
# This is part of the python program 'AFM_thumbs'. Please, read the
# full licence text and other comments in the file 'AFM_thumbs.py'
#
# Dr. Clemens Barth (barth@root-1.de), project manager.
#
# DO NOT REMOVE THIS PREAMBLE !!!
#
import os
import shutil
import re
from AFM_thumbs.Variables import globvar_AFMdata
from AFM_thumbs.Variables import globvar_AFMdir
from AFM_thumbs.Variables import globvar_ThumbPara
from AFM_thumbs.Variables import globvar_spaces
# This is the definiton which produces the pdf. We will make use of
# graphicsmagic's 'gm montage' to realize the montage of thumbnail images in a
# x/y pattern into png/jpeg sheets. After, imagemagick's 'convert' will be used
# to create pdfs from the pngs/jpegs. Last, it is pdftk that puts together all
# pdfs.
#
# May be, all this can be changed such that we either have only graphicsmagic
# or imagemagick. Can we avoid also pdftk? Let's see in future.
def prepare_PDFs():
# Decide over the image ending
if globvar_ThumbPara.png:
end = '.png'
else:
end = '.jpeg'
# Get the list of images in the thumbnail directory.
image_file_list = filter(lambda x: x.endswith(end),
os.listdir(globvar_AFMdir.thumbnail_directory))
image_file_list = list(image_file_list)
if image_file_list == []:
print(globvar_spaces + "There are no images in the directory\n" +
globvar_spaces + globvar_AFMdir.thumbnail_directory + " !\n" +
globvar_spaces + "We leave this out ... .\n")
return False
# The list is well ordered thanks to the 001, 002, etc. order (see
# definitions in Omicron_SCALA.py, Nanotec_DULCINEA.py and RHK.py)
image_file_list.sort()
# Initialize the string for gm montage
# p: font size of the title, scale with number of images in one row
para_mon_p = globvar_ThumbPara.col * 8
str_tile = str(globvar_ThumbPara.col) + "x" + str(globvar_ThumbPara.row)
geo_x = max(globvar_ThumbPara.geometry_x)
geo_y = max(globvar_ThumbPara.geometry_y)
# First part of the string for graphicsmagick with standard parameters for
# all pdfs.
para_gm_mon_1 = ("gm montage -geometry " + str(geo_x) + "x" + str(geo_y) +
" -pointsize " + str(para_mon_p) +
" -title \"" + globvar_AFMdir.working_directory + "\"" +
" -tile " + str_tile + " " )
# Initialization of the string for gm and convert
m_str = ""
r = 0 # counter - total number of images on one page
q = 0 # counter - number of pages
FLAG_first_spec_image = False
# This "for loop" takes up to 'globvar_ThumbPara.col*globvar_ThumbPara.row'
# images for one page. The code is done such that first single pdf pages
# are created in the thumbnail directory. The reason is that graphicsmagic
# is asking for too much RAM otherwise. After all, pdftk puts together
# all pdf pages (see below).
#
# This was strongly improved on 2017-07-28.
for image_file in image_file_list:
if r < globvar_ThumbPara.col*globvar_ThumbPara.row:
# If a spectroscopy image appears then ...
if "_s" in image_file:
#if image_file[9:10] == "s":
# For the first spectroscopy image:
if "_s00" in image_file:
#if image_file[9:11] == "s0":
# Note that there is some spectroscopy data.
FLAG_first_spec_image = True
# Number of free slots in a row.
if r % globvar_ThumbPara.col == 0:
free_slots_in_row = 0
else:
free_slots_in_row = globvar_ThumbPara.col - (r % globvar_ThumbPara.col)
# If there is still some place in the row, fill it
# with dummies.
if free_slots_in_row != 0:
for i in range (free_slots_in_row):
m_str = m_str + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
"dummy_image.png\"")
r += 1
# ... if it is an image then ...
else:
# Uuups, the last image was obviously a spectroscopy image, now
# it is an SPM image. If so, new row:
if FLAG_first_spec_image == True:
# Number of free slots in a row.
if r % globvar_ThumbPara.col == 0:
free_slots_in_row = 0
else:
free_slots_in_row = globvar_ThumbPara.col - (r % globvar_ThumbPara.col)
# If there is still some place in the row, fill it
# with dummies.
if free_slots_in_row != 0:
for i in range (free_slots_in_row):
m_str = m_str + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
"dummy_image.png\"")
r += 1
FLAG_first_spec_image = False
# Put image and ...
m_str = m_str + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
image_file) + "\""
# ... image counter one up
r += 1
# globvar_ThumbPara.col*globvar_ThumbPara.row images are
# collected, or, end of image_file_list. So then do:
else:
# Create only one page first!
# Reason for this: If about 20 pages shall be created the pages
# cannot be stored within one pdf because Graphicsmagick is
# asking for too much RAM!!! So, page after page ...
# Number of pages
q += 1
# Graphicsmagic - montage
last_part = "_%.02d" + end + "\""
image_string = (para_gm_mon_1 + m_str + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
globvar_AFMdir.data_directory_name) + \
last_part % q)
os.system(image_string)
# Imagemagic - pdf
middle_part = "_%.02d" + end + "\" \""
pdf_string = ("convert -density 300 -compress JPEG -quality " + \
str(globvar_ThumbPara.jpeg) + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
globvar_AFMdir.data_directory_name) + \
middle_part%q + \
os.path.join(globvar_AFMdir.thumbnail_directory,
globvar_AFMdir.data_directory_name) + \
"_%.03d.pdf\" "%q )
os.system(pdf_string)
# Initialize and put in the first image
r = 1
m_str = "\""+os.path.join(globvar_AFMdir.thumbnail_directory,
image_file) + "\""
# If the image is a 'first' spectroscopy image then ...
if "_s00" in image_file:
# if image_file[9:11] == "s00":
# Note that there is some spectroscopy data.
FLAG_first_spec_image = True
# if there are remaining pages (r > 1) put them on one page
if r > 1:
q += 1
last_part = "_%.02d" + end + "\""
image_string = (para_gm_mon_1+m_str + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
globvar_AFMdir.data_directory_name) + \
last_part % q )
os.system(image_string)
middle_part = "_%.02d" + end + "\" \""
pdf_string = ("convert -density 300 -compress JPEG -quality " + \
str(globvar_ThumbPara.jpeg) + " \"" + \
os.path.join(globvar_AFMdir.thumbnail_directory,
globvar_AFMdir.data_directory_name) + \
middle_part%q + \
os.path.join(globvar_AFMdir.thumbnail_directory,
globvar_AFMdir.data_directory_name) + \
"_%.03d.pdf\" "%q )
os.system(pdf_string)
#
#
# Prepare the command string for pdftk for connecting all pdf pages.
#
#
# This following 'directory thing' was done for pdftk under Windows XP.
# First, we save the directory of AFM_thumbs and ...
AFM_thumbs_directory = os.getcwd()
# ... then, we go inside the thumbnail directory, and later we put the
# system path back onto 'AFM_thumbs_directory' (see below).
os.chdir(globvar_AFMdir.thumbnail_directory)
# Apply pdftk for putting together the pdfs.
if globvar_AFMdir.pdf_path == "":
pdf_string = ("pdftk *.pdf cat output \"" + \
globvar_AFMdir.working_directory + \
globvar_ThumbPara.extension + ".pdf\"" )
else:
pdf_string = ("pdftk *.pdf cat output \"" + \
os.path.join(globvar_AFMdir.pdf_path,
globvar_AFMdir.data_directory_name) + \
globvar_ThumbPara.extension+".pdf\"" )
# Execute pdftk
os.system(pdf_string)
# Now, we go back into the directory of AFM_thumbs
os.chdir(AFM_thumbs_directory)
# If true, delete all thumbnails and pdfs and finally the thumbnail
# directory
if globvar_ThumbPara.thumbs_after:
delete_string = globvar_AFMdir.thumbnail_directory
shutil.rmtree(delete_string)
else:
file_list = os.listdir(globvar_AFMdir.thumbnail_directory)
for f in file_list:
if f.endswith(".pdf"):
os.remove(os.path.join(globvar_AFMdir.thumbnail_directory,f))
elif f.endswith(end):
if re.search(globvar_AFMdir.data_directory_name,f):
os.remove(os.path.join(globvar_AFMdir.thumbnail_directory,f))