|
1 | 1 | #! /usr/bin/python3 -OOt
|
2 | 2 |
|
3 | 3 | # --- Version number ---
|
4 |
| -converterVersion = "001002008" # Change the number if you want to trigger an update. |
| 4 | +converterVersion = "001002009" # Change the number if you want to trigger an update. |
5 | 5 |
|
6 | 6 | # --- Imports ---
|
7 | 7 | import gi
|
|
23 | 23 | from urllib.parse import urlparse, unquote
|
24 | 24 | from pathlib import Path
|
25 | 25 | from datetime import datetime
|
26 |
| -import mimetypes |
| 26 | +import magic |
27 | 27 | import pathlib
|
28 | 28 | import os, shlex
|
29 | 29 | import urllib.request
|
|
32 | 32 | import ast
|
33 | 33 | import re
|
34 | 34 |
|
| 35 | +# --- Create magic object --- |
| 36 | +mime = magic.Magic(mime=True) |
| 37 | + |
35 | 38 | # --- Get the path to the script and if it's writeable ---
|
36 | 39 | currentPath = str(pathlib.Path(__file__).parent.resolve()) # used for config file and self-update!
|
37 | 40 | scriptUpdateable = os.access(f"{currentPath}/{os.path.basename(__file__)}", os.W_OK)
|
38 | 41 |
|
39 | 42 | # --- Check if dependencies are installed and imported ---
|
40 |
| -pyheifInstalled = False |
| 43 | +pillow_heifInstalled = False |
41 | 44 | jxlpyInstalled = False
|
42 | 45 | pillow_avif_pluginInstalled = False
|
43 | 46 |
|
44 | 47 | try:
|
45 |
| - import pyheif |
46 |
| - pyheifInstalled = True |
| 48 | + from pillow_heif import register_heif_opener |
| 49 | + register_heif_opener() |
| 50 | + pillow_heifInstalled = True |
47 | 51 | except ImportError:
|
48 |
| - pyheifInstalled = False |
49 |
| - print(f"WARNING(Nautilus-file-converter)(000): \"pyheif\" not found, if you want to convert from heif format. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information." ) |
| 52 | + pillow_heifInstalled = False |
| 53 | + print(f"WARNING(Nautilus-file-converter)(000): \"pillow_heif\" not found. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information." ) |
50 | 54 |
|
51 | 55 | try:
|
52 | 56 | import jxlpy
|
53 | 57 | from jxlpy import JXLImagePlugin
|
54 | 58 | jxlpyInstalled = True
|
55 | 59 | except ImportError:
|
56 | 60 | jxlpyInstalled = False
|
57 |
| - print(f"WARNING(Nautilus-file-converter)(001): \"jxlpy\" not found, if you want to convert from- or to jxl format. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.") |
| 61 | + print(f"WARNING(Nautilus-file-converter)(001): \"jxlpy\" not found. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.") |
58 | 62 |
|
59 | 63 | try:
|
60 | 64 | import pillow_avif
|
61 | 65 | pillow_avif_pluginInstalled = True
|
62 | 66 | except ImportError:
|
63 |
| - print(f"WARNING(Nautilus-file-converter)(002) \"pillow-avif-plugin\" not found, if you want to convert to avif format. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.") |
| 67 | + print(f"WARNING(Nautilus-file-converter)(002) \"pillow-avif-plugin\" not found. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.") |
64 | 68 |
|
65 | 69 | if not scriptUpdateable:
|
66 | 70 | print(f"WARNING(Nautilus-file-converter)(003): No permission to self-update; script at \"{currentPath}/{os.path.basename(__file__)}\" is not writeable. View https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/errors-and-warnings.md for more information.")
|
|
110 | 114 | print("Updating...")
|
111 | 115 | fileUpdatePath = f"{currentPath}/{os.path.basename(__file__)}"
|
112 | 116 | if _config["showPatchNotes"]:
|
113 |
| - os.system(f"nohup xdg-open \"https://github.com/Lich-Corals/linux-file-converter-addon/releases\" &") |
| 117 | + os.system(f"nohup xdg-open \"https://github.com/Lich-Corals/linux-file-converter-addon/blob/main/markdown/update-notification.md\" &") |
114 | 118 | with open(fileUpdatePath, 'w') as file:
|
115 | 119 | file.write(onlineFile)
|
116 | 120 |
|
|
120 | 124 |
|
121 | 125 | # --- Disable debug printing ---
|
122 | 126 | # comment it out (using '#' in front of the line) if you wish debug printing
|
123 |
| -#print = lambda *wish, **verbosity: None |
| 127 | +print = lambda *wish, **verbosity: None |
| 128 | + |
| 129 | +print(f"pyheif: {pillow_heifInstalled}\njxlpy: {jxlpyInstalled}\npillow_avif: {pillow_avif_pluginInstalled}") |
124 | 130 |
|
125 | 131 | # --- Create file format tuples and write format dict-lists? ---
|
126 | 132 | READ_FORMATS_IMAGE = ('image/jpeg',
|
|
143 | 149 | 'image/webp')
|
144 | 150 |
|
145 | 151 | pyheifReadFormats = ('image/avif',
|
146 |
| - 'image/heif') |
| 152 | + 'image/heif', |
| 153 | + 'image/heic') |
147 | 154 |
|
148 | 155 | jxlpyReadFormats = ('image/jxl')
|
149 | 156 |
|
|
236 | 243 | {'name': 'MP3'},
|
237 | 244 | {'name': 'WAV'}]
|
238 | 245 |
|
239 |
| -if pyheifInstalled: |
| 246 | +if pillow_heifInstalled: |
240 | 247 | READ_FORMATS_IMAGE = READ_FORMATS_IMAGE + pyheifReadFormats
|
241 | 248 |
|
242 | 249 | if jxlpyInstalled:
|
@@ -273,25 +280,14 @@ def convert_image(menu, format, files):
|
273 | 280 | from_file_path = file
|
274 | 281 | print(__removeTimestamp(from_file_path.stem) + from_file_path.stem)
|
275 | 282 | to_file_path = from_file_path.with_name(f"{__removeTimestamp(from_file_path.stem)}{_addToName}.{format['extension'].lower()}")
|
276 |
| - try: |
277 |
| - image = Image.open(from_file_path) |
278 |
| - if (format['name']) == 'JPEG': |
279 |
| - image = image.convert('RGB') |
280 |
| - if 'square' in format: |
281 |
| - image = image.resize((int(format['square']), int(format['square']))) |
282 |
| - if 'w' in format: |
283 |
| - image = image.resize((int(format['w']), int(format['h']))) |
284 |
| - image.save(to_file_path, format=(format['extension'])) |
285 |
| - except UnidentifiedImageError: |
286 |
| - try: |
287 |
| - heif_file = pyheif.read(from_file_path) |
288 |
| - heif_image = Image.frombytes(heif_file.mode, heif_file.size, heif_file.data, "raw", heif_file.mode, heif_file.stride,) |
289 |
| - if (format['extension']) == 'JPEG': |
290 |
| - heif_image = heif_image.convert("RGB") |
291 |
| - heif_image.save(to_file_path, format['extension']) |
292 |
| - except UnidentifiedImageError: |
293 |
| - pass |
294 |
| - pass |
| 283 | + image = Image.open(from_file_path) |
| 284 | + if (format['name']) == 'JPEG': |
| 285 | + image = image.convert('RGB') |
| 286 | + if 'square' in format: |
| 287 | + image = image.resize((int(format['square']), int(format['square']))) |
| 288 | + if 'w' in format: |
| 289 | + image = image.resize((int(format['w']), int(format['h']))) |
| 290 | + image.save(to_file_path, format=(format['extension'])) |
295 | 291 |
|
296 | 292 |
|
297 | 293 | # --- Function to convert using FFMPEG (video and audio) ---
|
@@ -323,15 +319,16 @@ def __init__(self):
|
323 | 319 | _allAudios = True
|
324 | 320 | _allVideos = True
|
325 | 321 | for _arg in _nemoArgs:
|
326 |
| - if not mimetypes.guess_type(str(_arg))[0] in READ_FORMATS_IMAGE: |
| 322 | + if not mime.from_file(_arg) in READ_FORMATS_IMAGE: |
327 | 323 | _allImages = False
|
328 |
| - if not mimetypes.guess_type(str(_arg))[0] in READ_FORMATS_AUDIO: |
| 324 | + if not mime.from_file(_arg) in READ_FORMATS_AUDIO: |
329 | 325 | _allAudios = False
|
330 |
| - if not mimetypes.guess_type(str(_arg))[0] in READ_FORMATS_VIDEO: |
| 326 | + if not mime.from_file(_arg) in READ_FORMATS_VIDEO: |
331 | 327 | _allVideos = False
|
332 | 328 |
|
333 | 329 | if _allImages:
|
334 | 330 | for writeFormat in WRITE_FORMATS_IMAGE:
|
| 331 | + print(writeFormat) |
335 | 332 | extensions.append([writeFormat['name'], str(writeFormat), 0])
|
336 | 333 | if _config["convertToSquares"]:
|
337 | 334 | for writeFormat in WRITE_FORMATS_SQUARE:
|
|
0 commit comments