超级实用的python代码

 2023-10-20 16:19:37  阅读 0

1、创建二维码
import pyqrcode
import png
from pyqrcode import QRCode

# Text which is to be converted to QR code
print("Enter text to convert")
s = input(": ")
# Name of QR code png file
print("Enter image name to save")
n = input(": ")
# Adding extension as .pnf
d = n + ".png"
# Creating QR code
url = pyqrcode.create(s)
# Saving QR code as a png file
url.show()
url.png(d, scale=6)

2、从图片中截取文字
# extract text from a img and its coordinates using the pytesseract module
import cv2
import pytesseract

# You need to add tesseract binary dependency to system variable for this to work

img = cv2.imread("img.png")
# We need to convert the img into RGB format
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

hI, wI, k = img.shape
print(pytesseract.image_to_string(img))
boxes = pytesseract.image_to_boxes(img)
for b in boxes.splitlines():
      b = b.split(" ")
      x, y, w, h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
      cv2.rectangle(img, (x, hI - y), (w, hI - h), (0, 0, 255), 0.2)

cv2.imshow("img", img)
cv2.waitKey(0)

3、判断闰年
def is_leap(year):
      leap = False
      if year % 4 == 0:
           leap = True
           if year % 100 == 0:
                leap = False
                if year % 400 == 0:
                     leap = True
       return leap

year = int(input("Enter the year here: "))
print(is_leap(year))

4、打印图片分辨率
def jpeg_res(filename):
      """"This function prints the resolution of the jpeg image file passed into it"""

       # open image for reading in binary mode
       with open(filename,'rb') as img_file:

               # height of image (in 2 bytes) is at 164th position
                img_file.seek(163)

                # read the 2 bytes
                a = img_file.read(2)

                # calculate height
                height = (a[0] << 8) + a[1]

                # next 2 bytes is width
                a = img_file.read(2)

                # calculate width
                width = (a[0] << 8) + a[1]

      print("The resolution of the image is",width,"x",height)

jpeg_res("img1.jpg")

标签:

如本站内容信息有侵犯到您的权益请联系我们删除,谢谢!!


Copyright © 2020 All Rights Reserved 京ICP5741267-1号 统计代码