在label中添加pixmap来显示图片时,当图片过大时图片显示不全。

1.这时可以使用pixmap的scared()方法,来设置图片缩放。

QPixmap QPixmap.scaled (self, int w, int h, Qt.AspectRatioMode aspectMode = Qt.IgnoreAspectRatio, Qt.TransformationMode mode = Qt.FastTransformation)

Scales the pixmap to the given size, using the aspect ratio and transformation modes specified by aspectRatioMode and transformMode.

If aspectRatioMode is Qt.IgnoreAspectRatio, the pixmap is scaled to size.

If aspectRatioMode is Qt.KeepAspectRatio, the pixmap is scaled to a rectangle as large as possible inside size, preserving the aspect ratio.

If aspectRatioMode is Qt.KeepAspectRatioByExpanding, the pixmap is scaled to a rectangle as small as possible outside size, preserving the aspect ratio.

If the given size is empty, this function returns a null pixmap.

In some cases it can be more beneficial to draw the pixmap to a painter with a scale set rather than scaling the pixmap. This is the case when the painter is for instance based on OpenGL or when the scale factor changes rapidly.

2.可以使用QLabel.setScaledContents (self, bool)方法来使pixmap自适应label大小

测试代码:

#encoding:utf-8

'''

Created on 2016年7月10日

@author: Administrator

'''

from PyQt4.QtGui import *

from PyQt4.QtCore import *

import sys

class ImageFrame(QMainWindow):

def __init__(self):

super(ImageFrame, self).__init__()

self.initUI()

def initUI(self):

#窗体设置

self.setGeometry(100, 100, 500, 400)

self.setMaximumSize(500, 400)

self.setMinimumSize(500, 400)

self.setVisible(True)

self.statusBar()

image = QAction(QIcon('open.png'), 'open', self)

image.setShortcut('ctrl+o')

image.setStatusTip('open new image')

self.connect(image, SIGNAL('triggered()'), self.openImage)

toolbar = self.addToolBar('image')

toolbar.addAction(image)

label = QLabel()

label.setGeometry(0, 0, 400, 400)

self.setCentralWidget(label)

layout = QGridLayout()

self.label1 = QLabel()

self.label1.setGeometry(0, 0, 200, 200)

#设置label对齐方式

self.label1.setAlignment(Qt.AlignLeft)

button = QPushButton('edit')

edit = QLineEdit()

layout.addWidget(self.label1, 0, 0)

layout.addWidget(edit, 1, 0)

layout.addWidget(button, 1, 1)

label.setLayout(layout)

self.updataImage()

def openImage(self):

imageName = QFileDialog.getOpenFileName(self,"Open file dialog","/","jpg files(*.jpg)")

self.updataImage(imageName)

def updataImage(self, imageName = 'icon.png'):

pixmap = QPixmap(imageName)

'''图像缩放:使用pixmap的scare方法,参数aspectRatioMode=Qt.KeepAspectRatio设置为等比例缩放,

aspectRatioMode=Qt.IgnoreAspectRatio为不按比例缩放'''

scaredPixmap = pixmap.scaled(400, 400, aspectRatioMode=Qt.KeepAspectRatio)

#图像缩放:使用label的setScaledContents(True)方法,自适应label大小

#self.label1.setScaledContents(True)

print pixmap.height()

print pixmap.width()

self.label1.setPixmap(scaredPixmap)

python自适应图片大小_pixmap和label设置图片自适应大小相关推荐

  1. python opencv 图像大小_python使用OpenCV设置图片尺寸

    1.显示图像:使用imshow()函数 如果使用过其 他 GUI 框架背景,就会很自然第调用 imshow()来显示一幅图像. imshow()函数有两个参数: 显示图像的帧名称以及要显示的图像本身. ...

  2. Kindeditor 图片粘贴上传后设置图片属性

    之前转载过一篇 Kindeditor 图片粘贴上传(https://blog.csdn.net/Dongguabai/article/details/100116608)的博客.但是还是存在一个问题, ...

  3. html怎么让图片变灰色,利用CSS设置图片黑白/灰色效果,同时适用于整站变灰

    写页面的时候由于页面风格原因,可能需要图片变灰色,如果用图片处理软件的话可能会比较麻烦,而且会增加图片的数量,不利于资源的合理利用. 下面提供两种方法: 1.使用CSS的 filter: gray;  ...

  4. 系统计算机大小怎么更改,调整设置电脑图标大小的方法

    所谓的计算机图标是具有明确指代含义的计算机图形.其中桌面图标是软件标识,界面中的图标是功能标识.下面是学习啦小编为大家整理的关于调整设置电脑图标大小的方法,一起来看看吧! 调整设置电脑图标大小的方法 ...

  5. html如何让图片跟字体重叠,CSS设置图片与文字的间距

    [实例介绍] CSS设置图片与文字的间距 希望图片本身与文字有一定的距离只需要给标记添加margin属性即可.margin属性包括margin.top.margin-right,margin-bott ...

  6. markdown统一设置图片格式_Markdown中如何设置图片居中

    Markdown默认并不支持排版,排版需要借助HTML/CSS来实现 遇到这个问题时,网上搜索发现很多人都有相同需求,但看到的答案却大多不怎么满意,因此这里整理一下.说是Markdown中图片居中,但 ...

  7. css如何让图片不平铺,css怎么设置图片平铺方式?

    background-repeat属性是用来设置背景图像如何平铺的.默认地,背景图像在水平和垂直方向上重复. 属性值:repeat:即默认方式,完全平铺背景: no-repeat:在X及Y轴方向均不平 ...

  8. java pdf 用系统字体大小_java 用itext设置pdf纸张大小操作_Java_软件编程

    做快递面单打印模板,快递要求纸张大小100 x 150mm. PageSize.A4=595 x 842 A4尺寸=210mm×297mm 故设置纸张大小: public static final R ...

  9. windows小技巧_1 设置图片查看器默认自适应缩放

    1 问题描述 在查看图片,尤其是查看大量图片时,我们往往希望图片能够随着查看窗口的大小自适应缩放.但在实际操作中,windows自带的图片查看器却默认设置图片为1:1查看.当然,可以使用快捷键ctrl ...

最新文章

  1. 《JAVA程序设计》第七周学习总结
  2. Redis资料汇总专题
  3. javascript数据结构-介绍
  4. MinGW:sjlj vs dwarf vs seh有什么区别?
  5. Java中字符串以小数点分割时无效,String s = “12.34“; String[] ss = split(“.“);分割结果为空
  6. .Net常用正则判断方法
  7. apache ignite_Apache Ignite本机持久性,简要概述
  8. linux 2.6 hash表作用,高性能分布式哈希表FastDHT介绍及安装配置
  9. jQuery中的元素操作
  10. [专栏精选]Unity中动态构建NavMesh
  11. Quartz总结(四):动态修改定时器二
  12. C++11新特性之std::bind()
  13. 【Java基础知识 17】聊一聊同步代码块
  14. 考勤打卡记录数据库表结构_中控zktime5.0考勤管理系统数据库表结构.doc
  15. 一篇文章带你认识数学建模中的图论模型
  16. 关于iPhone 6S你想知道的一切
  17. 人生第一份跳槽面试经历
  18. 【那些年,我们一起追得女孩】第十五章
  19. 熟练计算机数字键盘,总结:计算机键盘的盲打和快速输入技巧[1]
  20. .sh(shell)文件打印文件夹下所有文件的文件名

热门文章

  1. weka中的arff格式数据
  2. 如何快速连接别人共享的打印机?
  3. OpenPose(一):根据关键点生成置信图(Confidence Map)
  4. 皓月战地3不显示服务器,【求助】登录皓月服出错。。
  5. Python入门第7课——tuple变量(只读课堂)
  6. c语言中输入scanf与getchar的啃爹之处
  7. java线程实现磁力链接_Aria2磁力下载工具官方版
  8. 用 Windows Media Center 免费看大片 (一)
  9. android驱动开发 老罗,在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序(老罗学习笔记3)...
  10. 《软技能--代码之外的生存指南》读书笔记