Datasets:

Languages:
English
License:
File size: 2,422 Bytes
5c7c0b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from wand.image import Image as WandImage
from wand.color import Color
import os

# save all pages in a temporary directory to load and plot later
def save_pages(ffout, pages,tmp_dir, # ffout is the modified .tex file
    pdffigures_dpi = 72, # default
    fac_dpi = 4, # how much larger to make jpeg to avoid any antialiasing 
               check_exist=False, # , check if exist
              save_fmt = 'jpeg', 
              fac_down_dpi = 1, 
              beginner='', 
              return_error = False): 
    
    if ffout[-4:] != '.pdf':
        pdfout = ffout.replace('.tex','.pdf')
        split_ffout = ffout.split('/')[-1].split('.tex')[0]
    else:
        pdfout = ffout
        split_ffout = ffout.split('/')[-1].split('.pdf')[0]
    
    if fac_down_dpi == None:
        fac_down_dpi = 1.0/fac_dpi

    page_errors = []
    for page in pages:
        outimgname = tmp_dir+ beginner + \
              split_ffout + \
             '_p'+str(page) + '.'+save_fmt

        # check if it exists
        err = False
        if (not os.path.exists(outimgname)) or (not check_exist):
            try:
                wimgPDF = WandImage(filename=pdfout +'[' + str(int(page)) + ']', 
                                    resolution=pdffigures_dpi*fac_dpi, format='pdf') #2x DPI which shrinks later
            except:
                err = True
                print('error in DPI')
                print(pdfout)
                
            if not err:
                thisSeq = wimgPDF.sequence
                imPDF = thisSeq[0] # load PDF page into memory

                # make sure we do our best to capture accurate text/images
                imPDF.resize(width=int(round(fac_down_dpi*imPDF.width)),
                             height=int(round(fac_down_dpi*imPDF.height)))
                imPDF.background_color = Color("white")
                imPDF.alpha_channel = 'remove'
                #WandImage(imPDF).save(filename=outimgname)
                #imPDF.background_color = Color("white")
                #imPDF.alpha_channel = 'remove'
                imPDFout = WandImage(imPDF)
                imPDFout.resolution=int(round(pdffigures_dpi*fac_down_dpi*fac_dpi))
                imPDFout.units = 'pixelsperinch'
                imPDFout.save(filename=outimgname)
                del imPDF
                del imPDFout
        page_errors.append(err)
    if return_error: 
        return page_errors