概要
iPhoneで撮影したheicファイルをjpegに変換する際、様々な変換ツールが存在しているが、webサービスもインストーラ付フリーソフトもセキュリティ上不安・・・ということで、インストーラなしのシンプルな変換ツールを自作したので公開する。
Pythonでさらっと書いてexe化したものなので、インストール等の作業は一切不要、exeを起動してファイルをドロップしたら画像を一括変換してくれる。
以下からダウンロードできます。
https://drive.google.com/u/0/uc?id=1dblWe8YMvGe2FRLvGOAU770AvgaB38aw&export=download
ソースコード
Pythonを使ったシンプルなコード。
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QLabel, QPushButton, QGridLayout
import sys, os
import pathlib
from PIL import Image
import pillow_heif
import glob
class DragAndDrop(QtWidgets.QLabel):
def __init__(self):
super(DragAndDrop, self).__init__()
self.setWindowTitle('HEIC->JPEG変換ツール')
self.setAlignment(QtCore.Qt.AlignCenter)
self.setSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
self.setWindowFlags(QtCore.Qt.Window | QtCore.Qt.WindowCloseButtonHint)
self.setAcceptDrops(True)
# 文字やボタンはこちら
self.Label1 = QLabel('ボタンにファイル・ディレクトリをドロップしてください', self)
self.Label1.setFont(QtGui.QFont('Meiryo', 10))
self.button1 = CustomButtom1(u'ファイル変換(複数枚可)', self)
self.button1.setFont(QtGui.QFont('Meiryo', 14))
self.button1.setStyleSheet('QPushButton{ background-color: #0099ff; color: white ;}')
# レイアウト作成
layout = QGridLayout()
layout.addWidget(self.Label1)
layout.addWidget(self.button1)
self.setLayout(layout)
class CustomButtom1(QPushButton):
def __init__(self, title, parent):
super(CustomButtom1, self).__init__(title, parent)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
# dropEventにコマンドを書く
def dropEvent(self, event):
mimedata = event.mimeData()
urllist = mimedata.urls()
for i in urllist:
print(i)
filepath = i.path()[1:]
print(filepath)
sf = pathlib.PurePath(filepath).suffix
if sf == ".HEIC" or sf == ".heic":
st = pathlib.PurePath(filepath).stem
heic_jpg("//"+ str(filepath), "//"+ str(filepath).replace(".HEIC", ".JPEG").replace(".heic", ".JPEG"))
else:
QtWidgets.QMessageBox.information(self, 'エラー', "拡張子が.HEICのファイルを指定してください")
return
QtWidgets.QMessageBox.information(self, '成功', str(len(urllist)) + "枚の画像を変換しました")
def heic_jpg(image_path, save_path):
heif_file = pillow_heif.read_heif(image_path)
for img in heif_file:
image = Image.frombytes(
img.mode,
img.size,
img.data,
'raw',
img.mode,
img.stride,
)
image.save(save_path, "JPEG")
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
w = DragAndDrop()
w.show()
sys.exit(app.exec_())