Skip to content

Commit

Permalink
changed to have temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Benes committed Jan 28, 2024
1 parent e98e8f0 commit 24b3d92
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/jpeglib/spatial_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from dataclasses import dataclass
import numpy as np
import os
import pathlib
import struct
import tempfile
from typing import Union, List
Expand Down Expand Up @@ -222,24 +223,25 @@ def unquantized_coefficients(
assert self.samp_factor == '4:4:4' or (self.samp_factor == 1).all()
assert version.get() in version.LIBJPEG_VERSIONS
# write to temporary files
dst = tempfile.NamedTemporaryFile('w', suffix='.jpeg', delete=False)
dst_uq = tempfile.NamedTemporaryFile('w', suffix='.bin', delete=False)
dst.close()
dst_uq.close()
tmp_dir = pathlib.Path(tempfile.mkdtemp())
dst = str(tmp_dir / 'dst.jpeg')
dst_uq = str(tmp_dir / 'uq.bin')
# dst = tempfile.NamedTemporaryFile('w', suffix='.jpeg', delete=False)
# dst_uq = tempfile.NamedTemporaryFile('w', suffix='.bin', delete=False)
# dst.close()
# dst_uq.close()
self.write_spatial(
path=dst.name,
path=dst, #.name,
dct_method=dct_method,
dst_unquantized=dst_uq.name,
dst_unquantized=dst_uq, #.name,
flags=flags
)

# extract unquantized coefficients
with open(dst_uq.name, 'rb') as fp:
with open(dst_uq, 'rb') as fp:
content = fp.read()
os.remove(dst.name)
os.remove(dst_uq.name)
del dst
del dst_uq
os.remove(dst)
os.remove(dst_uq)
uq = struct.unpack(f'<{len(content)//4}f', content)
uq = np.reshape(uq, (
self.height//8, self.width//8,
Expand Down

0 comments on commit 24b3d92

Please sign in to comment.