def add_book(self, path, collection=None): """Add the archive at <path> to the library. If <collection> is not None, it is the collection that the books should be put in. Return True if the book was successfully added (or was already added). """ path = os.path.abspath(path) name = os.path.basename(path) info = archive.get_archive_info(path) if info is None: return False format, pages, size = info thumbnail.get_thumbnail(path, create=True, dst_dir=_cover_dir) old = self._con.execute('''select id from Book where path = ?''', (path,)).fetchone() try: if old is not None: self._con.execute('''update Book set name = ?, pages = ?, format = ?, size = ? where path = ?''', (name, pages, format, size, path)) else: self._con.execute('''insert into Book (name, path, pages, format, size) values (?, ?, ?, ?, ?)''', (name, path, pages, format, size)) except dbapi2.Error: print '! Could not add book %s to the library' % path return False if collection is not None: book = self._con.execute('''select id from Book where path = ?''', (path,)).fetchone() self.add_book_to_collection(book, collection) return True
def get_thumbnail(self, page=None, width=128, height=128, create=False): """Return a thumbnail pixbuf of <page> that fit in a box with dimensions <width>x<height>. Return a thumbnail for the current page if <page> is None. If <create> is True, and <width>x<height> <= 128x128, the thumbnail is also stored on disk. """ self._wait_on_page(page) path = self.get_path_to_page(page) if width <= 128 and height <= 128: thumb = thumbnail.get_thumbnail(path, create) else: try: if "gif" not in path[-3:].lower(): thumb = gtk.gdk.pixbuf_new_from_file_at_size(path, width, height) else: thumb = gtk.gdk.PixbufAnimation(path).get_static_image() src_width = thumb.get_width() src_height = thumb.get_height() if float(src_width) / width > float(src_height) / height: thumb = thumb.scale_simple(width, int(max(src_height * width / src_width, 1)), gtk.gdk.INTERP_TILES) else: thumb = thumb.scale_simple(int(max(src_width * height / src_height, 1)), height, gtk.gdk.INTERP_TILES) except Exception: thumb = None if thumb is None: thumb = self._get_missing_image() thumb = image.fit_in_rectangle(thumb, width, height) return thumb
def get_thumbnail(self, page=None, width=128, height=128, create=False): """Return a thumbnail pixbuf of <page> that fit in a box with dimensions <width>x<height>. Return a thumbnail for the current page if <page> is None. If <create> is True, and <width>x<height> <= 128x128, the thumbnail is also stored on disk. """ self._wait_on_page(page) path = self.get_path_to_page(page) if width <= 128 and height <= 128: thumb = thumbnail.get_thumbnail(path, create) else: try: if "gif" not in path[-3:].lower(): thumb = gtk.gdk.pixbuf_new_from_file_at_size( path, width, height) else: thumb = gtk.gdk.PixbufAnimation(path).get_static_image() src_width = thumb.get_width() src_height = thumb.get_height() if float(src_width) / width > float(src_height) / height: thumb = thumb.scale_simple( width, int(max(src_height * width / src_width, 1)), gtk.gdk.INTERP_TILES) else: thumb = thumb.scale_simple( int(max(src_width * height / src_height, 1)), height, gtk.gdk.INTERP_TILES) except Exception: thumb = None if thumb is None: thumb = self._get_missing_image() thumb = image.fit_in_rectangle(thumb, width, height) return thumb
def add_extra_image(self, path): """Add an imported image (at <path>) to the end of the image list.""" thumb = thumbnail.get_thumbnail(path, create=False) if thumb is None: thumb = self.render_icon(Gtk.STOCK_MISSING_IMAGE, Gtk.IconSize.DIALOG) thumb = image.fit_in_rectangle(thumb, 67, 100) thumb = image.add_border(thumb, 1, 0x555555FF) self._liststore.append([thumb, os.path.basename(path), path])
def add_extra_image(self, path): """Add an imported image (at <path>) to the end of the image list.""" thumb = thumbnail.get_thumbnail(path, create=False) if thumb is None: thumb = self.render_icon(gtk.STOCK_MISSING_IMAGE, gtk.ICON_SIZE_DIALOG) thumb = image.fit_in_rectangle(thumb, 67, 100) thumb = image.add_border(thumb, 1, 0x555555FF) self._liststore.append([thumb, os.path.basename(path), path])
def run(self): if self._thumb is None: self._thumb = thumbnail.get_thumbnail(self._path) image = wx.Image(self._thumb) image.Resize(THUMBNAIL_SIZE, ( (THUMBNAIL_SIZE[0] - image.GetWidth()) / 2, (THUMBNAIL_SIZE[1] - image.GetHeight()) / 2, )) if self._notify_window: wx.PostEvent(self._notify_window, ThumbnailLoadEvent(data=image))
def update_thumbnails(nodes, progress=None): totalsize = sum([node.size for node in nodes]) if progress: progress.set(total=totalsize) for node in nodes: nm = node.abspath() node.thumbnail = get_thumbnail(path=nm) if node.thumbnail: node.thumbnail.from_file() if progress: progress.update(node.size, info=nm)
def get_file_thumbnail(filename): logging.debug('Thumbnail for %s',filename) t = get_thumbnail(path=filename) if t is not None: try: t.from_file() except: t = None if filedataglobal.status: filedataglobal.status.incbytes(1) return filename, t
def get_book_cover(self, book): """Return a pixbuf with a thumbnail of the cover of <book>, or None if the cover can not be fetched. """ try: path = self._con.execute('''select path from Book where id = ?''', (book,)).fetchone() except Exception: print '! Non-existant book #%d' % book return None thumb = thumbnail.get_thumbnail(path, create=True, dst_dir=_cover_dir) if thumb is None: print '! Could not get cover for %s' % path return thumb
def from_hdf5(self, h5gp): self.size = h5gp.attrs['Size'] self.modified = time.struct_time(h5gp.attrs['Modified']) self.islink = h5gp.attrs["IsLink"] ## TODO: process links better here if "Hash" in h5gp: self.hashval = np.zeros(HASH_FUNCTION().digest_size, dtype='uint8') self.hashval = h5gp["Hash"][:,-1] else: self.hashval = None self.thumbnail = get_thumbnail(h5obj=h5gp)
def _update_preview(self, *args): path = self.filechooser.get_preview_filename() if path and os.path.isfile(path): pixbuf = thumbnail.get_thumbnail(path, prefs['create thumbnails']) if pixbuf is None: self._preview_image.clear() self._namelabel.set_text('') self._sizelabel.set_text('') else: pixbuf = image.add_border(pixbuf, 1) self._preview_image.set_from_pixbuf(pixbuf) self._namelabel.set_text(encoding.to_unicode( os.path.basename(path))) self._sizelabel.set_text( '%.1f KiB' % (os.stat(path).st_size / 1024.0)) else: self._preview_image.clear() self._namelabel.set_text('') self._sizelabel.set_text('')
def _update_preview(self, *args): path = self.filechooser.get_preview_filename() if path and os.path.isfile(path): pixbuf = thumbnail.get_thumbnail(path, prefs['create thumbnails']) if pixbuf is None: self._preview_image.clear() self._namelabel.set_text('') self._sizelabel.set_text('') else: pixbuf = image.add_border(pixbuf, 1) self._preview_image.set_from_pixbuf(pixbuf) self._namelabel.set_text( encoding.to_unicode(os.path.basename(path))) self._sizelabel.set_text('%.1f KiB' % (os.stat(path).st_size / 1024.0)) else: self._preview_image.clear() self._namelabel.set_text('') self._sizelabel.set_text('')
def get_thumbnail(self, page=None, width=128, height=128, create=False): """Return a thumbnail pixbuf of <page> that fit in a box with dimensions <width>x<height>. Return a thumbnail for the current page if <page> is None. If <create> is True, and <width>x<height> <= 128x128, the thumbnail is also stored on disk. """ self._wait_on_page(page) path = self.get_path_to_page(page) if width <= 128 and height <= 128: thumb = thumbnail.get_thumbnail(path, create) else: try: thumb = gtk.gdk.pixbuf_new_from_file_at_size( path, width, height) except Exception: thumb = None if thumb is None: thumb = self._get_missing_image() thumb = image.fit_in_rectangle(thumb, width, height) return thumb
def get_thumbnail(self, page=None, width=128, height=128, create=False): """Return a thumbnail pixbuf of <page> that fit in a box with dimensions <width>x<height>. Return a thumbnail for the current page if <page> is None. If <create> is True, and <width>x<height> <= 128x128, the thumbnail is also stored on disk. """ self._wait_on_page(page) path = self.get_path_to_page(page) if width <= 128 and height <= 128: thumb = thumbnail.get_thumbnail(path, create) else: try: thumb = gtk.gdk.pixbuf_new_from_file_at_size(path, width, height) except Exception: thumb = None if thumb is None: thumb = self._get_missing_image() thumb = image.fit_in_rectangle(thumb, width, height) return thumb
def get_file(self, index): if self.file_buffer[index] is not None: return self.file_buffer[index] connection = database.get_current_gallery("connection") c = connection.cursor() c.execute( "SELECT uuid, file_name FROM file WHERE pk_id = ?", (self.files[index],), ) result = c.fetchone() path = os.path.join( database.get_current_gallery("directory"), "files", result[0], ) thumb_path = thumbnail.get_thumbnail(self.files[index], path_only=True) if thumb_path is not None: path = thumb_path self.file_buffer[index] = (result[1], path) return self.file_buffer[index]