Images in Pytonista (fastest loading time)
I copied the code below from the fourm here. I have downloaded a lot of image files from the web using requests lib. They are in .jpg format. I just wanted to know if the code below is as fast as I can make it, or should I open each jpg and write it out as a PNG file instead? Or is there a more native way to store the pics on disk that don't require as much translation when trying to assign them to let's say ui.ImageView.image
f_dir = "/private/var/mobile/Containers/Data/Application/32F49CCB-1015-4E4A-B696-DFC5A77BBEAD/Documents/movie/movie_poster/" f_name = 'tt2209764.jpeg' try: import cStringIO as StringIO except ImportError: import StringIO import ui, Image def pil_to_ui(img): # code from pytonista fourms strio = StringIO.StringIO() img.save(strio, img.format) data = strio.getvalue() strio.close() return ui.Image.from_data(data) if __name__ == '__main__': img = ui.ImageView(frame = (0,0,540,576)) img.image = pil_to_ui(Image.open(f_dir + f_name)) img.present('sheet')
you don't need to open as a PIL image then convert! that means you are reading the image, converting and writing the image, then reading it again! instead, use
I would suggest, when you download the images originally, that you convert them to a smaller size, which might make it a lot faster to load again.
@JonB, thanks. Made a big difference. I didn't time it, but took out the jerkiness out of the scrolling with many images being loaded on the page. I will try to figure out optimum size I need for display and correct my code for the download. I will also write a function to re-write the files I already have.
But given the downloads are a batch function in this case, would it be better for me to re save them as PNG files. I see there are a number of PNG implementations. I assume, the PNG file will be bigger than a JPEG, but I also assume decoding a PNG file is quicker. But I know, it can go very wrong assuming