Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix comparations for image colorspace literals #132

Merged
merged 6 commits into from
Oct 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions pdfminer/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def export_image(self, image):
if len(filters) == 1 and filters[0][0] in LITERALS_DCT_DECODE:
ext = '.jpg'
elif (image.bits == 1 or
image.bits == 8 and image.colorspace in (LITERAL_DEVICE_RGB, LITERAL_DEVICE_GRAY)):
image.bits == 8 and (LITERAL_DEVICE_RGB in image.colorspace or LITERAL_DEVICE_GRAY in image.colorspace)):
ext = '.%dx%d.bmp' % (width, height)
else:
ext = '.%d.%dx%d.img' % (image.bits, width, height)
Expand All @@ -101,15 +101,15 @@ def export_image(self, image):
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_RGB:
elif image.bits == 8 and LITERAL_DEVICE_RGB in image.colorspace:
bmp = BMPWriter(fp, 24, width, height)
data = stream.get_data()
i = 0
width = width*3
for y in range(height):
bmp.write_line(y, data[i:i+width])
i += width
elif image.bits == 8 and image.colorspace is LITERAL_DEVICE_GRAY:
elif image.bits == 8 and LITERAL_DEVICE_GRAY in image.colorspace:
bmp = BMPWriter(fp, 8, width, height)
data = stream.get_data()
i = 0
Expand Down
75 changes: 53 additions & 22 deletions tests/test_tools_pdf2txt.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
#!/usr/bin/env python
import os
from shutil import rmtree
from tempfile import NamedTemporaryFile, mkdtemp

# -*- coding: utf-8 -*-

import nose, logging, os
import nose

import tools.pdf2txt as pdf2txt

path=os.path.dirname(os.path.abspath(__file__))+'/'

def run(datapath,filename,options=None):
i=path+datapath+filename+'.pdf'
o=path+filename+'.txt'
def full_path(relative_path_to_this_file):
this_file_dir = os.path.dirname(os.path.abspath(__file__))
abspath = os.path.abspath(os.path.join(this_file_dir, relative_path_to_this_file))
return abspath


def run(datapath, filename, options=None):
i = full_path(datapath + filename + '.pdf')
o = full_path(filename + '.txt')
if options:
s='pdf2txt -o%s %s %s'%(o,options,i)
s = 'pdf2txt -o%s %s %s' % (o, options, i)
else:
s='pdf2txt -o%s %s'%(o,i)
s = 'pdf2txt -o%s %s' % (o, i)
pdf2txt.main(s.split(' ')[1:])


class TestDumpPDF():

def test_1(self):
run('../samples/','jo')
run('../samples/','simple1')
run('../samples/','simple2')
run('../samples/','simple3')
run('../samples/', 'jo')
run('../samples/', 'simple1')
run('../samples/', 'simple2')
run('../samples/', 'simple3')
run('../samples/','sampleOneByteIdentityEncode')

def test_2(self):
run('../samples/nonfree/','dmca')
run('../samples/nonfree/', 'dmca')

def test_3(self):
run('../samples/nonfree/','f1040nr')
run('../samples/nonfree/', 'f1040nr')

def test_4(self):
run('../samples/nonfree/','i1040nr')
run('../samples/nonfree/', 'i1040nr')

def test_5(self):
run('../samples/nonfree/','kampo')
run('../samples/nonfree/', 'kampo')

def test_6(self):
run('../samples/nonfree/','naacl06-shinyama')
run('../samples/nonfree/', 'naacl06-shinyama')

# this test works on Windows but on Linux & Travis-CI it says
# PDFSyntaxError: No /Root object! - Is this really a PDF?
Expand All @@ -50,13 +56,38 @@ def test_7(self):
"""

def test_8(self):
run('../samples/contrib/','2b','-A -t xml')
run('../samples/contrib/', '2b', '-A -t xml')

def test_9(self):
run('../samples/nonfree/','175') # https://github.com/pdfminer/pdfminer.six/issues/65
run('../samples/nonfree/', '175') # https://github.com/pdfminer/pdfminer.six/issues/65

def test_10(self):
run('../samples/scancode/','patchelf') # https://github.com/euske/pdfminer/issues/96
run('../samples/scancode/', 'patchelf') # https://github.com/euske/pdfminer/issues/96


class TestDumpImages(object):

def extract_images(self, input_file):
output_dir = mkdtemp()
with NamedTemporaryFile() as output_file:
commands = ['-o', output_file.name, '--output-dir', output_dir, input_file]
pdf2txt.main(commands)
image_files = os.listdir(output_dir)
rmtree(output_dir)
return image_files

def test_nonfree_dmca(self):
"""Extract images of pdf containing bmp images

Regression test for: https://github.com/pdfminer/pdfminer.six/issues/131
"""
image_files = self.extract_images(full_path('../samples/nonfree/dmca.pdf'))
assert image_files[0].endswith('bmp')

def test_nonfree_175(self):
"""Extract images of pdf containing jpg images"""
self.extract_images(full_path('../samples/nonfree/175.pdf'))


if __name__ == '__main__':
nose.runmodule()