Python Qt Image Viewer Application with Rotate Feature Included

in programming •  7 years ago 

output.gif

I have been building this Image Viewer in Python and I am now up to the point where it is already coming in useful.

I can browse back and forth, select in finder/file manager, and today I added the ability to rotate the picture on the screen.

Get the full code here in this Gist

Adding Rotate

The key additions were the button ...

rotate = QPushButton("Rotate", window)
rotate.clicked.connect(rotate_picture)
rotate.setShortcut(QtGui.QKeySequence.MoveToPreviousLine)
rotate.move(380, 0)

and this function ....

# rotate 90
def rotate_picture():
    global window, file, files, index, label
    file = files[index]
    picture = label.pixmap()
    transform = QTransform().rotate(90)
    picture = picture.transformed(transform)
    label.setPixmap(picture)
    label.setGeometry(QtCore.QRect(10, 40, picture.width(), picture.height()))
    window.resize(picture.width()+20, picture.height()+100)
    window.setWindowTitle(file)
    window.show()

Where before we have been using the filename to load the image from the file system, if I did that we would simply rotate 90 degrees then seem to stop working, because every rotate click/press would rotate the original 90 degrees.

Instead we take the existing label image and rotate that.

picture = label.pixmap()

The rest is easy, we create a 90 degree transform, apply it, then set the label image to this transformed version.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

I love this a lot