找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 22485|回复: 7

[已解决] 怎么让图片自适应当前窗口

[复制链接]
发表于 2019-8-22 13:57:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
本帖最后由 老司机飞飞飞 于 2019-8-25 13:44 编辑

想显示个背景图,因为尺寸不同显示不全
所以怎么让图片自适应窗口,或者让窗口自适应图片,有没有大佬解答下
 楼主| 发表于 2019-8-24 17:17:24 | 显示全部楼层
本帖最后由 老司机飞飞飞 于 2019-9-4 16:30 编辑
老司机飞飞飞 发表于 2019-8-23 23:16
[RenPy] 纯文本查看 复制代码
class ProportionalScale(im.ImageBase):
        '''Resizes a renpy image to f ...[/quote]
[mw_shl_code=renpy,true]init python:

    ## Images are declared using an auto-generating function
    ## 使用自动生成函数声明图像

    class ProportionalScale(im.ImageBase):
        '''Resizes a renpy image to fit into the specified width and height.
        The aspect ratio of the image will be conserved.'''
        #调整renpy图像的大小以适应指定的宽度和高度。
        #长宽比的图像将被保存。

        def __init__(self, imgname, maxwidth, maxheight, bilinear=True, **properties):
            img = im.image(imgname)
            super(ProportionalScale, self).__init__(img, maxwidth, maxheight, bilinear, **properties)
            self.imgname = imgname
            self.image = img
            self.maxwidth = int(maxwidth)
            self.maxheight = int(maxheight)
            self.bilinear = bilinear

        def load(self):
            child = im.cache.get(self.image)
            width, height = child.get_size()

            ratio = min(self.maxwidth/float(width), self.maxheight/float(height))
            width = ratio * width
            height = ratio * height

            if self.bilinear:
                try:
                    renpy.display.render.blit_lock.acquire()
                    rv = renpy.display.scale.smoothscale(child, (width, height))
                finally:
                    renpy.display.render.blit_lock.release()
            else:
                try:
                    renpy.display.render.blit_lock.acquire()
                    rv = renpy.display.pgrender.transform_scale(child, (newwidth, newheight))
                finally:
                    renpy.display.render.blit_lock.release()
            return rv

        def predict_files(self):
            return self.image.predict_files()

    def declare(name, img, method=None, x=config.screen_width, y=config.screen_height, wide=False, gallery=True, unlock=False):
    # img is the complete image path (from the game folder root) img是完整的图像路径(来自游戏文件夹根目录)

        # if wide:
        #     y = int(y*0.8)


        if method == "s": # Scale method (image will fit the exact target dimensions - not proportional) 比例尺法(图像将适合准确的目标尺寸-不成比例)
            renpy.image(name, im.Scale(img, x, y))

        elif method == "p": # ProportionalScale method (image will fit the target dimensions while preserving its aspect ratio) 比例比例尺法(图像拟合目标尺寸,同时保持长宽比)
            renpy.image(name, ProportionalScale(img,x, y))

        elif method == "f": # Factor Scale (image dimensions will change proportionately to float numbers x and y) 因子比例(图像尺寸将按比例变化为浮点数x和y)

            # Foolproofing
            if x == config.screen_width:
                x = 1.0
            if y == config.screen_height:
                y = 1.0

            renpy.image(name, im.FactorScale(img, x, y))

        else: # No change to the original image 原图不变
            renpy.image(name, img)

        if unlock:
            unlock_pic(name)

        if gallery: # Returns image name if the image is to be stored in a gallery 如果要将图像存储在图库中,则返回图像名称
            return name
        else:
            return None

    # 声明多个
    def declare_multiple(base_name, base_img, method=None, start=0, finish=0, series=[], x=config.screen_width, y=config.screen_height, wide=False, gallery=True, unlock=False, loud=False):
        r = []

        if not series:
            series = range(start, finish+1)

        for nb in series:
            name, img = base_name % str(nb), base_img % str(nb)

            r.append(declare(name, img, method=method, x=x, y=y, wide=wide, gallery=gallery, unlock=unlock))

        return r # Python 2.7 won't allow me to unpack it. Darn. Python 2.7不允许我打开它。该死的。


#### IMAGE DECLARATIONS 图像的声明####

# These dicts are used for generating CG galleries. Pictures will be displayed in the order they appear. Pictures with 'gallery' set to False will not appear in galleries.
# Each key is a separate button
# 这些dicts用于生成CG图库。图片将按照它们出现的顺序显示。将“gallery”设置为False的图片将不会出现在画廊中。
# 每个键都是一个单独的按钮

    game_image_dict = {

                    ## BACKGROUNDS 背景图片##

                    "bg" : [
                            declare("bg 1", "images/bg/1.png", "p", wide=True),
                            declare("bg 2", "images/bg/2.png", "p", wide=True),
                            declare("bg 3", "images/bg/3.png", "p", wide=True),
                            declare("bg 4", "images/bg/4.png", "p", wide=True),
                            declare("bg 5", "images/bg/5.png", "p", wide=True),
                            declare("bg 6", "images/bg/6.png", "p", wide=True),
                            declare("bg 7", "images/bg/7.png", "p", wide=True),
                            declare("bg 8", "images/bg/8.png", "p", wide=True),
                            declare("bg 9", "images/bg/9.png", "p", wide=True),
                            declare("bg 10", "images/bg/10.png", "p", wide=True),
                            ],

                    }

    #添加图片(名字,图片路径,图片格式,图片数量,方法(s、p、f)),
    #图片为1.png,2.png时img = "comic/cltg2/
    #xxx1.png,xxx2png时img = "comic/cltg2/xxx"
    #或者修改代码
    def add_image(name,img_path,format,num,method):
        img_list = []
        a = 1
        while a <= num:
            name1 = name + "_" +str(a)
            img1 = img_path + str(a) + format
            method1 = method
            b = declare(name1,img1,method1,wide=True)
            img_list.append(b)
            a += 1
        return img_list

    #添加cltg2到game_iamge_dict
    game_image_dict["z1"] = add_image("z1","images/comic/z1/",".png",46,"p")
    game_image_dict["z2"] = add_image("z2","images/comic/z2/",".png",59,"p")
    game_image_dict["cltg1"] = add_image("cltg1","images/comic/cltg1/",".png",46,"p")
    game_image_dict["cltg2"] = add_image("cltg2","images/comic/cltg2/",".png",41,"p")

更具体点的

回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2019-8-23 00:28:59 | 显示全部楼层
[RenPy] 纯文本查看 复制代码
init python:
    import io
    from PIL import Image, ImageTk

    def resize( w_box, h_box, pil_image): #参数是:要适应的窗口宽、高、Image.open后的图片
      w, h = pil_image.size #获取图像的原始大小
      f1 = 1.0*w_box/w
      f2 = 1.0*h_box/h
      factor = min([f1, f2])
      width = int(w*factor)
      height = int(h*factor)
      return pil_image.resize((width, height), Image.ANTIALIAS)

label start():
    $ pil_image = Image.open(r"E:\bcrj\Ren'Py\ceshi\game\images\1_1.png")
    $ pil_image_resized = resize( 1280, 720, pil_image)
    $ tk_image = ImageTk.PhotoImage(pil_image_resized)
    show tk_image

到网上找了下,发现上面这段代码
但是一运行就报cannot import name_imaging,但我在pycharm中跑了下又没任何问题
有没有大佬知道什么问题
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2019-8-23 23:16:05 | 显示全部楼层
[RenPy] 纯文本查看 复制代码
class ProportionalScale(im.ImageBase):
        '''Resizes a renpy image to fit into the specified width and height.
        The aspect ratio of the image will be conserved.'''
        #调整renpy图像的大小以适应指定的宽度和高度。
        #长宽比的图像将被保存。

        def __init__(self, imgname, maxwidth, maxheight, bilinear=True, **properties):
            img = im.image(imgname)
            super(ProportionalScale, self).__init__(img, maxwidth, maxheight, bilinear, **properties)
            self.imgname = imgname
            self.image = img
            self.maxwidth = int(maxwidth)
            self.maxheight = int(maxheight)
            self.bilinear = bilinear

        def load(self):
            child = im.cache.get(self.image)
            width, height = child.get_size()

            ratio = min(self.maxwidth/float(width), self.maxheight/float(height))
            width = ratio * width
            height = ratio * height

            if self.bilinear:
                try:
                    renpy.display.render.blit_lock.acquire()
                    rv = renpy.display.scale.smoothscale(child, (width, height))
                finally:
                    renpy.display.render.blit_lock.release()
            else:
                try:
                    renpy.display.render.blit_lock.acquire()
                    rv = renpy.display.pgrender.transform_scale(child, (newwidth, newheight))
                finally:
                    renpy.display.render.blit_lock.release()
            return rv

        def predict_files(self):
            return self.image.predict_files()

翻了人家大神写的游戏,终于找到了
回复 支持 抱歉

使用道具 举报

发表于 2019-9-3 20:18:39 | 显示全部楼层
请问楼主,这段代码放到哪个地方呢?新手请教
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2019-9-4 12:59:05 | 显示全部楼层
lhm7844 发表于 2019-9-3 20:18
请问楼主,这段代码放到哪个地方呢?新手请教

随便放在哪都行,你新建个class.rpy文件放在里面就行了
回复 支持 抱歉

使用道具 举报

发表于 2019-9-4 16:28:58 | 显示全部楼层
@楼主。非常感谢楼主。不过还是无法使用,需要对代码里面的内容根据实际情况修改一下吗?改哪里?头三句不能读,我每句前面打了#,还是显示错误:说7,16,38行都有错。
回复 支持 抱歉

使用道具 举报

 楼主| 发表于 2019-9-4 16:33:48 | 显示全部楼层
本帖最后由 老司机飞飞飞 于 2019-9-4 16:35 编辑
lhm7844 发表于 2019-9-4 16:28
@楼主。非常感谢楼主。不过还是无法使用,需要对代码里面的内容根据实际情况修改一下吗?改哪里?头三句不 ...

从init python往下复制就行了,前面的[mw_shl_code=renpy,true]一大堆不要管它,添加图片那你要根据自己的实际情况添加
回复 支持 抱歉

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|RenPy中文空间 ( 苏ICP备17067825号|苏公网安备 32092302000068号 )

GMT+8, 2024-11-23 15:16 , Processed in 0.123071 second(s), 27 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表