Pillow ImageTK 仅显示图像的一部分

Pillow ImageTK displaying only a part of an image

我最近编写了一个代码来尝试成为照片的应用程序,但是图像显示不正确。我有一张图片供参考,我试图让它在 canvas 上显示全部内容,但我得到以下信息:

代码:

def open_image():
    global selected_image
    global img
    try:
        selected_image = askopenfile(title="Open Image", filetypes=filetypes).name
        root.title(selected_image + " - Photos")
        img_temp = Image.open(selected_image).resize((960, 540), Image.ANTIALIAS)
        img = ImageTk.PhotoImage(img_temp)
        image_area.create_image(1, 1, anchor="n", image=img)
    except Exception as e:
        print(e)

测试用图片: The original image

代码与图片的结果:The result

如果有人能提供帮助,我将不胜感激!

(人们要求完整的代码,所以在这里)

import tkinter
from tkinter.filedialog import *
from PIL import Image, ImageTk

img_exten = r"*.png  *.bmp  *jpeg  *.bmp  *.ico  *.gif  *.jpg"
filetypes = (
    ("Image Files", img_exten),
    ("All Files", "*.*")
)

selected_image = ""
img = ""

root = tkinter.Tk()
root.title("Photos")


def do_nothing():
    print(0)


def open_image():
    global selected_image
    global img
    try:
        selected_image = askopenfile(title="Open Image", filetypes=filetypes).name
        root.title(selected_image + " - Photos")
        img_temp = Image.open(selected_image).resize((960, 540), Image.ANTIALIAS)
        size = 960, 540
        img_temp.thumbnail(size, Image.ANTIALIAS)
        img = ImageTk.PhotoImage(img_temp)
        image_area.create_image(1, 1, image=img)
    except Exception as e:
        print(e)


def close_image():
    global selected_image
    selected_image = ""
    root.title("Photos")

image_area = tkinter.Canvas(root, width=960, height=540)
image_area.grid(column=1, row=1)

# Running the window
tkinter.mainloop()
import os, sys
import Image

size = 128, 128


for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

取自How do I resize an image using PIL and maintain its aspect ratio?

所以在你的情况下:

def open_image():
    global selected_image
    global img
    try:
        selected_image = askopenfile(title="Open Image", filetypes=filetypes).name
        root.title(selected_image + " - Photos")
        img_temp = Image.open(selected_image))
        im_temp.thumbnail(960, 540, Image.ANTIALIAS)
        img = ImageTk.PhotoImage(img_temp)
        image_area.create_image(1, 1, anchor="n", image=img)
    except Exception as e:
        print(e)

更新:

import os, sys
from PIL import Image, ImageDraw

size = 128, 128

infile = r'Drive:\path\logo.png'
outfile = r'Drive:\path\logo2.png'
im = Image.open(infile)
im.thumbnail(size, Image.ANTIALIAS)
im.save(outfile, "PNG")

我已经测试过,这将调整 .png 和 .jpeg 的大小。

结果:

上传时它被转换为.png。是.jpeg.

image_area 未在此函数内定义。如果我们能看到定义的位置以及定义的内容以查看问题是否源自那里,那将会很有帮助。

我还想通过更改

确保将图像调整为相同的纵横比
img_temp = Image.open(selected_image).resize((960, 540), Image.ANTIALIAS)

img_temp = Image.open(selected_image)
ar = img_temp.width / img_temp.height
height = 540
width = int(height*ar)
img_temp = img_temp.resize((width, height), Image.ANTIALIAS)

除了我不得不修改您的代码以使其生成任何图像这一事实之外,唯一的问题似乎是您没有设置图像的 anchor 属性。因此,tkinter 会将图像的 center 放置在给定坐标处。这意味着只会显示图像的右下象限。

如果您希望图像的左上角位于给定坐标处,请将 anchor 设置为“nw”(例如:“northwest”)。当我进行以下修改时,我的 window 显示了完整的调整大小的图像。

image_area.create_image(1, 1, image=img, anchor="nw")