Tkinter画布不显示整个照片图像

时间:2015-09-03 13:13:27

标签: python tkinter

我创建了一个简短的脚本来在Tkinter画布元素中显示照片。我将canvas元素的大小设置为与照片大小相同,但是当我启动窗口时,无论我多大程度地展开窗口,我通常只会看到图像的一小部分。

当我打印出用于设置画布大小的图像大小时,此大小显示(922,614),而当我在画布上记录鼠标点击时,右下角就像(500,300)。我的代码如下。我应该更改什么,以使画布与图像大小相同,并完全显示图像?

class AppWindow(Frame):

def __init__(self, parent, list_of_files, write_file):
    Frame.__init__(self, parent)           
    self.parent = parent
    ...
    self.image = None
    self.canvas = None
    self.index = -1
    self.loadImage()
    self.initUI()
    self.resetCanvas()

def initUI(self):
    self.style = Style()
    self.style.theme_use("default")
    self.pack(fill=BOTH, expand=1)
    ...
    self.canvas = Tkinter.Canvas(self, width = self.image.width(), height = self.image.height())   

def loadImage(self):
    self.index += 1
    img = cv2.imread(self.list_of_files[self.index])                       
    img = cv2.resize(img, (0,0), fx = IMAGE_RESIZE_FACTOR, fy = IMAGE_RESIZE_FACTOR)
    b, g, r = cv2.split(img)
    img = cv2.merge((r,g,b))
    im = Image.fromarray(img)
    self.image = ImageTk.PhotoImage(image=im)       

def resetCanvas(self):        
    self.canvas.create_image(0, 0, image=self.image)
    self.canvas.place(x = 0, y = 0, height = self.image.height(), width = self.image.width())

以下是显示照片及其在Tkinter画布中的显示方式的屏幕截图:

screenshot

这是我到目前为止所尝试的内容:

  • 不调整图像大小或更改调整大小 - 这不会做任何事情
  • 使画布的大小加倍于图像的大小,而不是等于图像,喜欢这样self.canvas = Tkinter.Canvas(self, width = self.image.width()*2, height = self.image.height()*2)这样可以使画布更大(我可以说,因为我在画布上有一些绘图功能),但是图像保持相同的小尺寸,而不是显示整个图像
  • 当我用cv2.imshow()显示cv2格式图像时,我看到了完整的图像,因此切断图像部分的cv2不是import Tkinter import Image, ImageTk from Tkinter import Tk, BOTH from ttk import Frame, Button, Style import cv2 import os import time import itertools IMAGE_RESIZE_FACTOR = .3 class AppWindow(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.loadImage() self.initUI() def loadImage(self): img = cv2.imread("w_4131.jpg") img = cv2.resize(img, (0,0), fx = IMAGE_RESIZE_FACTOR, fy = IMAGE_RESIZE_FACTOR) b, g, r = cv2.split(img) img = cv2.merge((r,g,b)) im = Image.fromarray(img) self.image = ImageTk.PhotoImage(image=im) def initUI(self): self.style = Style() self.style.theme_use("default") self.pack(fill=BOTH, expand=1) print "width and height of image should be ", self.image.width(), self.image.height() self.canvas = Tkinter.Canvas(self, width = self.image.width(), height = self.image.height()) self.canvas.pack() self.canvas.create_image(0, 0, image=self.image) def main(): root = Tk() root.geometry("250x150+300+300") app = AppWindow(root) root.mainloop() if __name__ == '__main__': main()

我意识到上面的内容并不是一个完整的工作示例。我已经减少了脚本,现在运行它我仍然看到同样的问题:

Advertisements.cachedCategoriesByUrl = {};

Advertisements.getCategories = function(categoriesUrl) {

    var cachedCategories = Advertisements.cachedCategoriesByUrl[categoriesUrl];

    if(cachedCategories){

        return cachedCategories;

    } else {

        var getCategories = $.ajax({
            url: categoriesUrl,
            data: { format: 'json' },
            error: function(jqXHR, status, thrownError) {
                => I'd like to pass an empty array so the slot will show 
                ads with no targetting set. 
                However this doesn't seem to be working.

                Do I need to do callback?
            }
        });

        Advertisements.cachedCategoriesByUrl[categoriesUrl] = getCategories;
        return getCategories;
    }
}

1 个答案:

答案 0 :(得分:5)

当您将图像放在0,0的画布上时,它指定图像的中心的位置,而不是左上角。图像就在那里,只是你只看到了右下角。

在为坐标创建图像时添加anchor="nw"以表示图像的左上角:

self.canvas.create_image(0, 0, image=self.image, anchor="nw")

至于画布与图像大小不同,我没有看到。图像看起来非常适合画布。