Initial commit
This commit is contained in:
commit
ff6a649386
3 changed files with 88 additions and 0 deletions
52
heic2jpeg.py
Executable file
52
heic2jpeg.py
Executable file
|
|
@ -0,0 +1,52 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import io
|
||||
import pyheif
|
||||
import piexif
|
||||
from PIL import Image
|
||||
from pathlib import Path
|
||||
|
||||
class Heic2Jpeg:
|
||||
def __init__(self, path):
|
||||
self.path = Path(path)
|
||||
self.img = None
|
||||
|
||||
def decodeImage(self):
|
||||
self.himage = pyheif.read_heif(self.path)
|
||||
self.img = Image.frombytes(
|
||||
self.himage.mode,
|
||||
self.himage.size,
|
||||
self.himage.data,
|
||||
"raw",
|
||||
self.himage.mode,
|
||||
self.himage.stride)
|
||||
|
||||
def getExif(self):
|
||||
for metadata in self.himage.metadata or []:
|
||||
if metadata['type'] == 'Exif':
|
||||
# pyheif.read_heif() rotates the picture
|
||||
# we need to remove the Orientation from EXIF
|
||||
exif_dict = piexif.load(metadata['data'])
|
||||
if exif_dict["0th"] and exif_dict["0th"][piexif.ImageIFD.Orientation]:
|
||||
exif_dict["0th"][piexif.ImageIFD.Orientation] = 1
|
||||
|
||||
return piexif.dump(exif_dict)
|
||||
|
||||
def safe(self, quality=85):
|
||||
if self.img == None:
|
||||
self.decodeImage()
|
||||
|
||||
self.img.save(
|
||||
self.path.with_suffix('.jpg'),
|
||||
format="JPEG",
|
||||
quality=quality,
|
||||
exif=self.getExif())
|
||||
|
||||
if __name__ == "__main__":
|
||||
if len(sys.argv) != 2:
|
||||
print("Usage: heic2jpeg.py path/to/picture.heic")
|
||||
else:
|
||||
img_path = sys.argv[1]
|
||||
h2j = Heic2Jpeg(img_path)
|
||||
h2j.safe()
|
||||
Loading…
Add table
Add a link
Reference in a new issue