Move convert_PIL_image_to_pixmap to the GUI thread

This commit is contained in:
Charles Haley 2024-01-26 20:35:44 +00:00
parent 3960e6a8d5
commit d6f5384b8a

View File

@ -1005,16 +1005,16 @@ class GridView(QListView):
def make_thumbnail(self, cover_tuple): def make_thumbnail(self, cover_tuple):
# Render the cover image data to the thumbnail size and correct format. # Render the cover image data to the thumbnail size and correct format.
# Rendering isn't needed if the cover came from the cache and the cache # Rendering isn't needed if the cover came from the cache and the cache
# is valid. Put a newly rendered image into the cache. Returns a # is valid. Put a newly rendered image into the cache. Returns the
# QPixmap. This method is called on the cover thread. # thumbnail as a PIL Image. This method is called on the cover thread.
cdata = cover_tuple.cdata cdata = cover_tuple.cdata
book_id = cover_tuple.book_id book_id = cover_tuple.book_id
tc = self.thumbnail_cache tc = self.thumbnail_cache
thumb = None # thumb = None
if cover_tuple.has_cover: if cover_tuple.has_cover:
# The book has a cover. Render the cover data as needed to get the # cdata contains either the resized thumbnail, the full cover.jpg,
# thumbnail that will be cached. # or None if cover.jpg isn't valid
if cdata.getbbox() is None and cover_tuple.cache_valid: if cdata.getbbox() is None and cover_tuple.cache_valid:
# Something wrong with the cover data in the cache. Remove it # Something wrong with the cover data in the cache. Remove it
# from the cache and render it again. # from the cache and render it again.
@ -1040,13 +1040,14 @@ class GridView(QListView):
# The PIL thumbnail operation works in-place, changing # The PIL thumbnail operation works in-place, changing
# the source image. # the source image.
cdata.thumbnail((int(nwidth), int(nheight))) cdata.thumbnail((int(nwidth), int(nheight)))
thumb = cdata
# Put the new thumbnail into the cache. # Put the new thumbnail into the cache.
try: try:
with BytesIO() as buf: with BytesIO() as buf:
cdata.save(buf, format=CACHE_FORMAT) cdata.save(buf, format=CACHE_FORMAT)
# use getbuffer() instead of getvalue() to avoid a copy # use getbuffer() instead of getvalue() to avoid a copy
tc.insert(book_id, cover_tuple.timestamp, buf.getbuffer()) tc.insert(book_id, cover_tuple.timestamp, buf.getbuffer())
thumb = convert_PIL_image_to_pixmap(cdata) thumb = cdata
except Exception: except Exception:
tc.invalidate((book_id,)) tc.invalidate((book_id,))
import traceback import traceback
@ -1055,22 +1056,27 @@ class GridView(QListView):
# The cover data isn't valid. Remove it from the cache # The cover data isn't valid. Remove it from the cache
tc.invalidate((book_id,)) tc.invalidate((book_id,))
else: else:
# The data from the cover cache is valid. the QPixmap to pass to # The data from the cover cache is valid and is already a thumb.
# the GUI thumb = cdata
thumb = convert_PIL_image_to_pixmap(cdata) else:
elif cover_tuple.cache_valid is not None: # The book doesn't have a cover.
# Cover was removed, but it exists in cache. Remove it from the cache if cover_tuple.cache_valid is not None:
tc.invalidate((book_id,)) # Cover was removed, but it exists in cache. Remove it from the cache
# Return the thumbnail, which is either None or a QPixmap. This can tc.invalidate((book_id,))
# result in putting None into the cache so re-rendering doesn't try thumb = None
# again. # Return the thumbnail, which is either None or a PIL Image. If not None
# the image will be converted to a QPixmap on the GUI thread. Putting
# None into the CoverCache ensures re-rendering won't try again.
return thumb return thumb
def re_render(self, book_id, thumb): def re_render(self, book_id, thumb):
# This is called on the GUI thread when a cover thumbnail is not in the # This is called on the GUI thread when a cover thumbnail is not in the
# CoverCache. The parameter "thumb" is None if there is no cover or a # CoverCache. The parameter "thumb" is either None if there is no cover
# QPixmap of the correctly scaled cover # or a PIL Image of the thumbnail.
self.delegate.cover_cache.clear_staging() self.delegate.cover_cache.clear_staging()
if thumb is not None:
# Convert the image to a QPixmap
thumb = convert_PIL_image_to_pixmap(thumb)
self.delegate.cover_cache.set(book_id, thumb) self.delegate.cover_cache.set(book_id, thumb)
m = self.model() m = self.model()
try: try: