from pathlib import Path

from PIL import Image
import matplotlib.pylab as plt


def fix_image_resolution(image_path):
	figure, axis = plt.subplots(figsize=(8, 6), dpi=100)
	figure.patch.set_facecolor("#111111")
	axis.set_facecolor("#1a1a1a")
	axis.axis("off")
	axis.set_title("Source Image", color="#f2f2f2")

	def close_on_key(event):
		if event.key in ("escape", "q"):
			plt.close(figure)

	figure.canvas.mpl_connect("key_press_event", close_on_key)

	if image_path is None:
		axis.text(0.5, 0.5, "No source image selected", ha="center", va="center", color="#f2f2f2")
		plt.show()
		return

	image = Image.open(image_path).resize((800, 600))
	axis.imshow(image)
	if isinstance(image_path, Path):
		axis.set_title(f"Source Image - {image_path.name}", color="#f2f2f2")
	else:
		axis.set_title(f"Source Image - {Path(image_path).name}", color="#f2f2f2")
	plt.show()
