1import importlib
2
3from ..core.legacy_plugin_wrapper import LegacyPlugin
4
5
6class PluginConfig:
7 """Plugin Configuration Metadata
8
9 This class holds the information needed to lazy-import plugins.
10
11 Parameters
12 ----------
13 name : str
14 The name of the plugin.
15 class_name : str
16 The name of the plugin class inside the plugin module.
17 module_name : str
18 The name of the module/package from which to import the plugin.
19 is_legacy : bool
20 If True, this plugin is a v2 plugin and will be wrapped in a
21 LegacyPlugin. Default: False.
22 package_name : str
23 If the given module name points to a relative module, then the package
24 name determines the package it is relative to.
25 install_name : str
26 The name of the optional dependency that can be used to install this
27 plugin if it is missing.
28 legacy_args : Dict
29 A dictionary of kwargs to pass to the v2 plugin (Format) upon construction.
30
31 Examples
32 --------
33 >>> PluginConfig(
34 name="TIFF",
35 class_name="TiffFormat",
36 module_name="imageio.plugins.tifffile",
37 is_legacy=True,
38 install_name="tifffile",
39 legacy_args={
40 "description": "TIFF format",
41 "extensions": ".tif .tiff .stk .lsm",
42 "modes": "iIvV",
43 },
44 )
45 >>> PluginConfig(
46 name="pillow",
47 class_name="PillowPlugin",
48 module_name="imageio.plugins.pillow"
49 )
50
51 """
52
53 def __init__(
54 self,
55 name,
56 class_name,
57 module_name,
58 *,
59 is_legacy=False,
60 package_name=None,
61 install_name=None,
62 legacy_args=None,
63 ):
64 legacy_args = legacy_args or dict()
65
66 self.name = name
67 self.class_name = class_name
68 self.module_name = module_name
69 self.package_name = package_name
70
71 self.is_legacy = is_legacy
72 self.install_name = install_name or self.name
73 self.legacy_args = {"name": name, "description": "A legacy plugin"}
74 self.legacy_args.update(legacy_args)
75
76 @property
77 def format(self):
78 """For backwards compatibility with FormatManager
79
80 Delete when migrating to v3
81 """
82 if not self.is_legacy:
83 raise RuntimeError("Can only get format for legacy plugins.")
84
85 module = importlib.import_module(self.module_name, self.package_name)
86 clazz = getattr(module, self.class_name)
87 return clazz(**self.legacy_args)
88
89 @property
90 def plugin_class(self):
91 """Get the plugin class (import if needed)
92
93 Returns
94 -------
95 plugin_class : Any
96 The class that can be used to instantiate plugins.
97
98 """
99
100 module = importlib.import_module(self.module_name, self.package_name)
101 clazz = getattr(module, self.class_name)
102
103 if self.is_legacy:
104 legacy_plugin = clazz(**self.legacy_args)
105
106 def partial_legacy_plugin(request):
107 return LegacyPlugin(request, legacy_plugin)
108
109 clazz = partial_legacy_plugin
110
111 return clazz
112
113
114known_plugins = dict()
115known_plugins["pillow"] = PluginConfig(
116 name="pillow", class_name="PillowPlugin", module_name="imageio.plugins.pillow"
117)
118known_plugins["pyav"] = PluginConfig(
119 name="pyav", class_name="PyAVPlugin", module_name="imageio.plugins.pyav"
120)
121known_plugins["opencv"] = PluginConfig(
122 name="opencv", class_name="OpenCVPlugin", module_name="imageio.plugins.opencv"
123)
124known_plugins["tifffile"] = PluginConfig(
125 name="tifffile",
126 class_name="TifffilePlugin",
127 module_name="imageio.plugins.tifffile_v3",
128)
129known_plugins["SPE"] = PluginConfig(
130 name="spe", class_name="SpePlugin", module_name="imageio.plugins.spe"
131)
132known_plugins["rawpy"] = PluginConfig(
133 name="rawpy", class_name="RawPyPlugin", module_name="imageio.plugins.rawpy"
134)
135
136# Legacy plugins
137# ==============
138#
139# Which are partly registered by format, partly by plugin, and partly by a mix
140# of both. We keep the naming here for backwards compatibility.
141# In v3 this should become a single entry per plugin named after the plugin
142# We can choose extension-specific priority in ``config.extensions``.
143#
144# Note: Since python 3.7 order of insertion determines the order of dict().keys()
145# This means that the order here determines the order by which plugins are
146# checked during the full fallback search. We don't advertise this downstream,
147# but it could be a useful thing to keep in mind to choose a sensible default
148# search order.
149
150known_plugins["TIFF"] = PluginConfig(
151 name="TIFF",
152 class_name="TiffFormat",
153 module_name="imageio.plugins.tifffile",
154 is_legacy=True,
155 install_name="tifffile",
156 legacy_args={
157 "description": "TIFF format",
158 "extensions": ".tif .tiff .stk .lsm",
159 "modes": "iIvV",
160 },
161)
162
163# PILLOW plugin formats (legacy)
164PILLOW_FORMATS = [
165 ("BMP", "Windows Bitmap", ".bmp", "PillowFormat"),
166 ("BUFR", "BUFR", ".bufr", "PillowFormat"),
167 ("CUR", "Windows Cursor", ".cur", "PillowFormat"),
168 ("DCX", "Intel DCX", ".dcx", "PillowFormat"),
169 ("DDS", "DirectDraw Surface", ".dds", "PillowFormat"),
170 ("DIB", "Windows Bitmap", "", "PillowFormat"),
171 ("EPS", "Encapsulated Postscript", ".ps .eps", "PillowFormat"),
172 ("FITS", "FITS", ".fit .fits", "PillowFormat"),
173 ("FLI", "Autodesk FLI/FLC Animation", ".fli .flc", "PillowFormat"),
174 ("FPX", "FlashPix", ".fpx", "PillowFormat"),
175 ("FTEX", "Texture File Format (IW2:EOC)", ".ftc .ftu", "PillowFormat"),
176 ("GBR", "GIMP brush file", ".gbr", "PillowFormat"),
177 ("GIF", "Compuserve GIF", ".gif", "GIFFormat"),
178 ("GRIB", "GRIB", ".grib", "PillowFormat"),
179 ("HDF5", "HDF5", ".h5 .hdf", "PillowFormat"),
180 ("ICNS", "Mac OS icns resource", ".icns", "PillowFormat"),
181 ("ICO", "Windows Icon", ".ico", "PillowFormat"),
182 ("IM", "IFUNC Image Memory", ".im", "PillowFormat"),
183 ("IMT", "IM Tools", "", "PillowFormat"),
184 ("IPTC", "IPTC/NAA", ".iim", "PillowFormat"),
185 ("JPEG", "JPEG (ISO 10918)", ".jfif .jpe .jpg .jpeg", "JPEGFormat"),
186 (
187 "JPEG2000",
188 "JPEG 2000 (ISO 15444)",
189 ".jp2 .j2k .jpc .jpf .jpx .j2c",
190 "JPEG2000Format",
191 ),
192 ("MCIDAS", "McIdas area file", "", "PillowFormat"),
193 ("MIC", "Microsoft Image Composer", ".mic", "PillowFormat"),
194 # skipped in legacy pillow
195 # ("MPEG", "MPEG", ".mpg .mpeg", "PillowFormat"),
196 ("MPO", "MPO (CIPA DC-007)", ".mpo", "PillowFormat"),
197 ("MSP", "Windows Paint", ".msp", "PillowFormat"),
198 ("PCD", "Kodak PhotoCD", ".pcd", "PillowFormat"),
199 ("PCX", "Paintbrush", ".pcx", "PillowFormat"),
200 ("PIXAR", "PIXAR raster image", ".pxr", "PillowFormat"),
201 ("PNG", "Portable network graphics", ".png", "PNGFormat"),
202 ("PPM", "Pbmplus image", ".pbm .pgm .ppm", "PillowFormat"),
203 ("PSD", "Adobe Photoshop", ".psd", "PillowFormat"),
204 ("SGI", "SGI Image File Format", ".bw .rgb .rgba .sgi", "PillowFormat"),
205 ("SPIDER", "Spider 2D image", "", "PillowFormat"),
206 ("SUN", "Sun Raster File", ".ras", "PillowFormat"),
207 ("TGA", "Targa", ".tga", "PillowFormat"),
208 ("TIFF", "Adobe TIFF", ".tif .tiff", "TIFFFormat"),
209 ("WMF", "Windows Metafile", ".wmf .emf", "PillowFormat"),
210 ("XBM", "X11 Bitmap", ".xbm", "PillowFormat"),
211 ("XPM", "X11 Pixel Map", ".xpm", "PillowFormat"),
212 ("XVTHUMB", "XV thumbnail image", "", "PillowFormat"),
213]
214for id, summary, ext, class_name in PILLOW_FORMATS:
215 config = PluginConfig(
216 name=id.upper() + "-PIL",
217 class_name=class_name,
218 module_name="imageio.plugins.pillow_legacy",
219 is_legacy=True,
220 install_name="pillow",
221 legacy_args={
222 "description": summary + " via Pillow",
223 "extensions": ext,
224 "modes": "iI" if class_name == "GIFFormat" else "i",
225 "plugin_id": id,
226 },
227 )
228 known_plugins[config.name] = config
229
230known_plugins["FFMPEG"] = PluginConfig(
231 name="FFMPEG",
232 class_name="FfmpegFormat",
233 module_name="imageio.plugins.ffmpeg",
234 is_legacy=True,
235 install_name="ffmpeg",
236 legacy_args={
237 "description": "Many video formats and cameras (via ffmpeg)",
238 "extensions": ".mov .avi .mpg .mpeg .mp4 .mkv .webm .wmv .h264",
239 "modes": "I",
240 },
241)
242
243known_plugins["BSDF"] = PluginConfig(
244 name="BSDF",
245 class_name="BsdfFormat",
246 module_name="imageio.plugins.bsdf",
247 is_legacy=True,
248 install_name="bsdf",
249 legacy_args={
250 "description": "Format based on the Binary Structured Data Format",
251 "extensions": ".bsdf",
252 "modes": "iIvV",
253 },
254)
255
256known_plugins["DICOM"] = PluginConfig(
257 name="DICOM",
258 class_name="DicomFormat",
259 module_name="imageio.plugins.dicom",
260 is_legacy=True,
261 install_name="dicom",
262 legacy_args={
263 "description": "Digital Imaging and Communications in Medicine",
264 "extensions": ".dcm .ct .mri",
265 "modes": "iIvV",
266 },
267)
268
269known_plugins["FEI"] = PluginConfig(
270 name="FEI",
271 class_name="FEISEMFormat",
272 module_name="imageio.plugins.feisem",
273 is_legacy=True,
274 install_name="feisem",
275 legacy_args={
276 "description": "FEI-SEM TIFF format",
277 "extensions": [".tif", ".tiff"],
278 "modes": "iv",
279 },
280)
281
282known_plugins["FITS"] = PluginConfig(
283 name="FITS",
284 class_name="FitsFormat",
285 module_name="imageio.plugins.fits",
286 is_legacy=True,
287 install_name="fits",
288 legacy_args={
289 "description": "Flexible Image Transport System (FITS) format",
290 "extensions": ".fits .fit .fts .fz",
291 "modes": "iIvV",
292 },
293)
294
295known_plugins["GDAL"] = PluginConfig(
296 name="GDAL",
297 class_name="GdalFormat",
298 module_name="imageio.plugins.gdal",
299 is_legacy=True,
300 install_name="gdal",
301 legacy_args={
302 "description": "Geospatial Data Abstraction Library",
303 "extensions": ".tiff .tif .img .ecw .jpg .jpeg",
304 "modes": "iIvV",
305 },
306)
307
308known_plugins["ITK"] = PluginConfig(
309 name="ITK",
310 class_name="ItkFormat",
311 module_name="imageio.plugins.simpleitk",
312 is_legacy=True,
313 install_name="simpleitk",
314 legacy_args={
315 "description": "Insight Segmentation and Registration Toolkit (ITK) format",
316 "extensions": " ".join(
317 (
318 ".gipl",
319 ".ipl",
320 ".mha",
321 ".mhd",
322 ".nhdr",
323 ".nia",
324 ".hdr",
325 ".nrrd",
326 ".nii",
327 ".nii.gz",
328 ".img",
329 ".img.gz",
330 ".vtk",
331 ".hdf5",
332 ".lsm",
333 ".mnc",
334 ".mnc2",
335 ".mgh",
336 ".mnc",
337 ".pic",
338 ".bmp",
339 ".jpeg",
340 ".jpg",
341 ".png",
342 ".tiff",
343 ".tif",
344 ".dicom",
345 ".dcm",
346 ".gdcm",
347 )
348 ),
349 "modes": "iIvV",
350 },
351)
352
353known_plugins["NPZ"] = PluginConfig(
354 name="NPZ",
355 class_name="NpzFormat",
356 module_name="imageio.plugins.npz",
357 is_legacy=True,
358 install_name="numpy",
359 legacy_args={
360 "description": "Numpy's compressed array format",
361 "extensions": ".npz",
362 "modes": "iIvV",
363 },
364)
365
366known_plugins["SWF"] = PluginConfig(
367 name="SWF",
368 class_name="SWFFormat",
369 module_name="imageio.plugins.swf",
370 is_legacy=True,
371 install_name="swf",
372 legacy_args={
373 "description": "Shockwave flash",
374 "extensions": ".swf",
375 "modes": "I",
376 },
377)
378
379known_plugins["SCREENGRAB"] = PluginConfig(
380 name="SCREENGRAB",
381 class_name="ScreenGrabFormat",
382 module_name="imageio.plugins.grab",
383 is_legacy=True,
384 install_name="pillow",
385 legacy_args={
386 "description": "Grab screenshots (Windows and OS X only)",
387 "extensions": [],
388 "modes": "i",
389 },
390)
391
392known_plugins["CLIPBOARDGRAB"] = PluginConfig(
393 name="CLIPBOARDGRAB",
394 class_name="ClipboardGrabFormat",
395 module_name="imageio.plugins.grab",
396 is_legacy=True,
397 install_name="pillow",
398 legacy_args={
399 "description": "Grab from clipboard (Windows only)",
400 "extensions": [],
401 "modes": "i",
402 },
403)
404
405# LYTRO plugin (legacy)
406lytro_formats = [
407 ("lytro-lfr", "Lytro Illum lfr image file", ".lfr", "i", "LytroLfrFormat"),
408 (
409 "lytro-illum-raw",
410 "Lytro Illum raw image file",
411 ".raw",
412 "i",
413 "LytroIllumRawFormat",
414 ),
415 ("lytro-lfp", "Lytro F01 lfp image file", ".lfp", "i", "LytroLfpFormat"),
416 ("lytro-f01-raw", "Lytro F01 raw image file", ".raw", "i", "LytroF01RawFormat"),
417]
418for name, des, ext, mode, class_name in lytro_formats:
419 config = PluginConfig(
420 name=name.upper(),
421 class_name=class_name,
422 module_name="imageio.plugins.lytro",
423 is_legacy=True,
424 install_name="lytro",
425 legacy_args={
426 "description": des,
427 "extensions": ext,
428 "modes": mode,
429 },
430 )
431 known_plugins[config.name] = config
432
433# FreeImage plugin (legacy)
434FREEIMAGE_FORMATS = [
435 (
436 "BMP",
437 0,
438 "Windows or OS/2 Bitmap",
439 ".bmp",
440 "i",
441 "FreeimageBmpFormat",
442 "imageio.plugins.freeimage",
443 ),
444 (
445 "CUT",
446 21,
447 "Dr. Halo",
448 ".cut",
449 "i",
450 "FreeimageFormat",
451 "imageio.plugins.freeimage",
452 ),
453 (
454 "DDS",
455 24,
456 "DirectX Surface",
457 ".dds",
458 "i",
459 "FreeimageFormat",
460 "imageio.plugins.freeimage",
461 ),
462 (
463 "EXR",
464 29,
465 "ILM OpenEXR",
466 ".exr",
467 "i",
468 "FreeimageFormat",
469 "imageio.plugins.freeimage",
470 ),
471 (
472 "G3",
473 27,
474 "Raw fax format CCITT G.3",
475 ".g3",
476 "i",
477 "FreeimageFormat",
478 "imageio.plugins.freeimage",
479 ),
480 (
481 "GIF",
482 25,
483 "Static and animated gif (FreeImage)",
484 ".gif",
485 "iI",
486 "GifFormat",
487 "imageio.plugins.freeimagemulti",
488 ),
489 (
490 "HDR",
491 26,
492 "High Dynamic Range Image",
493 ".hdr",
494 "i",
495 "FreeimageFormat",
496 "imageio.plugins.freeimage",
497 ),
498 (
499 "ICO",
500 1,
501 "Windows Icon",
502 ".ico",
503 "iI",
504 "IcoFormat",
505 "imageio.plugins.freeimagemulti",
506 ),
507 (
508 "IFF",
509 5,
510 "IFF Interleaved Bitmap",
511 ".iff .lbm",
512 "i",
513 "FreeimageFormat",
514 "imageio.plugins.freeimage",
515 ),
516 (
517 "J2K",
518 30,
519 "JPEG-2000 codestream",
520 ".j2k .j2c",
521 "i",
522 "FreeimageFormat",
523 "imageio.plugins.freeimage",
524 ),
525 (
526 "JNG",
527 3,
528 "JPEG Network Graphics",
529 ".jng",
530 "i",
531 "FreeimageFormat",
532 "imageio.plugins.freeimage",
533 ),
534 (
535 "JP2",
536 31,
537 "JPEG-2000 File Format",
538 ".jp2",
539 "i",
540 "FreeimageFormat",
541 "imageio.plugins.freeimage",
542 ),
543 (
544 "JPEG",
545 2,
546 "JPEG - JFIF Compliant",
547 ".jpg .jif .jpeg .jpe",
548 "i",
549 "FreeimageJpegFormat",
550 "imageio.plugins.freeimage",
551 ),
552 (
553 "JPEG-XR",
554 36,
555 "JPEG XR image format",
556 ".jxr .wdp .hdp",
557 "i",
558 "FreeimageFormat",
559 "imageio.plugins.freeimage",
560 ),
561 (
562 "KOALA",
563 4,
564 "C64 Koala Graphics",
565 ".koa",
566 "i",
567 "FreeimageFormat",
568 "imageio.plugins.freeimage",
569 ),
570 # not registered in legacy pillow
571 # ("MNG", 6, "Multiple-image Network Graphics", ".mng", "i", "FreeimageFormat", "imageio.plugins.freeimage"),
572 (
573 "PBM",
574 7,
575 "Portable Bitmap (ASCII)",
576 ".pbm",
577 "i",
578 "FreeimageFormat",
579 "imageio.plugins.freeimage",
580 ),
581 (
582 "PBMRAW",
583 8,
584 "Portable Bitmap (RAW)",
585 ".pbm",
586 "i",
587 "FreeimageFormat",
588 "imageio.plugins.freeimage",
589 ),
590 (
591 "PCD",
592 9,
593 "Kodak PhotoCD",
594 ".pcd",
595 "i",
596 "FreeimageFormat",
597 "imageio.plugins.freeimage",
598 ),
599 (
600 "PCX",
601 10,
602 "Zsoft Paintbrush",
603 ".pcx",
604 "i",
605 "FreeimageFormat",
606 "imageio.plugins.freeimage",
607 ),
608 (
609 "PFM",
610 32,
611 "Portable floatmap",
612 ".pfm",
613 "i",
614 "FreeimageFormat",
615 "imageio.plugins.freeimage",
616 ),
617 (
618 "PGM",
619 11,
620 "Portable Greymap (ASCII)",
621 ".pgm",
622 "i",
623 "FreeimageFormat",
624 "imageio.plugins.freeimage",
625 ),
626 (
627 "PGMRAW",
628 12,
629 "Portable Greymap (RAW)",
630 ".pgm",
631 "i",
632 "FreeimageFormat",
633 "imageio.plugins.freeimage",
634 ),
635 (
636 "PICT",
637 33,
638 "Macintosh PICT",
639 ".pct .pict .pic",
640 "i",
641 "FreeimageFormat",
642 "imageio.plugins.freeimage",
643 ),
644 (
645 "PNG",
646 13,
647 "Portable Network Graphics",
648 ".png",
649 "i",
650 "FreeimagePngFormat",
651 "imageio.plugins.freeimage",
652 ),
653 (
654 "PPM",
655 14,
656 "Portable Pixelmap (ASCII)",
657 ".ppm",
658 "i",
659 "FreeimagePnmFormat",
660 "imageio.plugins.freeimage",
661 ),
662 (
663 "PPMRAW",
664 15,
665 "Portable Pixelmap (RAW)",
666 ".ppm",
667 "i",
668 "FreeimagePnmFormat",
669 "imageio.plugins.freeimage",
670 ),
671 (
672 "PSD",
673 20,
674 "Adobe Photoshop",
675 ".psd",
676 "i",
677 "FreeimageFormat",
678 "imageio.plugins.freeimage",
679 ),
680 (
681 "RAS",
682 16,
683 "Sun Raster Image",
684 ".ras",
685 "i",
686 "FreeimageFormat",
687 "imageio.plugins.freeimage",
688 ),
689 (
690 "RAW",
691 34,
692 "RAW camera image",
693 ".3fr .arw .bay .bmq .cap .cine .cr2 .crw .cs1 .dc2 "
694 ".dcr .drf .dsc .dng .erf .fff .ia .iiq .k25 .kc2 .kdc .mdc .mef .mos .mrw .nef .nrw .orf "
695 ".pef .ptx .pxn .qtk .raf .raw .rdc .rw2 .rwl .rwz .sr2 .srf .srw .sti",
696 "i",
697 "FreeimageFormat",
698 "imageio.plugins.freeimage",
699 ),
700 (
701 "SGI",
702 28,
703 "SGI Image Format",
704 ".sgi .rgb .rgba .bw",
705 "i",
706 "FreeimageFormat",
707 "imageio.plugins.freeimage",
708 ),
709 (
710 "TARGA",
711 17,
712 "Truevision Targa",
713 ".tga .targa",
714 "i",
715 "FreeimageFormat",
716 "imageio.plugins.freeimage",
717 ),
718 (
719 "TIFF",
720 18,
721 "Tagged Image File Format",
722 ".tif .tiff",
723 "i",
724 "FreeimageFormat",
725 "imageio.plugins.freeimage",
726 ),
727 (
728 "WBMP",
729 19,
730 "Wireless Bitmap",
731 ".wap .wbmp .wbm",
732 "i",
733 "FreeimageFormat",
734 "imageio.plugins.freeimage",
735 ),
736 (
737 "WebP",
738 35,
739 "Google WebP image format",
740 ".webp",
741 "i",
742 "FreeimageFormat",
743 "imageio.plugins.freeimage",
744 ),
745 (
746 "XBM",
747 22,
748 "X11 Bitmap Format",
749 ".xbm",
750 "i",
751 "FreeimageFormat",
752 "imageio.plugins.freeimage",
753 ),
754 (
755 "XPM",
756 23,
757 "X11 Pixmap Format",
758 ".xpm",
759 "i",
760 "FreeimageFormat",
761 "imageio.plugins.freeimage",
762 ),
763]
764for name, i, des, ext, mode, class_name, module_name in FREEIMAGE_FORMATS:
765 config = PluginConfig(
766 name=name.upper() + "-FI",
767 class_name=class_name,
768 module_name=module_name,
769 is_legacy=True,
770 install_name="freeimage",
771 legacy_args={
772 "description": des,
773 "extensions": ext,
774 "modes": mode,
775 "fif": i,
776 },
777 )
778 known_plugins[config.name] = config
779
780# exists for backwards compatibility with FormatManager
781# delete in V3
782_original_order = [x for x, config in known_plugins.items() if config.is_legacy]