Compare commits
3 Commits
e7a579e168
...
cc0c22c68b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc0c22c68b | ||
|
|
b4b5a0e68a | ||
|
|
1bea995dc1 |
50
scripts/grayscale.py
Normal file
50
scripts/grayscale.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
import sys
|
||||||
|
import numpy as np
|
||||||
|
import cv2 # opencv-python
|
||||||
|
|
||||||
|
# 引入Python的可视化工具包 matplotlib
|
||||||
|
from matplotlib import pyplot as plt
|
||||||
|
|
||||||
|
|
||||||
|
def print_img_info(img):
|
||||||
|
print("================打印一下图像的属性================")
|
||||||
|
print("图像对象的类型 {}".format(type(img)))
|
||||||
|
print(img.shape)
|
||||||
|
print("图像宽度: {} pixels".format(img.shape[1]))
|
||||||
|
print("图像高度: {} pixels".format(img.shape[0]))
|
||||||
|
# GRAYScale 没有第三个维度哦, 所以这样会报错
|
||||||
|
# print("通道: {}".format(img.shape[2]))
|
||||||
|
print("图像分辨率: {}".format(img.size))
|
||||||
|
print("数据类型: {}".format(img.dtype))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
# 导入一张图像 模式为彩色图片
|
||||||
|
img_path = sys.argv[1]
|
||||||
|
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
|
||||||
|
|
||||||
|
# 将色彩空间转变为灰度图并展示
|
||||||
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
|
# 打印图片信息
|
||||||
|
# print_img_info(gray)
|
||||||
|
|
||||||
|
# 打印图片的局部
|
||||||
|
# print("打印图片局部")
|
||||||
|
# print(gray[100:105, 100:105])
|
||||||
|
|
||||||
|
# plt.imshow(gray)
|
||||||
|
# 需要添加colormap 颜色映射函数为gray
|
||||||
|
plt.imshow(gray, cmap="gray")
|
||||||
|
|
||||||
|
# 隐藏坐标系
|
||||||
|
plt.axis('off')
|
||||||
|
# 展示图片
|
||||||
|
|
||||||
|
# plt.show()
|
||||||
|
# 你也可以保存图片, 填入图片路径就好
|
||||||
|
new_name = '%s-gray.%s' % (img_path.split('.')[0], img_path.split('.')[-1])
|
||||||
|
plt.savefig(new_name)
|
||||||
|
print(new_name)
|
||||||
|
plt.close()
|
||||||
73
scripts/ins2mastodon.py
Normal file
73
scripts/ins2mastodon.py
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# scraper instagram posts
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dsite.settings")
|
||||||
|
sys.path.insert(0, '../')
|
||||||
|
sys.path.insert(0, './')
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
get_wsgi_application()
|
||||||
|
|
||||||
|
import pickle
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from mastodon import Mastodon
|
||||||
|
from instagram_private_api import Client
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from dsite import settings
|
||||||
|
|
||||||
|
mastodon_cli = Mastodon(access_token='Ug_bUMWCk3RLamOnqYIytmeB0nO6aNfjdmf06mAj2bE', api_base_url='https://nofan.xyz')
|
||||||
|
|
||||||
|
|
||||||
|
def send_image_to_mastodon(image_url, text):
|
||||||
|
resp = requests.get(image_url)
|
||||||
|
mime_type = 'image/jpeg'
|
||||||
|
url = urlparse(image_url)
|
||||||
|
ext = url.path.split('.')[-1]
|
||||||
|
if ext == 'gif':
|
||||||
|
mime_type = 'image/gif'
|
||||||
|
toot_resp = mastodon_cli.media_post(resp.content, mime_type)
|
||||||
|
if toot_resp.get('id'):
|
||||||
|
media_ids = [toot_resp['id']]
|
||||||
|
mastodon_cli.status_post(text, media_ids=media_ids)
|
||||||
|
|
||||||
|
|
||||||
|
# write binary file with api.settings
|
||||||
|
def writeSettings(user, pwd, settings_file):
|
||||||
|
api = Client(user, pwd)
|
||||||
|
with open(settings_file, "wb") as FileObj:
|
||||||
|
pickle.dump(api.settings, FileObj)
|
||||||
|
|
||||||
|
|
||||||
|
# read binary file to api.settings
|
||||||
|
def readSettings(settings_file):
|
||||||
|
cache = None
|
||||||
|
with open(settings_file, "rb") as FileObj:
|
||||||
|
cache = pickle.load(FileObj)
|
||||||
|
return cache
|
||||||
|
|
||||||
|
|
||||||
|
if not os.path.exists(settings.IG_PRIVATE_API_SETTINGS):
|
||||||
|
writeSettings(settings.IG_LOGIN_USERNAME, settings.IG_LOGIN_PASSWORD, settings_file)
|
||||||
|
|
||||||
|
cache_settings = readSettings(settings.IG_PRIVATE_API_SETTINGS)
|
||||||
|
api = Client(settings.IG_LOGIN_USERNAME, settings.IG_LOGIN_PASSWORD, settings=cache_settings)
|
||||||
|
|
||||||
|
results = api.self_feed()
|
||||||
|
for item in results['items']:
|
||||||
|
text = item['caption']['text']
|
||||||
|
image_url = item['image_versions2']['candidates'][0]['url']
|
||||||
|
try:
|
||||||
|
with open(settings.MASTODON_SYNCED_IMAGES_LOG, 'r') as f:
|
||||||
|
send_images = f.readlines()
|
||||||
|
send_images = [x.strip() for x in send_images]
|
||||||
|
if image_url not in send_images:
|
||||||
|
send_image_to_mastodon(image_url, text)
|
||||||
|
with open(settings.MASTODON_SYNCED_IMAGES_LOG, 'a') as f:
|
||||||
|
f.write(image_url + '\n')
|
||||||
|
except:
|
||||||
|
print('error')
|
||||||
42
scripts/ins_scraper.py
Executable file
42
scripts/ins_scraper.py
Executable file
@ -0,0 +1,42 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# scraper instagram posts
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dsite.settings")
|
||||||
|
sys.path.insert(0, '../')
|
||||||
|
sys.path.insert(0, './')
|
||||||
|
from django.core.wsgi import get_wsgi_application
|
||||||
|
|
||||||
|
get_wsgi_application()
|
||||||
|
|
||||||
|
import instagram_scraper
|
||||||
|
import time
|
||||||
|
from dsite import settings
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
args = {
|
||||||
|
'login_user': settings.IG_LOGIN_USERNAME,
|
||||||
|
'login_pass': settings.IG_LOGIN_PASSWORD,
|
||||||
|
'cookiejar': settings.IG_SCRAPER_COOKIE_FILE,
|
||||||
|
'latest_stamps': settings.IG_SCRAPER_LAST_STAMP_FILE,
|
||||||
|
'usernames': settings.IG_SCRAPER_USERNAMES,
|
||||||
|
'destination': settings.IG_SCRAPER_DESTINATION,
|
||||||
|
}
|
||||||
|
|
||||||
|
scraper = instagram_scraper.InstagramScraper(**args)
|
||||||
|
scraper.authenticate_with_login()
|
||||||
|
scraper.save_cookies()
|
||||||
|
while True:
|
||||||
|
time.sleep(10)
|
||||||
|
args_ = {
|
||||||
|
'cookiejar': settings.IG_SCRAPER_COOKIE_FILE,
|
||||||
|
'latest_stamps': settings.IG_SCRAPER_LAST_STAMP_FILE,
|
||||||
|
'usernames': settings.IG_SCRAPER_USERNAMES,
|
||||||
|
'destination': settings.IG_SCRAPER_DESTINATION,
|
||||||
|
'template': '{username}-{datetime}-{shortcode}',
|
||||||
|
}
|
||||||
|
scraper = instagram_scraper.InstagramScraper(**args_)
|
||||||
|
scraper.scrape()
|
||||||
Loading…
x
Reference in New Issue
Block a user