Hướng dẫn how do i find the dpi of an image in python? - làm cách nào để tìm dpi của hình ảnh trong python?

Ngoài ra đang thay đổi DPI bằng cách sử dụng (sử dụng PIL tại đây)

def get_dpi_group(dpi):
    # type: (int) -> Density
    """Get viewport density group from given dpi.

    :type dpi: int
    :return: viewport density group enum
    :rtype: Density
    """
    if dpi in range(0, 121):
        return Density.XLOW
    elif dpi in range(121, 161):
        return Density.LOW
    elif dpi in range(161, 241):
        return Density.MEDIUM
    elif dpi in range(241, 321):
        return Density.HIGH
    elif dpi in range(321, 481):
        return Density.XHIGH
    elif dpi >= 481:
        return Density.XXHIGH

    raise AskSdkException("Unknown dpi group value: {}".format(dpi)) 
5 hữu ích cho mục đích OCR?Tôi đọc rằng tối thiểu 300 ppi (cái được gọi là độ phân giải cao) là bắt buộc đối với các động cơ OCR cho độ chính xác tốt, do đó, thay đổi nó theo cách này từ (72, 72) thành (600, 600) có ý nghĩa, và cũng có nghĩa là gìKhi tôi thực hiện thao tác này, các pixel của tôi có kích thước nhỏ hơn từ 1/72 đến 1/300 bằng cách phá vỡ các pixel lớn (cũng có hữu ích) không?dpi using (using PIL here)
def get_dpi_group(dpi):
    # type: (int) -> Density
    """Get viewport density group from given dpi.

    :type dpi: int
    :return: viewport density group enum
    :rtype: Density
    """
    if dpi in range(0, 121):
        return Density.XLOW
    elif dpi in range(121, 161):
        return Density.LOW
    elif dpi in range(161, 241):
        return Density.MEDIUM
    elif dpi in range(241, 321):
        return Density.HIGH
    elif dpi in range(321, 481):
        return Density.XHIGH
    elif dpi >= 481:
        return Density.XXHIGH

    raise AskSdkException("Unknown dpi group value: {}".format(dpi)) 
5 helpful for OCR purposes? I read that a minimum of 300 ppi (what is called high resolution) is required for OCR engines for good accuracy, so is changing it this way from (72, 72) to (600, 600) meaningful, and also what does it mean when I do perform this operation, do my pixels get smaller in size from 1/72 to 1/300 by breaking up the large pixels ( also is it helpful)?

- & nbsp; Aspirs1

Ngày 26 tháng 5 năm 2020 lúc 6:38

25 ví dụ mã Python được tìm thấy liên quan đến "Nhận DPI".Bạn có thể bỏ phiếu cho những người bạn thích hoặc bỏ phiếu cho những người bạn không thích và đi đến dự án gốc hoặc tệp nguồn bằng cách theo các liên kết trên mỗi ví dụ. get dpi". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

ví dụ 1

def get_dpi():
    """Returns an int of current DPI for windows or None."""
    if platform.system() == 'Windows':
        version = get_version()
        if version >= 6.2:
            # Windows 8.0 is 6.2.9200
            # Only run on windows 8 or higher to check high dpi monitors

            user32 = ctypes.windll.user32
            w_curr = user32.GetSystemMetrics(0)
            user32.SetProcessDPIAware()
            w_phys = user32.GetSystemMetrics(0)
            curr_dpi = round(w_phys*96/w_curr, 0)

            from pylab import rcParams
            rcParams['figure.dpi'] = curr_dpi
            return curr_dpi
    else:
        return None 

Ví dụ 2

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 

Ví dụ 3

def get_dpi_group(dpi):
    # type: (int) -> Density
    """Get viewport density group from given dpi.

    :type dpi: int
    :return: viewport density group enum
    :rtype: Density
    """
    if dpi in range(0, 121):
        return Density.XLOW
    elif dpi in range(121, 161):
        return Density.LOW
    elif dpi in range(161, 241):
        return Density.MEDIUM
    elif dpi in range(241, 321):
        return Density.HIGH
    elif dpi in range(321, 481):
        return Density.XHIGH
    elif dpi >= 481:
        return Density.XXHIGH

    raise AskSdkException("Unknown dpi group value: {}".format(dpi)) 

Ví dụ 4

def get_dpi_ratio(self, context):
        """
        Gets the ratio of physical pixels to logical pixels for the given HTML
        Canvas context.

        This is typically 2 on a HiDPI ("Retina") display, and 1 otherwise.
        """
        backing_store = (
            getattr(context, "backingStorePixelRatio", 0)
            or getattr(context, "webkitBackingStorePixel", 0)
            or getattr(context, "mozBackingStorePixelRatio", 0)
            or getattr(context, "msBackingStorePixelRatio", 0)
            or getattr(context, "oBackingStorePixelRatio", 0)
            or getattr(context, "backendStorePixelRatio", 0)
            or 1
        )
        return (getattr(window, "devicePixelRatio", 0) or 1) / backing_store 

Ví dụ 5

def get_dpi_xy_byte(self):
    """
    get the DPI on the mouse

    :return: List of X, Y DPI
    :rtype: list of int
    """
    self.logger.debug("DBus call get_dpi_both")

    driver_path = self.get_driver_path('dpi')

    with open(driver_path, 'r') as driver_file:
        result = driver_file.read()
        dpi_x, dpi_y = [int(dpi) for dpi in result.strip().split(':')]

    dpi_x = int(round(dpi_x / 255 * 6750, 2))
    dpi_y = int(round(dpi_y / 255 * 6750, 2))

    return [dpi_x, dpi_y] 

Ví dụ 6

def getImgDpi(self, im):
        if isinstance(im,np.ndarray):
            shape = im.shape
        else:
            if isinstance(im,bytes): im = BytesIO(im)
            with Image.open(im) as pilimg:
                try:
                    dpi = [float(n) for n in pilimg.info["dpi"]]
                except KeyError: pass
                else: 
                    dpi = np.average(dpi)
                    if dpi != 72: return dpi
                shape = pilimg.size
        # assume DIN A4
        imgDpi = round(max(shape)/11.7,-2)
        sys.stderr.write("Error detecting input image DPI. "
                    +"Assuming %f dpi. "%imgDpi
                    +"Specify parameter inputDpi!\n")
        return imgDpi 

Ví dụ 7

def get_dpi_xy(self):
    """
    get the DPI on the mouse

    :return: List of X, Y DPI
    :rtype: list of int
    """
    self.logger.debug("DBus call get_dpi_xy")

    driver_path = self.get_driver_path('dpi')

    with open(driver_path, 'r') as driver_file:
        result = driver_file.read()
        dpi = [int(dpi) for dpi in result.strip().split(':')]

    return dpi 

Ví dụ 8

def get_dpi(self):
        if self.device.has('dpi'):
            return self.device.dpi
        else:
            return -1 

Ví dụ 9

def get_dpi_cor(self):
        """
        dpi_cor is currently used for linewidth-related things and
        shink factor. Mutation scale is not affected by this.
        """

        return self._dpi_cor 

Ví dụ 10

def get_max_dpi(self):
        if self.device.has('dpi'):
            return self.device.max_dpi
        else:
            return -1 

Ví dụ 11

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
0

Ví dụ 12

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
1

Ví dụ 13

def get_dpi_cor(self):
        """
        dpi_cor is currently used for linewidth-related things and
        shink factor. Mutation scale is not affected by this.
        """

        return self._dpi_cor 

Ví dụ 14

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
3

Ví dụ 15

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
3

Ví dụ 16

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
3

Ví dụ 17

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
6

Ví dụ 18

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
7

Ví dụ 19

def get_dpi_cor(self):
        """
        dpi_cor is currently used for linewidth-related things and
        shink factor. Mutation scale is not affected by this.
        """

        return self._dpi_cor 

Ví dụ 20

def get_dpi_cor(self):
        """
        dpi_cor is currently used for linewidth-related things and
        shink factor. Mutation scale is not affected by this.
        """

        return self._dpi_cor 

Ví dụ 21

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
3

Ví dụ 22

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
3

Ví dụ 23

def get_dpi_group(dpi):
    # type: (int) -> Density
    """Get viewport density group from given dpi.

    :type dpi: int
    :return: viewport density group enum
    :rtype: Density
    """
    if dpi in range(0, 121):
        return Density.XLOW
    elif dpi in range(121, 161):
        return Density.LOW
    elif dpi in range(161, 241):
        return Density.MEDIUM
    elif dpi in range(241, 321):
        return Density.HIGH
    elif dpi in range(321, 481):
        return Density.XHIGH
    elif dpi >= 481:
        return Density.XXHIGH

    raise AskSdkException("Unknown dpi group value: {}".format(dpi)) 
2

Ví dụ 24

def get_dpi_group(dpi):
    # type: (int) -> Density
    """Get viewport density group from given dpi.

    :type dpi: int
    :return: viewport density group enum
    :rtype: Density
    """
    if dpi in range(0, 121):
        return Density.XLOW
    elif dpi in range(121, 161):
        return Density.LOW
    elif dpi in range(161, 241):
        return Density.MEDIUM
    elif dpi in range(241, 321):
        return Density.HIGH
    elif dpi in range(321, 481):
        return Density.XHIGH
    elif dpi >= 481:
        return Density.XXHIGH

    raise AskSdkException("Unknown dpi group value: {}".format(dpi)) 
3

Ví dụ 25

def get_device_dpi(self, adb_device=None):
    """Returns the dpi of a device connected to adb.

    Args:
      adb_device: Serial of the device to get the dpi from. If none specified,
        the only device connected will be used.

    Returns:
      int of the device's pixel density.
    """
    adb_path = self._find_binary(BuildEnvironment.ADB)
    device = self.check_adb_devices(adb_device=adb_device)
    adb_device_arg = self.get_adb_device_argument(adb_device=device.serial)
    density, _ = self.run_subprocess(('%s %s shell getprop ro.sf.lcd_density' %
                                      (adb_path, adb_device_arg)
                                     ),
                                     shell=True,
                                     capture=True)
    return int(density) 
3