Last active
May 29, 2024 03:37
-
-
Save rise-worlds/5657a970a66fbf2588193b7b3e6d9387 to your computer and use it in GitHub Desktop.
code record
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# code record | |
## 以中心点旋转点.py | |
## todaybing.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from bs4 import BeautifulSoup | |
import sys | |
import os | |
from PIL import Image | |
from io import BytesIO | |
def debug(*value, sep=' ', end='\n', file=sys.stdout, flush=False): | |
if sys.argv[0].split('.')[-1] == "exe": | |
return | |
print(*value, sep=sep, end=end, file=file, flush=flush) | |
url = "https://www.todaybing.com/" | |
headers = { | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" | |
} | |
response = requests.get(url, headers=headers) | |
if response.status_code == 200: | |
debug("Success") | |
else: | |
debug("Error") | |
sys.exit() | |
soup = BeautifulSoup(response.text, "html.parser") | |
# debug(soup.prettify()) | |
# use query selector to find the div with class "today-bing" | |
a = soup.select_one("i.text-sm.iconfont.icon-download.mr-4").parent | |
img_url = a["href"] | |
debug(img_url) | |
# download the image | |
response = requests.get(img_url, headers=headers) | |
if response.status_code == 200: | |
Image.open(BytesIO(response.content)).convert('RGBA').save("bing.png") | |
debug("Image saved") | |
else: | |
debug("Error downloading image") | |
sys.exit() | |
# set the wallpaper | |
import ctypes | |
SPI_SETDESKWALLPAPER = 20 | |
ctypes.windll.user32.SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0, os.path.abspath("bing.png"), 3) | |
debug("Wallpaper set") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def rotate_point_around_center(x, y, center_x, center_y, angle): | |
""" | |
以中心点旋转点 | |
Args: | |
x: 点的x坐标 | |
y: 点的y坐标 | |
center_x: 中心点的x坐标 | |
center_y: 中心点的y坐标 | |
angle: 旋转角度,单位为度 | |
Returns: | |
旋转后的点的坐标 | |
""" | |
angle = math.radians(angle) | |
x_prime = (y - center_y) * math.cos(angle) - (x - center_x) * math.sin(angle) + center_x | |
y_prime = (x - center_x) * math.cos(angle) + (y - center_y) * math.sin(angle) + center_y | |
# 交换x和y坐标 | |
x_prime, y_prime = y_prime, x_prime | |
return x_prime, y_prime | |
x = 690 | |
y = 350 | |
image_width = 720 | |
image_height = 1280 | |
center_x = image_width / 2 | |
center_y = image_height / 2 | |
angle = 90 | |
x_prime, y_prime = rotate_point_around_center(x, y, center_x, center_y, angle) | |
print(x_prime, y_prime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment