昨晚上ADSL居然斷線,鬱悶中無所事事。看到以前下載的PyGame庫於是開啟看看,覺得挺簡單的。
於是寫了個指令碼耍耍,呵呵
這是一個很簡單的圖片瀏覽程式,有興趣的自己看代碼吧。
## picView - A simple image viewer written using PyGame library.## Copyleft 2008 Bruce Jia#import osimport pygameimport pygame.imagefrom pygame import displaydef isImageFile(filePath): lowerFilePath = filePath.lower() if (lowerFilePath.endswith('.jpg') or lowerFilePath.endswith('.png') or lowerFilePath.endswith('.bmp') or lowerFilePath.endswith('.gif') or lowerFilePath.endswith('.jpeg')): return True return Falseclass ImageFolder: def CurrentImage(self): if self.index == -1 or len(self.images) == 0: return "" return self.images[self.index] def NextImage(self): if 0 == len(self.images) or self.index == len(self.images) - 1: return "" self.index = self.index + 1 return self.images[self.index] def PrevImage(self): if 0 == len(self.images) or self.index == -1: return "" self.index = self.index - 1 if self.index >= 0: return self.images[self.index] pass def __init__(self, folderPath): self.images = [] # TODO: add recursive search for file in os.listdir(folderPath): if (os.path.isfile(os.path.join(folderPath, file))): if (isImageFile(file)): self.images.append(os.path.join(folderPath, file)) self.index = -1 passdef ShowImageWithFitScale(screen, imageFilePath, angle=0): if (imageFilePath == None or imageFilePath == ""): return image = pygame.image.load(imageFilePath) scrWidth, scrHeight = screen.get_size() image = pygame.transform.rotate(image, angle) imgWidth, imgHeight = image.get_size() ratio = 1.0 * imgWidth / imgHeight if imgWidth > imgHeight: if imgWidth > scrWidth: imgWidth = scrWidth imgHeight = imgWidth / ratio if imgHeight > scrHeight: imgHeight = scrHeight imgWidth = imgHeight * ratio else: if imgHeight > scrHeight: imgHeight = scrHeight imgWidth = imgHeight * ratio if (imgWidth > scrWidth): imgWidth = scrWidth imgHeight = imgWidth / ratio image = pygame.transform.scale(image, (int(imgWidth), int(imgHeight))) posX = (scrWidth - imgWidth) / 2.0 posY = (scrHeight - imgHeight) / 2.0 screen.fill((0, 0, 0)) screen.blit(image, (posX, posY)) pygame.display.flip() if __name__ == "__main__": from sys import argv if(len(argv) < 2): print "Usage: picView.py " sys.exit(1) folderPath = argv[1] print "Showing pictures in " + folderPath imageFolder = ImageFolder(folderPath) display.init() screen = display.set_mode((0,0), pygame.FULLSCREEN) quit = False angle = 0 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: quit = True break if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: quit = True break elif event.key in [pygame.K_UP, pygame.K_PAGEUP, pygame.K_F7, pygame.K_BACKSPACE]: angle = 0 ShowImageWithFitScale(screen, imageFolder.PrevImage(), angle) elif event.key in [pygame.K_DOWN, pygame.K_PAGEDOWN, pygame.K_F8, pygame.K_RETURN, pygame.K_SPACE]: angle = 0 ShowImageWithFitScale(screen, imageFolder.NextImage(), angle) elif event.key == pygame.K_LEFT: angle = angle + 90 ShowImageWithFitScale(screen, imageFolder.CurrentImage(), angle) elif event.key == pygame.K_RIGHT: angle = angle - 90 ShowImageWithFitScale(screen, imageFolder.CurrentImage(), angle) elif event.key == pygame.K_F5: angle = 0 ShowImageWithFitScale(screen, imageFolder.CurrentImage(), angle) elif event.type == pygame.MOUSEBUTTONDOWN: ShowImageWithFitScale(screen, imageFolder.NextImage()) if quit: break print "Byebye!" display.quit()
Coding for fun!
del.icio.us Tags: python,pygame