[英]How to move images to a different directory using Linux Ubuntu terminal
我現在正在嘗試構建一個腳本,該腳本可以打開、旋轉、調整大小並保存圖像目錄中包含的多個圖像(運行pwd
命令會給出消息/home/student-01-052f372bc989/images
)。 圖像目錄中包含的圖像為 TIFF 格式,分辨率為 192x192 像素,逆時針旋轉 90°。 腳本必須將這些圖像轉換為以下格式:
並將修改后的圖像保存在 /opt/icons 目錄中
這是我目前擁有的代碼:
import os
from PIL import Image
Image_dir = '/home/student-01-052f372bc989/images'
imagedir = os.chdir(Image_dir)
new_dir = '/opt/icons'
for pic in os.listdir(os.getcwd()):
if pic.endswith(".tiff"):
img = Image.open(pic)
new_img = img.resize((128,128)).rotate(270)
newName = pic.replace(".tiff", ".jpeg")
newdir = os.chdir(new_dir)
new_img.save(newName, "JPEG")
imagedir = os.chdir(Image_dir)
代碼運行時沒有問題,但是當我運行ls /opt/icons
命令檢查修改后的圖像是否復制到目錄時,圖像還沒有。
該腳本當前位於/home/student-01-052f372bc989/images
目錄中。
有人可以告訴我我做錯了什么嗎?
所以......經過一些挖掘,我確實設法找到了一種更簡單的方法來編寫腳本
代碼如下:
import os
from PIL import Image
old_path = os.path.expanduser('~') + '/images/'
new_path = '/opt/icons/'
for image in os.listdir(old_path):
if '.' not in image[0]:
img = Image.open(old_path + image)
img.rotate(-90).resize((128, 128)).convert("RGB").save(new_path + image.split('.')[0], 'jpeg')
img.close()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.