Skip to content

Commit 84caaf8

Browse files
committed
fix pdf rendering with correct output. PythonMagick is currently broken, and dont allow to tweak the output writter. So we are unable to set our preference for interlace, depth... so use a png
1 parent 77f878f commit 84caaf8

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

kivy/core/image/img_pdf.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22
PDF: PDF image loader
33
'''
44

5-
try:
6-
from PIL import Image as PILImage
7-
except:
8-
raise
9-
105
from kivy.cache import Cache
116
from kivy.logger import Logger
127
from kivy.core.image import ImageLoaderBase, ImageData, ImageLoader
138

14-
from kivy.graphics.texture import Texture, TextureRegion
9+
from kivy.graphics.texture import Texture
1510
Debug = False
1611

1712
import io
1813
from pyPdf import PdfFileWriter, PdfFileReader
1914
import PythonMagick as pm
15+
import pygame
16+
2017

2118
class ImageLoaderPDF(ImageLoaderBase):
2219
'''Image loader for PDF'''
@@ -33,35 +30,46 @@ def extensions():
3330
def load(self, filename):
3431
try:
3532
print "load pdf!", self.page
33+
3634
# first extract the page from the pdf
37-
pdf = PdfFileReader(open(filename, 'rb'))
38-
p = pdf.getPage(self.page)
39-
size = p.mediaBox[2:]
40-
print p.trimBox, p.mediaBox, p.cropBox, p.bleedBox, p.artBox
35+
with open(filename, 'rb') as fd:
36+
pdf = PdfFileReader(fd)
37+
page = pdf.getPage(self.page)
4138

42-
w = PdfFileWriter()
43-
w.addPage(p)
44-
f = io.BytesIO()
45-
w.write(f)
46-
f.seek(0)
39+
pfw = PdfFileWriter()
40+
pfw.addPage(page)
41+
f = io.BytesIO()
42+
pfw.write(f)
43+
f.seek(0)
4744

4845
# then convert the one page pdf filebuffer to an image
4946
blob = pm.Blob(f.read())
50-
blobrgb = pm.Blob()
51-
pm.Image(blob).write(blobrgb,'rgba')
52-
#blobpng.data
47+
blobresult = pm.Blob()
48+
49+
# export as a png. raw rgb/rgba might output interlaced/depth that
50+
# we don't want. current PythonMagick bindings don't allow to change
51+
# it. so rely as a png.
52+
pmi = pm.Image(blob)
53+
pmi.write(blobresult, 'png')
5354

54-
im = PILImage.frombuffer(
55-
'RGBA',
56-
(size[0] * 2, size[1]),
57-
blobrgb.data).resize(size)
55+
# load the png though pygame
56+
fd = io.BytesIO()
57+
fd.write(blobresult.data)
58+
fd.seek(0)
59+
im = pygame.image.load(fd, 'bleh.png')
60+
assert(im.get_bytesize() == 3)
61+
62+
# update internals
63+
self.filename = filename
64+
data = pygame.image.tostring(im, 'RGB', True)
65+
66+
# result a list of one image available
67+
return [ImageData(im.get_width(), im.get_height(), 'rgb', data)]
5868

5969
except:
6070
Logger.warning('Image: Unable to load image <%s>' % filename)
6171
raise
6272

63-
return (ImageData(size[0], size[1], 'rgba', im.tostring()),)
64-
6573
def populate(self):
6674
self._textures = []
6775
if __debug__:

0 commit comments

Comments
 (0)