[心得] 用Cloudflare Worker AI 隨機畫桌布
一時興起,搞了一個多禮拜的實驗
事情是這樣,最近發現三星手機有AI繪製桌布和AI天氣效果的功能,但實際用了之後還是覺得這跟我想得有點差,前者可以根據指定主題AI繪圖,後者可以在已有照片上加上AI天氣效果
但我要的是可以根據現在天氣隨機繪製一張,而且會定時更新
研究了一下發現利用API繪圖應該是可行的辦法,但大部分API都要錢,如果一小時畫一張一天大概也要幾十元台幣,不是很貴但也不太值得
後來就注意到cloudflar有每天免費額度,對於我這種需求正好,但實際執行還是費了不少勁,主要問題不是隨機畫圖,而是如何讓手機定時執行這個程序,這片就是稍微分享一下我用的醜陋辦法
我會分享範例文件,歡迎各位指教
https://drive.google.com/drive/folders/1CW5EzsOymc8rPa-wCrtKuGS29z9HPmV1
需要申請:
Cloudflare 帳號
openweathermap 帳號
A.cloudflare 帳號
這目標是拿到帳號ID和AI token
帳號ID是my dashbord的網址中
https://dash.cloudflare.com/ 帳號ID /home/domains
token大概用下面方式取得
https://i.meee.com.tw/82Z4f5W.jpg





2.openweathermap
目標是拿到API
大概像這樣
https://i.meee.com.tw/BHghsP5.jpg

B.關鍵字繪圖
這裡準備了一個python腳本,其中關鍵字產生長這樣
https://i.meee.com.tw/SUBkw28.png

原理是先利用現代位置傳給API,API回傳現在天氣,日出日落時間
然後根據不同天氣,從一大串關鍵字中選幾個,加上天氣描述和日夜描述,當作繪圖的基底
當然有這些關鍵字就能畫圖,但我決定複雜點,將剛才的關鍵字扔進LLM模型,請AI模型幫我產生更清楚的描述
https://i.meee.com.tw/ZRGU8s4.png

產生關鍵字的promt可以自行修改,但我建議留著promt only等補述,否則LLM會產生諸如"好美妙的組合..."等等的廢話
擴展的關鍵字可以傳入繪圖AI,也可以加入negative promt。
其中最重要的一步是決定影像大小
這裡有所限制,影像的寬高需要是256的倍數,最大是2048,建議從中選一個最接近螢幕長寬比的組合
https://i.meee.com.tw/fveTaFV.png

然後回傳的影像經由PIL套件存檔
C.設定termux
temux是個終端機,我屆此在手機上執行python
在此須安裝
Termux
Termux-API
不建議安裝play版,我是在FDROID上裝,或是在git上裝,但兩者來源需相同
安裝好兩者之後要安裝套件,在termux鍵入
pkg update && upgrade
pkg install termux-api
pkg install jq
pkg install python
pkg install libjpeg-turbo
pkg install libpng
pip install pillow
pip install requests
termux-storage-setup 然後給予termux存取檔案權限
ternux-location 這時候會跳出給termux-api 位置權限,記得要選"永遠允許"而非"應用程式使用時",否則背景會抓不到位置
再試一次
termux-location -r last 這時應該要跳出目前位置
安裝好後我們把範例檔中的 api.json AI_wallpaper.py AI_wallpaper.sh放入Pictures底下的AI_wallpaper資料夾
api.json裡要填入剛才申請到的token和API
然後termux鍵入
mkdir ~/AI_wallpaper/
cp /storage/emulated/0/Pictures/AI_wallpaper/AI_wallpaper.sh ~/AI-wallpaper/
chmod +x ~/AI-wallpaper/AI_wallpaper.sh
上面三步是創建資料夾,把AI_wallpaper.sh搬過去,給予執行權限
現在可以執行看看 ~/AI-wallpaper/AI_wallpaper.sh
應該可以順利看到產生一張圖和一個log在 Picture/AI_wallpaper資料夾並被設定成桌面
如果可以執行,那鍵入
termux-job-scheduler \
--script /data/data/com.termux/files/home/AI_wallpaper/AI_wallpaper.sh \
--period-ms 3600000 \
--persisted true \
--battery-not-low true
這樣就可以設定定期一小時執行
D.摺疊機
個人是用折疊機,但上述程序有點麻煩,因為系統只讓我設定現在這個螢幕的桌布,不能設定沒開的另一個螢幕桌布
這裡利用
termux-sensor -s "hinge_angle Wakeup" -n 1
找到一個叫做"hinge_angle Wakeup"感測器,這個感測器折起來時輸出0,反之為180,我把這個參數輸入python產生不同比例的圖片並設為桌布
我把範例檔附上,但我不敢保這這個感測器的名稱在其他摺疊機能不能用
E.結果
keywords: railway, lake, mountain, ocean, Dusk, clear sky
Prompt: A railway track winds its way along the shores of a serene lake, surrounded
by towering mountains that reflect perfectly in the calm waters at dusk. In
the distance, the ocean stretches out to the horizon, where the sky is
painted with hues of pink and orange, gradating into a brilliant blue. The
atmosphere is tranquil, with a few wispy clouds scattered across the clear
sky, adding a touch of softness to the scene.
https://i.meee.com.tw/XgIsnFA.jpg


keywords: city, island, coastline, outdoor, Dusk, clear sky
Prompt: A serene coastal cityscape at dusk, with the sun dipping into the horizon,
casting a warm orange glow across the tranquil waters of the ocean. The sky
above is a brilliant blue, with a few wispy clouds scattered across the
canvas, their silhouettes outlined against the vibrant hues of the setting
sun. The city's skyline is a blend of modern and vintage architecture, with
sleek skyscrapers and charming beachside bungalows, all bathed in the soft
golden light of dusk. The coastline is dotted with elegant lamp posts, their
soft glow illuminating the winding promenade that stretches along the shore.
In the distance, the outline of a small island can be seen, its rugged
silhouette a striking contrast to the calm waters. The atmosphere is
peaceful, with the sound of gentle waves and the distant hum of city life
providing a soothing background melody.
https://i.meee.com.tw/xlzeuD5.jpg


keyword: coastline, island, city, ocean, Night, clear sky
Prompt: A serene coastal city on a tropical island at night, with a crystal-clear sky
displaying a million twinkling stars. The city's skyline is dominated by a
majestic lighthouse standing tall on a rocky outcropping, its beam cutting
through the darkness. In the distance, the ocean waves gently lap against the
shore, creating a soothing melody. The city's buildings are a mix of modern
and colonial architecture, with brightly lit windows and streets bustling
with nocturnal activity. A few sailboats are moored in the harbor, their
sails billowing in the gentle breeze. The air is filled with the sweet scent
of blooming flowers and the distant tang of saltwater, as the city's
residents go about their evening routines under the celestial showcase above.
https://i.meee.com.tw/3ws362Q.jpg


橫屏狀況
keyword: outdoor, ocean, railway, traffic, Night, clear sky
Prompt: A serene night scene of a coastal railway track overlooking the vast ocean,
with no traffic in sight. The clear sky above is filled with a million
twinkling stars, and the only sound is the distant hum of a passenger train
making its way along the tracks, its lights casting a warm glow on the dark
sand below.
https://i.meee.com.tw/fmWbnzU.jpg


心得和建議:
1. 整個python程式因為有AI幫忙,算是單純很多,但就是搞termux和android權限等等搞了好久,之前是過termux+macrodroid,但就是搞不定,最後還是會到termux+termux-api
2. Stable deffusion模型在細節上差強人意,如果要更好的細節,cloudflare有提供lucid phonix等,但一張大概就會耗掉一天免費額度,雖說如此,我有次真的用超過很多,但有就是一個提醒,沒有禁止我繼續用,隔天額度又恢復
3. Stable deffusion有時也會產生很多不合理的東西,樹讓的人,水裡的車,斷掉路和鐵軌等,而且圖越大越容易出現不合理的元素重複,lucid會好很多
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.228.93.245 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/MobileComm/M.1770918886.A.318.html
推
02/13 02:22,
6小時前
, 1F
02/13 02:22, 1F
推
02/13 04:04,
4小時前
, 2F
02/13 04:04, 2F
MobileComm 近期熱門文章
PTT數位生活區 即時熱門文章
11
12