summaryrefslogtreecommitdiff
blob: 864bed9608283b3ac714ffa0107882a6e67bc9a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 21 10:05:07 2020
Example use of gsapi for various tasks.

@author: Michael Vrhel
"""

try:
    import gsapi
except Exception:
    print('Failure to import gsapi. Check shared library path')
    raise

import os

ghostpdl_root = os.path.abspath('%s/../../..' % __file__)
print('ghostpdl_root=%s' % ghostpdl_root)

def run_gpdl(params, path):
    instance = gsapi.gsapi_new_instance(0)
    gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
    gsapi.gsapi_add_control_path(instance, gsapi.GS_PERMIT_FILE_READING, path)
    gsapi.gsapi_init_with_args(instance, params)
    end_gpdl(instance)

def init_gpdl(params):
    instance = gsapi.gsapi_new_instance(0)
    gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
    gsapi.gsapi_init_with_args(instance, params)
    return instance

def run_file(instance, filename):
    exitcode = gsapi.gsapi_run_file(instance, filename, None)
    return exitcode

def end_gpdl(instance):
    gsapi.gsapi_exit(instance)
    gsapi.gsapi_delete_instance(instance)

# run multiple files through same instance
def multiple_files():

    out_filename = 'multi_file_output_%d.png'

    params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pngalpha',
             '-r72', '-o', out_filename]
    instance = init_gpdl(params)
    run_file(instance, '%s/examples/tiger.eps' % ghostpdl_root)
    run_file(instance, '%s/examples/snowflak.ps' % ghostpdl_root)
    run_file(instance, '%s/examples/annots.pdf' % ghostpdl_root)

    end_gpdl(instance)

# Extract text from source file
def extract_text():

    in_filename = '%s/examples/alphabet.ps' % ghostpdl_root
    out_filename = 'alphabet.txt'
    print('Extracting text from %s to %s' % (in_filename, out_filename))

    params =['gs', '-dNOPAUSE', '-dBATCH','-sDEVICE=txtwrite',
             '-dTextFormat=3','-o', out_filename, '-f', in_filename]
    run_gpdl(params, in_filename)

# Perform different color conversions on text, graphic, and image content
# through the use of different destination ICC profiles
def object_dependent_color_conversion():

    in_filename = '%s/examples/text_graph_image_cmyk_rgb.pdf' % ghostpdl_root
    out_filename = 'rendered_profile.tif'
    image_icc = '%s/toolbin/color/icc_creator/effects/cyan_output.icc' % ghostpdl_root
    graphic_icc = '%s/toolbin/color/icc_creator/effects/magenta_output.icc' % ghostpdl_root
    text_icc = '%s/toolbin/color/icc_creator/effects/yellow_output.icc' % ghostpdl_root
    print('Object dependent color conversion on %s to %s' % (in_filename, out_filename))

    params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=tiff32nc',
             '-r72','-sImageICCProfile=' + image_icc,
             '-sTextICCProfile=' + text_icc,
             '-sGraphicICCProfile=' + graphic_icc,
             '-o', out_filename, '-f', in_filename]

    # Include ICC profile location to readable path
    run_gpdl(params, '../../toolbin/color/icc_creator/effects/')

# Perform different color conversions on text, graphic, and image content
# through the use of different rendering intents
def object_dependent_rendering_intent():

    in_filename = '%s/examples/text_graph_image_cmyk_rgb.pdf' % ghostpdl_root
    out_filename = 'rendered_intent.tif'
    output_icc_profile = '%s/toolbin/color/src_color/cmyk_des_renderintent.icc' % ghostpdl_root
    print('Object dependent rendering intents on %s to %s' % (in_filename, out_filename))

    params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=tiff32nc',
             '-r72', '-sOutputICCProfile=' + output_icc_profile,
             '-sImageICCIntent=0', '-sTextICCIntent=1',
             '-sGraphicICCIntent=2', '-o', out_filename,
             '-f', in_filename]

    # Include ICC profile location to readable path
    run_gpdl(params, '../../toolbin/color/src_color/')

# Distill
def distill():

    in_filename = '%s/examples/tiger.eps' % ghostpdl_root
    out_filename = 'tiger.pdf'
    print('Distilling %s to %s' % (in_filename, out_filename))

    params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pdfwrite',
             '-o', out_filename, '-f', in_filename]
    run_gpdl(params, in_filename)

# Transparency in Postscript
def trans_ps():

    in_filename = '%s/examples/transparency_example.ps' % ghostpdl_root
    out_filename = 'transparency.png'
    print('Rendering Transparency PS file %s to %s' % (in_filename, out_filename))

    params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pngalpha',
             '-dALLOWPSTRANSPARENCY', '-o', out_filename, '-f', in_filename]
    run_gpdl(params, in_filename)

# Run string to feed chunks
def run_string():

    size = 1024;
    in_filename = '%s/examples/tiger.eps' % ghostpdl_root
    out_filename = 'tiger_byte_fed.png'
    params =['gs', '-dNOPAUSE', '-dBATCH', '-sDEVICE=pngalpha',
              '-o', out_filename]

    instance = gsapi.gsapi_new_instance(0)

    gsapi.gsapi_set_arg_encoding(instance, gsapi.GS_ARG_ENCODING_UTF8)
    gsapi.gsapi_init_with_args(instance, params)

    gsapi.gsapi_run_string_begin(instance, 0)

    with open(in_filename,"rb") as f:
        while True:
            data = f.read(size)
            if not data:
                break
            gsapi.gsapi_run_string_continue(instance, data, 0)

    exitcode = gsapi.gsapi_run_string_end(instance, 0)

    end_gpdl(instance)

    return exitcode


# Examples
print('***********Text extraction***********');
extract_text()
print('***********Color conversion***********')
object_dependent_color_conversion()
print('***********Rendering intent***********')
object_dependent_rendering_intent()
print('***********Distillation***************')
distill()
print('***********Postscript with transparency********')
trans_ps()
print('***********Multiple files********')
multiple_files()
print('***********Run string********')
run_string()
wait = input("press enter to exit")