macos - How to display an image in retina resolution from Python - Stack Overflow

admin2025-04-17  2

I'd like to open a window and display an image, in full resolution on a retina display, on MacOS.

I can't find a way to do this, so I'm looking for a solution. I don't mind if it's via matplotlib or Tk or anything else. I don't need the script to do anything apart from generate the image and display it until I close the window. The image is stored in a NumPy array.

Note, this isn't quite as trivial as it sounds. It's not just a case of resizing the image, it's a case of changing the mode that the window is displayed in. This is an OS feature, and so it's a question of whether there are any Python libraries that support activating retina mode and displaying an image using it.

I've mostly tried poking around with Matplotlib, changing the backend and the figure.dpi in the rcparams, but it didn't seem to have any effect. It's difficult to google for solutions, because most people are using Jupyter notebooks but I really just want to use a script.

I'd like to open a window and display an image, in full resolution on a retina display, on MacOS.

I can't find a way to do this, so I'm looking for a solution. I don't mind if it's via matplotlib or Tk or anything else. I don't need the script to do anything apart from generate the image and display it until I close the window. The image is stored in a NumPy array.

Note, this isn't quite as trivial as it sounds. It's not just a case of resizing the image, it's a case of changing the mode that the window is displayed in. This is an OS feature, and so it's a question of whether there are any Python libraries that support activating retina mode and displaying an image using it.

I've mostly tried poking around with Matplotlib, changing the backend and the figure.dpi in the rcparams, but it didn't seem to have any effect. It's difficult to google for solutions, because most people are using Jupyter notebooks but I really just want to use a script.

Share Improve this question edited Feb 1 at 23:45 ZCGCoder 5342 gold badges10 silver badges30 bronze badges asked Jan 31 at 23:40 N. VirgoN. Virgo 8,45915 gold badges49 silver badges66 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

As this is specifically for MacOS you can use its built-in open command.

When you open an image, the action will (by default) use Preview to display the image. Optionally you can specify an application to manage the relevant file.

Here's an example where you can open an image using its default application and also Safari.

import subprocess

image = "foo.jpg"
# by default this will display the image using Preview
subprocess.run(f"open {image}".split())
# or, for example, open the image using Safari
subprocess.run(f"open -a Safari {image}".split())
转载请注明原文地址:http://anycun.com/QandA/1744843513a88399.html