pdf2dcm icon indicating copy to clipboard operation
pdf2dcm copied to clipboard

[RFE] Allow to pass file like object

Open rpitonak opened this issue 2 years ago • 3 comments

Hello,

First of all, great work with the package, it is really helpful. I have a suggestion for improvement. When working with remote storage, using for example RaRe-Technologies/smart_open, or when you accept Dicom in a web application through the form, you may have the file(s) already in some variable, and to store them on the disk and pass the path may be a bit cumbersome.

pydicom is able to work with file-like object and does not need a file path, e.g

import pydicom
source_dcm = pydicom.dcmread(io.BytesIO(data.getvalue()))

It would be nice to have pdf2dcm API ready for such case. First shot at how it could look like

from pdf2dcm import Pdf2EncapsDCM
converter = Pdf2EncapsDCM()
encapsulated_pdf = converter.run(
    pdf=io.BytesIO(b"My pdf"), template_dcm=pydicom.dcmread(io.BytesIO(data.getvalue()), return_ds=True)
)

return_ds would prevent the file from being saved on the disk and it would return pydicom Dataset because sometimes you want to, for example, save some metadata to the database and you need to read the file repeatedly. Also, template_dcm could accept pydicom Dataset as well.

I would be glad for your feedback on these suggestions and I am willing to open the PR afterward. Of course, I will understand if this functionality is out of the scope of this package, but I think more people could benefit from it.

I am looking forward to your response!

Rado.

rpitonak avatar Sep 04 '22 18:09 rpitonak

Hi @rpitonak!

Thanks for your interest in the project and for suggesting changes to improve the current API. I feel it is a good feature to have.

In terms of the implementation maybe when initiating the class you could specify the output as a stream converter = Pdf2EncapsDCM(return_stream=True) and then you could modify the store function to return the stream. Check https://github.com/a-parida12/pdf2dcm/blob/359e7e52699d7496524530d35719ba37132491bc/pdf2dcm/base.py#L93

Let's focus on the Pdf2EncapsDCM as it may be easier than the Secondary Capture one. What do you think?

Looking forward to a PR from you.

a-parida12 avatar Sep 04 '22 19:09 a-parida12

Hello @a-parida12,

Thanks for pointing me to the store function, makes sense to do that modification.

Let's focus on the Pdf2EncapsDCM as it may be easier than the Secondary Capture one. What do you think?

Agreed

I will open the PR soon.

rpitonak avatar Sep 05 '22 05:09 rpitonak

@rpitonak did you get a chance to look at the feature request?

a-parida12 avatar Mar 13 '23 14:03 a-parida12