def __init__(self, file_path, make_opengl_textures=True): image_reader = QImageReader(file_path) self.is_animated = image_reader.supportsAnimation() if self.is_animated: self.num_frames = image_reader.imageCount() # -1 means loop infinitely, 0 means no loop, > 0 is finite # of loops self.loop_count = image_reader.loopCount() self.loops_remaining = 0 self.frames = [] self.delays = [] while image_reader.currentImageNumber() < image_reader.imageCount() - 1: self.frames.append(image_reader.read()) self.delays.append(image_reader.nextImageDelay()) if make_opengl_textures: self.open_gl_textures = [QOpenGLTexture(this_frame.mirrored()) for this_frame in self.frames] self.made_opengl_textures = True self.frames_and_delays = zip(self.frames, self.delays) self.current_frame = 0 self.animating = False else: self.image = image_reader.read() assert isinstance(self.image, QImage) if make_opengl_textures: self.open_gl_texture = QOpenGLTexture(self.image.mirrored()) self.made_opengl_textures = True
def load(self, source): """Load anything that QImageReader or QMovie constructors accept""" # Use QImageReader to identify animated GIFs for separate handling # (Thanks to https://stackoverflow.com/a/20674469/435253 for this) image_reader = QImageReader(source) from PyQt5.QtGui import QImageIOHandler if image_reader.supportsAnimation() and image_reader.imageCount() > 1: movie = QMovie(source) # Calculate the aspect ratio and adjust the widget size movie.jumpToFrame(0) movie_size = movie.currentImage().size() self.movie_aspect = movie_size.width() / movie_size.height() self.resizeEvent() self.label.setMovie(movie) movie.start() # Free memory if the previous image was non-animated self.orig_pixmap = None else: self.orig_pixmap = QPixmap(image_reader.read()) self.label.setPixmap(self.orig_pixmap) # Fail quickly if our violated invariants result in stale # aspect-ratio information getting reused self.movie_aspect = None # Keep the image from preventing downscaling self.setMinimumSize(1, 1)
def load(self, source, adaptSize=True): """Load anything that QImageReader or QMovie constructors accept adaptSize=True: Initial image size to fit container adaptSize=False: Set container's size the same as image """ # Use QImageReader to identify animated GIFs for separate handling # (Thanks to https://stackoverflow.com/a/20674469/435253 for this) size = QSize(self.width(), self.height()) image_reader = QImageReader(source) if image_reader.supportsAnimation() and image_reader.imageCount() > 1: # Set content as Movie self.content = SaneQMovie(source) # Adjust the widget size if adaptSize: self.content.adaptScale(size) else: # Resizing container will trigger resizeEvent() self.resize(self.content.size()) # Start Movie replay self.setMovie(self.content) self.content.start() else: # Set content as Image self.content = SaneQPixmap(image_reader.read()) # Adjust the widget size if adaptSize: self.setPixmap(self.content.adaptScale(size)) else: # Resizing container will trigger resizeEvent() self.resize(self.content.size()) self.setPixmap(self.content) # Keep the image from preventing downscaling self.setMinimumSize(1, 1)