简体   繁体   English

如何使用 Linux Ubuntu 终端将图像移动到不同的目录

[英]How to move images to a different directory using Linux Ubuntu terminal

I am now trying to build a script that opens, rotates, resizes and saves several images contained in the images directory (running the pwd command gives the message /home/student-01-052f372bc989/images ).我现在正在尝试构建一个脚本,该脚本可以打开、旋转、调整大小并保存图像目录中包含的多个图像(运行pwd命令会给出消息/home/student-01-052f372bc989/images )。 The images contained in the images directory are of the format TIFF, have a resolution of 192x192 pixel and are rotated 90° anti-clockwise.图像目录中包含的图像为 TIFF 格式,分辨率为 192x192 像素,逆时针旋转 90°。 The script must turn these images in the following formats:脚本必须将这些图像转换为以下格式:

  1. .jpeg format .jpeg 格式
  2. Image resolution 128x128 pixels图像分辨率 128x128 像素
  3. Should be straight应该是直的

and save the modified images in the /opt/icons directory并将修改后的图像保存在 /opt/icons 目录中

Here is the code I currently have:这是我目前拥有的代码:

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)

The code has no issue when I run it, but when I run the ls /opt/icons command to check if the modified images were copied to the directory, the images are not yet there.代码运行时没有问题,但是当我运行ls /opt/icons命令检查修改后的图像是否复制到目录时,图像还没有。

The script is currently located in the /home/student-01-052f372bc989/images directory.该脚本当前位于/home/student-01-052f372bc989/images目录中。

Could someone please tell me what I did wrong?有人可以告诉我我做错了什么吗?

So... with a bit of digging, i did manage to find an easier way to do the script所以......经过一些挖掘,我确实设法找到了一种更简单的方法来编写脚本

the code is as follows:代码如下:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM