Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4104

Camera board • Re: Picamera2 - How to rotate image 90, 180, 270 degrees?

$
0
0
I ended up using the piexif and PIL libraries. I rotate the image immediately after successful capture.

This is the code for anyone's future reference.

The method expects the file path of the image you just captured and a clockwise rotation. PIL oddly uses counterclockwise rotation, so at first glance it maybe a bit confusing.

Code:

def rotateImage(filePath, angle):try:image = Image.open(filePath)EXIFData = piexif.load(filePath)newOrientation = 1if angle == 90:newOrientation = 6image = image.rotate(-90, expand=True)elif angle == 180:newOrientation = 3image = image.rotate(180, expand=True)elif angle == 270:newOrientation = 8image = image.rotate(90, expand=True)EXIFData['Orientation'] = newOrientationEXIFBytes = piexif.dump(EXIFData)image.save(filePath, exif=EXIFBytes)except Exception as ex:print('Could not rotate ' + filePath + ' ' + str(angle) + ' degrees. ' + str(ex))pass

Statistics: Posted by eat-sleep-code — Sat Jan 27, 2024 3:46 pm



Viewing all articles
Browse latest Browse all 4104

Trending Articles