TinyMCE and Flask - Part 2 - Managing Images
Posted on 2019-11-18T09:42:21Z by Kevin Foong
This is the second part of my earlier Flask / TinyMCE tutorial. See the first part here.
In this blog post I will outline the steps I used to save a thumbnail version of any image I upload to my post. Here I used the very useful Pillow (Python Imaging Library) library.
TinyMCE has a great image uploader feature where you can simply drag and drop images to directly upload into your post. I have summarised some of the main implementation points below:
imageuploaderfunction handles the image upload process.
- We obtain the image itself via
file = request.files.get('file')
- We truncate the filename to 30 characters.
- We only save the file if the image is a jpg, gif or png file by checking its file extension.
- We obtain the actual file size using
- We use the Pillow library to get the image dimensions.
- We use the Pillow to make a thumbnail copy of the image and save it in a separate thumbnail folder.
- We also save all details into a MySQL database.
The full source code is shown below.
You can also create an admin page to manage all the images, as I have done below. This page also has the ability to delete images which will delete the actual image file, its thumbnail and the database record.
Let me know if you have any feedback!