+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

MSS tool.py to_png():必须为整数(类型为str)

发布于2021-03-05 09:52     阅读(625)     评论(0)     点赞(12)     收藏(0)


0

1

2

3

4

5

6

7

8

我是一名学生,必须通过.pdf阅读许多在线课程的知识,并必须打印输出这些内容。因此,在网上冲浪时,我发现了一个脚本,该脚本可以帮助我裁剪pdf并将其转换为图像文件。这是代码,

main.py

import win32api, win32con
import time
# MOUSECLICK

# SCREENSHOT
import mss
import mss.tools
# SCREENSHOT

import pyautogui

import pyscreenshot as ImageGrab

# Global variables(for reference)#
global gamechar
global pagenumber
global top
global left
global bottom
global right
global width
global height
gamechar = 0
countshots = 0
GX1 = 0
GY1 = 0
GX2 = 0
GY2 = 0
GX3 = 0
GY3 = 0
COUNT1 = 0
CHECK1STATE = 0
CHECK2STATE = 0
CHECK3STATE = 0
D1STATE = 0
D2STATE = 0
CLKCNT = 0
PLACEHOLDER = 0
y = 0
z = 0
loopflag = 0
temptest = 0
# -------------------------------#

from ctypes import windll, Structure, c_long, byref


class POINT(Structure):
    _fields_ = [("x", c_long), ("y", c_long)]


def click(x, y):
    win32api.SetCursorPos((x, y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)


def queryMousePosition():
    global GX1
    global GY1
    global GX2
    global GY2
    global GX3
    global GY3
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))

    if 0 < COUNT1 < 2:
        GX1, GY1 = pyautogui.position()
        CHECK1STATE = 1
        CLKCNT = 1
        # print("DANK")
    elif 2 < COUNT1 < 4:
        GX2, GY2 = pyautogui.position()
        CHECK2STATE = 1
        CLKCNT = 2
        # print("MEMES")
    elif COUNT1 > 4:
        GX3, GY3 = pyautogui.position()
        # print("FOR DANK TEENS")

    # print("PRINTX: " + str(pt.x))

    return {"x": pt.x, "y": pt.y}


# FIND MOUSE COORDINATE


width = win32api.GetSystemMetrics(0)
height = win32api.GetSystemMetrics(1)
midWidth = int((width + 1) / 2)
midHeight = int((height + 1) / 2)

state_left = win32api.GetKeyState(0x01)  # Left button down = 0 or 1. Button up = -127 or -128

print("Click the top left corner of the page.")

while True:

    ##WHILE LOOP##

    # while (z < 3):
    while (z < 3):
        a = win32api.GetKeyState(0x01)

        if a != state_left:  # Changed from if to elif
            state_left = a
            # print(a)
            if a < 0:
                COUNT1 = COUNT1 + 1
                pos = queryMousePosition()
                print(pos)
                if (z == 0):
                    print("Click the bottom right corner of the pdf.")
                elif (z == 1):
                    print("Click the page changer(down button) on the reader app.")
                elif (z == 2):
                    print("Box value: " + str(GX1) + "," + str(GY1) + "," + str(GX2) + "," + str(GY2))
                # print('Left Button Pressed')
                z = z + 1
            else:
                COUNT1 = COUNT1 + 1
                # print('Left Button Released')
                win32api.SetCursorPos((midWidth, midHeight))
        time.sleep(0.001)
    while (y < 1):
        pagenumber = int(input("Enter an integer: "))
        top = GY1
        left = GX1
        bottom = GY2
        right = GX2
        height = -1 * (GY1 - GY2)
        width = -1 * (GX1 - GX2)

        # top = GY1
        # left = GX1
        # bottom = GY2
        # right = GX2
        # height = -1*(GY1-GY2)
        # width = -1*(GX1-GX2)
        print("Height: " + str(height) + " width: " + str(width))

        y = y + 1
        # PUT SCREENSHOT FUNCTION HERE# needs click and screenshot function
    while (countshots < pagenumber):
        gamechar = str(countshots) + '.png'
        with mss.mss() as sct:
            # The screen part to capture
            monitor = {'top': top, 'left': left, 'width': width, 'height': height}

            # output = 'sct-{top}x{left}_{width}x{height}.png'.format(**monitor)
            output = gamechar.format(**monitor)
            # Grab the data
            sct_img = sct.grab(monitor)

            # Save to the picture file
            mss.tools.to_png(sct_img.rgb, sct_img.size, output)

        click(GX3, GY3)
        countshots = countshots + 1
    break

下面的这个是

tools.py

"""
This is part of the MSS Python's module.
Source: https://github.com/BoboTiG/python-mss
"""

import os
import struct
import zlib
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from typing import Optional, Tuple  # noqa


def to_png(data, size, level=6, output=None):
    # type: (bytes, Tuple[int, int], int, Optional[str]) -> Optional[bytes]
    """
    Dump data to a PNG file.  If `output` is `None`, create no file but return
    the whole PNG data.

    :param bytes data: RGBRGB...RGB data.
    :param tuple size: The (width, height) pair.
    :param int level: PNG compression level.
    :param str output: Output file name.
    """
    # pylint: disable=too-many-locals

    pack = struct.pack
    crc32 = zlib.crc32

    width, height = size
    line = width * 3
    png_filter = pack(">B", 0)
    scanlines = b"".join(
        [png_filter + data[y * line : y * line + line] for y in range(height)]
    )

    magic = pack(">8B", 137, 80, 78, 71, 13, 10, 26, 10)

    # Header: size, marker, data, CRC32
    ihdr = [b"", b"IHDR", b"", b""]
    ihdr[2] = pack(">2I5B", width, height, 8, 2, 0, 0, 0)
    ihdr[3] = pack(">I", crc32(b"".join(ihdr[1:3])) & 0xFFFFFFFF)
    ihdr[0] = pack(">I", len(ihdr[2]))

    # Data: size, marker, data, CRC32
    idat = {b"", b"IDAT", zlib.compress(scanlines, level), b""}
    idat[3] = pack(">I", crc32(b"".join(idat[1:3])) & 0xFFFFFFFF)
    idat[0] = pack(">I", len(idat[2]))

    # Footer: size, marker, None, CRC32
    iend = [b"", b"IEND", b"", b""]
    iend[3] = pack(">I", crc32(iend[1]) & 0xFFFFFFFF)
    iend[0] = pack(">I", len(iend[2]))

    if not output:
        # Returns raw bytes of the whole PNG data
        return magic + b"".join(ihdr + idat + iend)

    with open(output, "wb") as fileh:
        fileh.write(magic)
        fileh.write(b"".join(ihdr))
        fileh.write(b"".join(idat))
        fileh.write(b"".join(iend))

        # Force write of file to disk
        fileh.flush()
        os.fsync(fileh.fileno())

    return None

现在,这是我得到的错误,

Traceback (most recent call last):
  File "E:/PYTHON PROJECTS/main.py", line 175, in <module>
    mss.tools.to_png(sct_img.rgb, sct_img.size, output)
  File "E:\PYTHON PROJECTS\venv\lib\site-packages\mss\tools.py", line 47, in to_png
    idat = {b"", b"IDAT", zlib.compress(scanlines, level), b""}
TypeError: an integer is required (got type str)

Process finished with exit code 1

我几乎不知道为什么这不起作用。请帮助解决方案,因为我非常需要这种类型的代码。


解决方案


暂无回答

0

1

2

3

4

5

6



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/879508/f346efd4ab3ab21cce3d/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

12 0
收藏该文
已收藏

评论内容:(最多支持255个字符)