- Python Multimedia
- Ninad Sathaye
- 840字
- 2025-03-31 06:21:37
Time for action – developing image processing code
Thus, with ThumbnailMakerDialog
at your disposal, you can develop your own code in scratch, in class ThumbnailMaker
. Just make sure to implement the method processImage
as this is the only method called by ThumbnailMakerDialog
.
Let's develop some important methods of class ThumbnailMaker
.
- Write the constructor for class
ThumbnailMaker
. It takesdialog
as an argument. In the constructor, we only initializeself._dialog
, which is an instance of classThumbnailMakerDialog
. Here is the code.def __init__(self, dialog): """ Constructor for class ThumbnailMaker. """ # This dialog can be an instance of # ThumbnailMakerDialog class. Alternatively, if # you have some other way to process input,
# it will be that class. Just make sure to implement # the public API methods defined in # ThumbnailMakerDialog class! self._dialog = dialog
- Next, write the
processImage
method in classThumbnailMaker
. The code is as follows:Tip
Note: You can download the file
ThumbnailMaker.py
from Packt website. The code written is from this file. The only difference is that some code comments are removed here.1 def processImage(self): 2 filePath = self._dialog.getInputImagePath() 3 imageFile = Image.open(filePath) 4 5 if self._dialog.maintainAspectRatio: 6 resizedImage = self._makeThumbnail(imageFile) 7 else: 8 resizedImage = self._resizeImage(imageFile) 9 10 rotatedImage = self._rotateImage(resizedImage) 11 12 fullPath = self._dialog.getOutImagePath() 13 14 # Finally save the image. 15 rotatedImage.save(fullPath)
- On line 2, it gets the full path of the input image file. Note that it relies on
self._dialog
to provide this information. - Then the image file is opened the usual way. On line 4, it checks a flag that decides whether or not to process the image by maintaining the aspect ratio. Accordingly,
_makeThumbnail
or_resizeImage
methods are called. - On line 10, it rotates the image resized earlier, using the
_rotateImage
method. - Finally, on line 15, the processed image is saved at a path obtained from the
getOutImagePath
method of classThumbnailMakerDialog
. - We will now write the
_makeThumbnail
method.1 def _makeThumbnail(self, imageFile): 2 foo = imageFile.copy() 3 size = self._dialog.getSize() 4 imageFilter = self._getImageFilter() 5 foo.thumbnail(size, imageFilter) 6 return foo
- First a copy of the original image is made. We will manipulate this copy and the method will return it for further processing.
- Then the necessary parameters such as the image dimension and filter for re-sampling are obtained from
self._dialog
and_getImageFilter
respectively. - Finally the thumbnail is created on line 5 and then method returns this image instance.
- We have already discussed how to resize and rotate image. The related code is straightforward to write and the readers are suggested to write it as an exercise. You will need to review the code from file
ThumbnailMakerDialog.py
for getting appropriate parameters. Write remaining routines namely,_resizeImage,
_rotateImage
and_getImageFilter.
- Once all methods are in place, run the code from the command line as:
python Thumbnailmaker.py
- It should show our application dialog. Play around with it to make sure everything works!
What just happened?
In the previous section, we completed an exciting project. Several things learned in this chapter, such as image I/O, resizing, and so on, were applied in the project. We developed a GUI application where some basic image manipulation features, such as creating thumbnails, were implemented. This project also helped us gain some insight into various aspects of GUI programming using QT.
Have a go hero – enhance the ThumbnailMaker application
Want to do something more with the Thumbnail Maker. Here you go! As you will add more features to this application, the first thing you would need to do is to change its name—at least from the caption of the dialog that pops up! Edit the thumbnailMaker.ui
file in QT designer, change the name to something like "Image Processor", and recreate the corresponding .py
file. Next, add the following features to this application.
Tip
If you don't want to deal with any UI code, that is fine too! You can write a class similar to ThumbnailMakerDialog
. Do the input argument processing in your own way. All that class ThumbnailMaker
requires is implementation of certain public methods in this new class, to get various input parameters.
- Accept output filename from the user. Currently, it gives the same name as the input file.
Edit the
.ui
file. You would need to break the layouts before adding aQLineEdit
and itsQLabel
and then recreate the layouts. - If there is a previously created output image file in the output directory, clicking OK would simply overwrite that file. Add a checkbox reading, "Overwrite existing file (if any)". If the checkbox in deselected, it should pop up a warning dialog and exit.
For the latter part, there is a commented out code block in
ThumbnailMakerDialog._processImage.
Just enable the code. - Add a feature that can add specified text in the lower-left corner of the output image.
- Create an image with this text, and use the combination of crop and paste to achieve desired results. For user input, you will need to add a new
QLineEdit
for accepting text input and then connect signals with a callable method inThumbnailMakerDialog._connect
.