在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)

pixmap和label设置图片自适应大小的更多相关文章

  1. css控制图片自适应大小

    相信大家做网页时经常会碰到大分辨率的图片会把表格涨破以致漂亮的网页面目全非,但只要使用以下的CSS语句即可解决.      该CSS的功能是:大于600的图片自动调整为600显示. <style ...

  2. iOS HTML 字符串中的图片 自适应大小

    本文原文地址:http://www.cnblogs.com/qianLL/p/6095988.html 有时候 我们接收数据的时候  后台给的数据室一串HTML 的字符串  但是 我们要显示出来  这 ...

  3. Android代码中动态设置图片的大小(自动缩放),位置

    项目中需要用到在代码中动态调整图片的位置和设置图片大小,能自动缩放图片,用ImageView控件,具体做法如下: 1.布局文件 <RelativeLayout xmlns:android=&qu ...

  4. Android drawableleft drawableTop 设置图片的大小

    例子: Drawable drawable=getResources().getDrawable(R.drawable.xx); //获取图片 drawable.setBounds(left, top ...

  5. 设置图片自适应DIV大小

    可以利用CSS样式表中表示后代的复合选择器进行设置.例: <head> <style type="text/css"> #right img /*设定box ...

  6. android设置图片自适应控件大小

    在XML文件的ImageView属性中加上:android:scaleType="fitXY"

  7. word 2013 粘贴的图片自适应大小

    1.先切换到页面视图 2.粘贴图片进去,成功自适应,像素不变,可右键图片另存为图片,查看原始图片,或者ctrl+滚轮上放大. 3.在其他视图就会出现超出范围的情况,还要自己调整

  8. 纯js实现div内图片自适应大小

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Winfrom PictureBox 设置图片自适应

    初始状态 Bitmap bm = new Bitmap(Image.FromStream(System.Net.WebRequest.Create(new Uri(result.Result)).Ge ...

随机推荐

  1. 手动更新nexus的索引

    安装nexus 1.下载 源码包 nexus-2.13.0-01-bundle.tar.gz 下载地址 http://www.sonatype.org/nexus/ 2.解压源码包 3.启动并访问 . ...

  2. bzoj 3143 随机游走

    题意: 给一个简单无向图,一个人从1号节点开始随机游走(即以相同概率走向与它相邻的点),走到n便停止,问每条边期望走的步数. 首先求出每个点期望走到的次数,每条边自然是从它的两个端点走来. /**** ...

  3. xtrabackup备份MySQL并主从同步

    为什么要使用xtarbackup? mysqldump备份数据库的时候,会锁库锁表,导致业务服务的暂时停滞,数据库数量小还没有感觉,当数据超过几个G的时候,使用mysqldump会严重影响服务器性能, ...

  4. ERROR: While executing gem … (Gem::RemoteFetcher::FetchError)

    原文地址:https://www.zfanw.com/blog/error-while-executing-gem-gem-remote-fetch-error.html 我对命令行下安装 gem 包 ...

  5. C#编程(八十一)---------- 捕获异常

    捕获异常 前面主要说了关于异常的一些基础和理论知识,没有进入到正真的异常案例,这一讲通过几个案例来描述一下异常的捕获和处理. 案例代码: using System; using System.Coll ...

  6. Asp.Net Core中Json序列化处理整理

    一.Asp.Net Core中的Json序列化处理使用的是Newtonsoft.Json,更多参考:C# Newtonsoft.Json JsonSerializerSettings配置序列化操作,C ...

  7. ubuntu16 64位 编译64位程序和32位程序

    安装了ubuntu16 64位的系统,想在该环境下用gcc编译64位和32位的程序 默认已经安装了64位环境的gcc 1. 首先确认安装的环境是不是64位的 cocoa@cocoaUKlyn:~/De ...

  8. 使用Genymotion模拟器调试出现INSTALL_FAILED_CPU_ABI_INCOMPATIBLE错误的解决办法

    如果遇到下面这种错误: 点击下载Genymotion-ARM-Translation.zip 百度云连接:http://pan.baidu.com/s/1o6ifjMM 将你的虚拟器启动起来,将下载好 ...

  9. CentOS---网络配置详解 (转)

    一.配置文件详解在RHEL或者CentOS等Redhat系的Linux系统里,跟网络有关的主要设置文件如下: /etc/host.conf         配置域名服务客户端的控制文件/etc/hos ...

  10. C#中Post请求的两种方式发送参数链和Body的

    POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...