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

Saving PDF documents with a char ':' in filename #4861

Closed
freQuensy23-coder opened this issue Aug 13, 2020 · 4 comments
Closed

Saving PDF documents with a char ':' in filename #4861

freQuensy23-coder opened this issue Aug 13, 2020 · 4 comments

Comments

@freQuensy23-coder
Copy link

freQuensy23-coder commented Aug 13, 2020

What did you do?

I needed to save an list of PIL Images to one pdf file. So I wrote code like this:

    img = image_list[0]
    img.save(get_file_name(FILE_PATH) + "_stamped_" + get_time() + ".pdf",
                                 resolution = 100.0,
                                 save_all=True,
                                 append_images=image_list[1:])

And get_time func:

def get_time():
    # Returns smth. like this: "12:00" or "02:51"
    return str(str(datetime.datetime.now().time())[0:5])

What happend

But when I ran the program, the pdf file was created, but it had an incomplete name and size of 0 bytes

image

The problem was that Windows does not support ':' in file names and Python cannot to create the file. But PIL does not give an error because of this and simply terminates the program. If the function is changed in this way
return str(str(datetime.datetime.now().time())[0:5]).replace(":", "-") , then everything will work.

What did you expect to happen?

PIL Image.save raise Exception

P.S.

image_list = [<PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x1844E25EB88>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B56FC8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B56108>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B5B048>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B638C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B664C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B66D88>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B66E08>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B66E88>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B66488>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B66F48>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B66FC8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E088>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E108>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E188>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E208>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E288>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E308>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E388>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E408>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E488>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E508>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E588>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E608>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E6C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E788>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E848>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E908>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6E9C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6EA88>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6EB48>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6EC08>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6ECC8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6ED88>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6EE48>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6EF08>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B6EFC8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B750C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75188>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75248>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75308>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B753C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75488>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75548>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75608>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B756C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75788>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75848>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75908>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B759C8>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75A88>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75B48>, <PIL.PpmImagePlugin.PpmImageFile image mode=RGB size=2339x1654 at 0x18400B75C08>]

P.P.S
OS Windows 10 with all updates
Python 3.7.6
Pillow==7.2.0
Other libs:

import pdf2image
import argparse
import os
import termcolor
import colorama
import PyPDF2
import config
from QRParse import parser #argsparse lib
import datetime
from fpdf import FPDF
@hugovk
Copy link
Member

hugovk commented Aug 13, 2020

What exception does Pillow give? Please could you paste the full traceback?

I'd guess some OS or file system exception? That would sound appropriate, and this is probably not a Pillow error.

I suggest you pass in a filename to img.save which is suitable for your operating system, so remove the colon in get_time, or pass it through a function to clean it first. Some ideas:

@freQuensy23-coder
Copy link
Author

freQuensy23-coder commented Aug 13, 2020

No. It DOES NOT give Exception. But I think that the behavior of the function, when it creates a file with a partial name and without extension, does not write anything to it and simply terminates, is wrong. It should throw an exception that the file should not have ':' in the name

@hugovk
Copy link
Member

hugovk commented Aug 13, 2020

Ah sorry, I misread the report.

Then it sounds like you're running into this:

Windows NTFS supports file "stream". You basically append data to a file, outside of the file, and can't be viewed normally. When you created the file "word1:word2", the hidden stream "word2" is attached to "word1". If you copied the file word1 to another NTFS machine, the word2 data would come with you

See the rest of the answer for more info. So something like test_stamped_00:37.pdf could be valid file: the hidden stream 37.pdf is attached to test_stamped_00.

@radarhere radarhere changed the title The problem with saving PDF documents with a char ':' in filename Saving PDF documents with a char ':' in filename Aug 13, 2020
@freQuensy23-coder
Copy link
Author

Thanks I got it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants