编辑:当我使用margin: 0 auto
而不是display: block
时,画布上没有任何显示。
See screenshot。
我正在创建一个图像查看器,该查看器分为四个框架,每个框架分别显示图像。目前,我注释掉了四个框架中的三个,重点放在首先解决异常上。
我正在尝试在画布内显示图像,后者被嵌入在屏幕左上角的<div class="container">
<img src="images/me.jpg" align="center" alt="park" width="500px">
<button class="btn">lokationer</button>
</div>`
内。每当我运行代码时,我都会得到堆栈跟踪:
.container {
border: 1pt solid red;
max-width: 400px;
margin: 0 auto;
}
.container img {
width: 100%;
height: auto;
}
.container .btn {
background-color: #555;
color: white;
font-size: 16px;
padding: 12px 24px;
border: none;
cursor: pointer;
border-radius: 5px;
text-align: center;
display: block;
margin: 0 auto;
}
.container .btn:hover {
background-color: black;
}
这是我的课程:
image = ImageTk.PhotoImage(resize_image)
我想念什么?
更新:现在窗口无例外地打开,但是图像仍然不显示,我用红色将file=resize_image
涂成红色,并将其宽度和高度减小了50px,以便能够区分它从框架开始,后者是彩色的(灰色/白色)。
答案 0 :(得分:0)
问题是我的image = Image.open(...)
被垃圾回收了,所以我不得不添加self.
以便它不会被破坏。请参见下面的代码段:
def resize_image(self, img_path):
self.image = Image.open(img_path)
w_coeff = self.image.width / self.grid_w
h_coeff = self.image.height / self.grid_h
w_coeff = 1 / w_coeff if w_coeff > 1 else w_coeff
h_coeff = 1 / h_coeff if h_coeff > 1 else h_coeff
# pick the smallest coeff to get the image as small
# as should be
coeff = min(w_coeff, h_coeff)
self.image = self.image.resize(
(int(self.image.width * coeff), int(self.image.height * coeff)), Image.ANTIALIAS)
return self.image
def show_image(self, resize_image):
self.image_tk = ImageTk.PhotoImage(resize_image)
self.can.create_image(0, 0, image=self.image_tk, anchor='center')