| 1 | # Fl_JPEG_Image from memory buffer |
| 2 | # test program for pyFLTK the Python bindings |
| 3 | # for the Fast Light Tool Kit (FLTK). |
| 4 | # |
| 5 | # FLTK copyright 1998-1999 by Bill Spitzak and others. |
| 6 | # pyFLTK copyright 2003 by Andreas Held and others. |
| 7 | # |
| 8 | # This library is free software you can redistribute it and/or |
| 9 | # modify it under the terms of the GNU Library General Public |
| 10 | # License, version 2.0 as published by the Free Software Foundation. |
| 11 | # |
| 12 | # This library is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | # Library General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU Library General Public |
| 18 | # License along with this library if not, write to the Free Software |
| 19 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 20 | # USA. |
| 21 | # |
| 22 | # Please report all bugs and problems to "[email protected]". |
| 23 | # |
| 24 | |
| 25 | # cat image from https://commons.wikimedia.org/wiki/File:Arthur,_the_cat.jpg |
| 26 | |
| 27 | from fltk import * |
| 28 | from PIL import Image |
| 29 | import io |
| 30 | |
| 31 | def img_resize(fname,width): |
| 32 | '''resizes any image type using high quality PIL library''' |
| 33 | img = Image.open(fname) #opens all image formats supported by PIL |
| 34 | w,h = img.size |
| 35 | height = int(width*h/w) #correct aspect ratio |
| 36 | img = img.resize((width, height), Image.BICUBIC) #high quality resizing |
| 37 | mem = io.BytesIO() #byte stream memory object |
| 38 | img.save(mem, format="JPEG") #converts image type to JPEG byte stream |
| 39 | return Fl_JPEG_Image(None, mem.getbuffer()) |
| 40 | |
| 41 | pic = img_resize('cat.jpg', 300) #resizes to 300 pixels width |
| 42 | win = Fl_Window(pic.w(), pic.h(), 'PIL resizing') |
| 43 | win.begin() |
| 44 | box = Fl_Box(0, 0, pic.w(), pic.h()) |
| 45 | win.end() |
| 46 | |
| 47 | box.image(pic) |
| 48 | |
| 49 | win.show() |
| 50 | Fl.run() |
| 51 | |
| 52 |