-
Notifications
You must be signed in to change notification settings - Fork 68
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
Add support for extracting thumbnails #98
Conversation
@harshithdwivedi @dtakeshta Could you give this a try? You can download the wheels by clicking "Show all checks", then clicking on the right variant, and then on the right click on "Artifacts". E.g. Windows 64bit Python 3.7 is here: https://github.com/letmaik/rawpy/pull/98/checks?check_run_id=401958378 Usage is like that: with rawpy.imread('image.nef') as raw:
thumb = raw.extract_thumb() # returns (format: enum, data: bytes) namedtuple
assert thumb.format == rawpy.ThumbFormat.JPEG
with open('thumb.jpg') as fp:
f.write(thumb.data) Does this make sense? |
Hey @letmaik thanks for this! The time taken to extract the jpeg previews from 6 raw files is as follows: I'm not sure if this overhead is added by python or if there's something else that's going on. Also, in case the embedded raw doesn't exist, will extract_thumb() return a None object? |
I looked at the libraw api examples, and looks like the sample code here emulates the behaviour of |
Thanks for trying this. I will have a look at performance, I'm assuming it's to do with that we unpack both the raw and the thumb, whereas dcraw would only do the latter. If there's no thumbnail or an unsupported format, then an exception is raised. Currently there's no easy way to distinguish between both cases, like in any other libraw error condition. If you need to handle them differently I could expose the error code via named constants in the exception object. |
Yeah, that'd be great! |
I added support for bitmap thumbnails (which are returned as RGB ndarray) and expose libraw errors now. A semi-complete example from the docstring: with rawpy.imread('image.nef') as raw:
try:
thumb = raw.extract_thumb()
except rawpy.LibRawNoThumbnailError:
print('no thumbnail found')
except rawpy.LibRawUnsupportedThumbnailError:
print('unsupported thumbnail')
else:
if thumb.format == rawpy.ThumbFormat.JPEG:
with open('thumb.jpg') as f:
f.write(thumb.data)
elif thumb.format == rawpy.ThumbFormat.BITMAP:
imageio.imsave('thumb.tiff', thumb.data) Let me know what you think. |
I just used the updated wheel again and it seems to have a lower performance than the earlier one. Here's the code I have: def raw_to_jpeg(self, raw_path, file_name, path_for_jpeg):
# create the final destination
new_file_path = pathlib.Path(path_for_jpeg).joinpath(
"{}.jpg".format(file_name))
try:
raw = rawpy.imread(raw_path)
try:
thumb = raw.extract_thumb()
if thumb.format == rawpy.ThumbFormat.JPEG:
print("jpeg")
with open(new_file_path, 'wb') as f:
f.write(thumb.data)
elif thumb.format == rawpy.ThumbFormat.BITMAP:
print("bitmap")
cv2.imwrite(new_file_path, thumb.data)
except (rawpy.LibRawNoThumbnailError, rawpy.LibRawUnsupportedThumbnailError):
# no embedded thumb, so convert the raw to jpeg
print("None")
rgb = raw.postprocess()
cv2.imwrite(new_file_path, rgb)
except rawpy._rawpy.LibRawNonFatalError:
pass I only see "jpeg" printed on my terminal, so it's certain that the library is extracting the embedded jepg only. |
I haven't looked at the performance issue yet, but my latest changes wouldn't have any impact. This was only about the bitmap support and errors. |
Ok, got it! |
@harshithdwivedi I fixed the performance issue. Can you try again? |
Perfect! It works like a charm now. |
Implements #96.