{"action":"opened","issue":{"url":"https://api.github.com/repos/pydicom/pydicom/issues/753","repository_url":"https://api.github.com/repos/pydicom/pydicom","labels_url":"https://api.github.com/repos/pydicom/pydicom/issues/753/labels{/name}","comments_url":"https://api.github.com/repos/pydicom/pydicom/issues/753/comments","events_url":"https://api.github.com/repos/pydicom/pydicom/issues/753/events","html_url":"https://github.com/pydicom/pydicom/issues/753","id":365550628,"node_id":"MDU6SXNzdWUzNjU1NTA2Mjg=","number":753,"title":"Cannot use gzip file-like in `dcmwrite`","user":{"login":"alistairewj","id":1282676,"node_id":"MDQ6VXNlcjEyODI2NzY=","avatar_url":"https://avatars1.githubusercontent.com/u/1282676?v=4","gravatar_id":"","url":"https://api.github.com/users/alistairewj","html_url":"https://github.com/alistairewj","followers_url":"https://api.github.com/users/alistairewj/followers","following_url":"https://api.github.com/users/alistairewj/following{/other_user}","gists_url":"https://api.github.com/users/alistairewj/gists{/gist_id}","starred_url":"https://api.github.com/users/alistairewj/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alistairewj/subscriptions","organizations_url":"https://api.github.com/users/alistairewj/orgs","repos_url":"https://api.github.com/users/alistairewj/repos","events_url":"https://api.github.com/users/alistairewj/events{/privacy}","received_events_url":"https://api.github.com/users/alistairewj/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-10-01T16:53:39Z","updated_at":"2018-10-01T16:53:39Z","closed_at":null,"author_association":"NONE","body":"#### Description\r\n\r\nWhen attempting to directly output a compressed dicom file with gzip (i.e. `with gzip.open('filename.dcm.gz', 'w')`, an OSError exception is raised. Looks like it's because the filewriter function can seek a negative value (e.g. [line 686 of filewriter.py](https://github.com/pydicom/pydicom/blob/07342bfa91d311023332f21ea7cd7a22e989d0e7/pydicom/filewriter.py#L686)), and you can't do negative seeks when writing compressed files. Not sure if this is a bug report or a feature request!\r\n\r\n#### Steps/Code to Reproduce\r\n\r\nThis is slightly modified from https://pydicom.github.io/pydicom/stable/auto_examples/input_output/plot_write_dicom.html\r\n\r\n```py\r\nimport os\r\nimport tempfile\r\nimport gzip\r\n\r\nimport pydicom\r\nfrom pydicom.dataset import Dataset, FileDataset\r\n\r\n# Create temporary filename\r\nfilename_little_endian = tempfile.NamedTemporaryFile(suffix='.dcm').name\r\n\r\nfile_meta = Dataset()\r\n# Setting the below is necessary to throw the exception\r\nfile_meta.FileMetaInformationGroupLength = 0\r\n\r\nds = FileDataset(filename_little_endian, {},\r\n                 file_meta=file_meta, preamble=b\"\\0\" * 128)\r\n\r\nprint(\"Throws error....\")\r\nwith gzip.open(filename_little_endian, 'w') as fp:\r\n    ds.save_as(fp)\r\n```\r\n\r\n#### Expected Results\r\n\r\nNo error is thrown and a gzipped DICOM is output.\r\n\r\n#### Actual Results\r\n\r\n```\r\nTraceback (most recent call last):\r\n  File \"test-dicom-gz.py\", line 27, in <module>\r\n    ds.save_as(fp)\r\n  File \"/home/alistairewj/.virtualenvs/eddata/lib/python3.5/site-packages/pydicom/dataset.py\", line 1048, in save_as\r\n    pydicom.dcmwrite(filename, self, write_like_original)\r\n  File \"/home/alistairewj/.virtualenvs/eddata/lib/python3.5/site-packages/pydicom/filewriter.py\", line 868, in dcmwrite\r\n    enforce_standard=not write_like_original)\r\n  File \"/home/alistairewj/.virtualenvs/eddata/lib/python3.5/site-packages/pydicom/filewriter.py\", line 670, in write_file_meta_info\r\n    fp.seek(end_group_length_elem - 12)\r\n  File \"/usr/lib/python3.5/gzip.py\", line 358, in seek\r\n    raise OSError('Negative seek in write mode')\r\n```\r\n\r\n#### Versions\r\n\r\n* Ubuntu 16.04\r\n* Python 3.5.2\r\n* pydicom 1.1.0"}}
{"actor":{"display_login":"alistairewj"}}