Skip to content

Commit 8fee04c

Browse files
Copilotletmaik
andcommitted
Refactor: create set_unpack_params() method instead of adding shot_select to open methods
Co-authored-by: letmaik <530988+letmaik@users.noreply.github.com>
1 parent cb32aa3 commit 8fee04c

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

rawpy/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def imread(pathOrFile, shot_select=0):
1717
"""
1818
d = RawPy()
1919
if hasattr(pathOrFile, 'read'):
20-
d.open_buffer(pathOrFile, shot_select=shot_select)
20+
d.open_buffer(pathOrFile)
2121
else:
22-
d.open_file(pathOrFile, shot_select=shot_select)
22+
d.open_file(pathOrFile)
23+
d.set_unpack_params(shot_select=shot_select)
2324
return d

rawpy/_rawpy.pyx

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -411,15 +411,13 @@ cdef class RawPy:
411411
with nogil:
412412
self.p.recycle()
413413

414-
def open_file(self, path, shot_select=0):
414+
def open_file(self, path):
415415
"""
416416
Opens the given RAW image file. Should be followed by a call to :meth:`~rawpy.RawPy.unpack`.
417417
418418
.. NOTE:: This is a low-level method, consider using :func:`rawpy.imread` instead.
419419
420420
:param str path: The path to the RAW image.
421-
:param int shot_select: select which image to extract from RAW files that contain multiple images
422-
(e.g., Dual Pixel RAW). Default is 0 for the first/main image.
423421
"""
424422
cdef wchar_t *wchars
425423
cdef Py_ssize_t wchars_len
@@ -435,19 +433,14 @@ cdef class RawPy:
435433
ELSE:
436434
res = self.p.open_file(path.encode('UTF-8'))
437435
self.handle_error(res)
438-
# Set shot_select after opening file
439-
cdef libraw_raw_unpack_params_t* rp = &self.p.imgdata.rawparams
440-
rp.shot_select = shot_select
441436

442-
def open_buffer(self, fileobj, shot_select=0):
437+
def open_buffer(self, fileobj):
443438
"""
444439
Opens the given RAW image file-like object. Should be followed by a call to :meth:`~rawpy.RawPy.unpack`.
445440
446441
.. NOTE:: This is a low-level method, consider using :func:`rawpy.imread` instead.
447442
448443
:param file fileobj: The file-like object.
449-
:param int shot_select: select which image to extract from RAW files that contain multiple images
450-
(e.g., Dual Pixel RAW). Default is 0 for the first/main image.
451444
"""
452445
self.unpack_called = False
453446
self.unpack_thumb_called = False
@@ -458,7 +451,19 @@ cdef class RawPy:
458451
with nogil:
459452
e = self.p.open_buffer(buf, buf_len)
460453
self.handle_error(e)
461-
# Set shot_select after opening buffer
454+
455+
def set_unpack_params(self, shot_select=0):
456+
"""
457+
Set parameters that affect RAW image unpacking.
458+
459+
This should be called after opening a file and before unpacking.
460+
461+
.. NOTE:: This is a low-level method. When using :func:`rawpy.imread`,
462+
unpack parameters can be provided directly.
463+
464+
:param int shot_select: select which image to extract from RAW files that contain multiple images
465+
(e.g., Dual Pixel RAW). Default is 0 for the first/main image.
466+
"""
462467
cdef libraw_raw_unpack_params_t* rp = &self.p.imgdata.rawparams
463468
rp.shot_select = shot_select
464469

test/test_shot_select.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ def test_shot_select_nonexistent_image():
2727

2828

2929
def test_shot_select_via_open_file():
30-
"""Test that shot_select can be passed via open_file"""
30+
"""Test that shot_select can be set via set_unpack_params"""
3131
raw = rawpy.RawPy()
32-
raw.open_file(rawTestPath, shot_select=0)
32+
raw.open_file(rawTestPath)
33+
raw.set_unpack_params(shot_select=0)
3334
raw.unpack()
3435
rgb = raw.postprocess()
3536
assert rgb is not None

0 commit comments

Comments
 (0)