question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

lose rotation info using this repo

See original GitHub issue

Now,i have a picture at cong

when i use

img = cv2.imread("cong.jpg")

the picture is been correctly displayed.But when i use

in_file = open("cong.jpg", 'rb')
img = jpeg.decode(in_file.read())
in_file.close()

the picture loses its rotation info which leads to wrong display. You can try and hope that you can fix this bug.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
lfxxcommented, Jun 23, 2021

Since numpy is not physically rotating the image in memory by simply changing the striding, if you want to get correct result after rotating, you need to call copy() before calling encode() function like below.

out_file = open('output.jpg', 'wb')
out_file.write(turbo_jpeg.encode(transposed_image.copy()))
out_file.close()

This is what i need!thank you!

1reaction
lfxxcommented, Jun 17, 2021

I assume you’re using Python 3 (not Python 2), here is a possible solution if the JPEG file is located on web server.

# This example shows how to use PyTurboJPEG with ExifRead to
# transpose an image if the image has an EXIF Orientation tag.
#
# pip install PyTurboJPEG -U
# pip install exifread -U

import cv2
import numpy as np
import exifread
import requests
from turbojpeg import TurboJPEG
from io import BytesIO

def transposeImage(image, orientation):
    """See Orientation in https://www.exif.org/Exif2-2.PDF for details."""
    if orientation == None: return image
    val = orientation.values[0]
    if val == 1: return image
    elif val == 2: return np.fliplr(image)
    elif val == 3: return np.rot90(image, 2)
    elif val == 4: return np.flipud(image)
    elif val == 5: return np.rot90(np.flipud(image), -1)
    elif val == 6: return np.rot90(image, -1)
    elif val == 7: return np.rot90(np.flipud(image))
    elif val == 8: return np.rot90(image)

# using default library installation
turbo_jpeg = TurboJPEG()
# download the jpeg file
file_content = requests.get('https://user-images.githubusercontent.com/27956643/122202251-7025a700-cecf-11eb-8f3c-f1f43a7f254f.jpg', stream=True, timeout=3600).content
# parse orientation
orientation = exifread.process_file(BytesIO(file_content)).get('Image Orientation', None)
# start to decode the JPEG file
image = turbo_jpeg.decode(file_content)
# transpose image based on EXIF Orientation tag
transposed_image = transposeImage(image, orientation)

cv2.imshow('transposed_image', transposed_image)
cv2.waitKey(0)

It works!thanks.This repo is still 1x faster than opencv though added so much ops.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Focal Loss for Dense Rotation Object Detection - GitHub
This repo is based on Focal Loss for Dense Object Detection, and it is completed by YangXue. We also recommend a tensorflow-based rotation...
Read more >
RecyclerView loses state on rotation - android - Stack Overflow
When you rotate your device, the view (fragment) is destroyed and re-created, but the ViewModel remains. You're running into trouble because ...
Read more >
Version Control with Git and GitHub - dib-lab metagenomics ...
It keeps “snapshots” of every point in time in the project's history, so you can never lose or overwrite it. Commit: This is...
Read more >
Disk Rotation Backup Repository - Vembu
Step 1: To set up rotated drives for the backup storage, you need to first create a Disk Rotation repository using one of...
Read more >
Git dependencies - Unity - Manual
When the Package Manager fetches a package from a Git repository, it adds the package locally to your ... For more information, see...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found