[Tutor] Converting to PDF
"Héctor Villafuerte D."
hec.villafuerte at telgua.com.gt
Thu Jan 8 11:39:01 EST 2004
Hi all!
Danny was so right! This is a bug in PIL.
I changed '/DctDecode' and '/DCTDecode' in PdfImagePlugin.py
and it worked just fine!
Thanks Danny and all you Python people.
Hector
Danny Yoo wrote:
>
>On Wed, 7 Jan 2004, [ISO-8859-1] "H?ctor Villafuerte D." wrote:
>
>
>>Hi all,
>>I'm trying to generate PDFs with this little script:
>>
>>-----SCRIPT-------------------------
>>import fileinput, os, Image
>>
>>def get_calls(dir, file):
>> im = Image.open(dir + file)
>> im.save(dir + '_' + file, 'PDF')
>>
>>if __name__ == '__main__':
>> dir = 'c:\\tmp\\scan\\'
>> for x in os.listdir(dir):
>> if x.split('.')[1] == 'jpg':
>> print x
>> get_calls(dir, x)
>>
>
>
>Hi Hector,
>
>
>I looked into the PdfImagePlugin.py source from version 1.1.4 of the
>Python Imaging Library,
>
> http://www.pythonware.com/products/pil/
>
>
>And you appear to have run into a bug in PIL! The code in question in
>PIL/PdfImagePlugin.py is:
>
>###
> if filter == "/ASCIIHexDecode":
> ImageFile._save(im, op, [("hex", (0,0)+im.size, 0, None)])
> elif filter == "/DCTDecode":
> ImageFile._save(im, op, [("jpeg", (0,0)+im.size, 0, im.mode)])
> elif filter == "/FlateDecode":
> ImageFile._save(im, op, [("zip", (0,0)+im.size, 0, im.mode)])
> elif filter == "/RunLengthDecode":
> ImageFile._save(im, op, [("packbits", (0,0)+im.size, 0, im.mode)])
> else:
> raise ValueError, "unsupported PDF filter"
>###
>
>
>That block is within the _safe() function in PdfImagePlugin.py. But
>there's only one block of code that assigns to this 'filter' variable:
>
>###
> if im.mode == "1":
> filter = "/ASCIIHexDecode"
> config = "/DeviceGray", "/ImageB", 1
> elif im.mode == "L":
> filter = "/DctDecode"
> # params = "<< /Predictor 15 /Columns %d >>" % (width-2)
> config = "/DeviceGray", "/ImageB", 8
> elif im.mode == "P":
> filter = "/ASCIIHexDecode"
> config = "/Indexed", "/ImageI", 8
> elif im.mode == "RGB":
> filter = "/DCTDecode"
> config = "/DeviceRGB", "/ImageC", 8
> elif im.mode == "CMYK":
> filter = "/DCTDecode"
> config = "/DeviceRGB", "/ImageC", 8
> else:
> raise ValueError, "illegal mode"
>###
>
>
>'filter' here has three possible values:
>
> ['/ASCIIHexDecode',
> '/DctDecode',
> '/DCTDecode']
>
>
>Notice the case difference here between '/DctDecode' and '/DCTDecode'.
>Python is case sensitive. This is a bad sign. *grin*
>
>
>The block that we're running into problems with, the one that throws the
>exception, checks for:
>
> ["/ASCIIHexDecode",
> "/DCTDecode",
> "/FlateDecode",
> "/RunLengthDecode"]
>
>
>It doesn't handle "/DctDecode"! Furthermore, there's no way 'filter' can
>be '/RunLengthDecode', so there's some dead code here too.
>
>There's definitely a bug here. Send a holler out to the Python Imaging
>Library folks. *grin*
>
> http://mail.python.org/mailman/listinfo/image-sig
>
>and get them to fix it so no one else runs into this.
>
>
>
>In the meantime, my best guess right now to fix the bug is to modify
>PdfImagePlugin.py and switch over the '/DctDecode' string to '/DCTDecode'
>and see if that clears up the problem. Unfortunately, I can't test this
>hypothesis without a sample JPEG image.
>
>
>
>
>
>By the way: you may want use os.path.join() to join together the directory
>name with the file name. The code in get_calls():
>
>
>>def get_calls(dir, file):
>> im = Image.open(dir + file)
>> im.save(dir + '_' + file, 'PDF')
>>
>
>may not get the correct name of the directory, since the path separator
>might be missing.
>
>I can see that you have hardcoded the path separator embedded in the 'dir'
>directory name within your main program, but if you are using get_calls()
>from another program, then there's no guarantee that the directory name
>ends with the path separator character.
>
>
>
>Hope this helps!
>
>
>
>
More information about the Tutor
mailing list