[英]How do I keep the file name of an image when resizing it and saving it to another folder?
I'm in an introductory neural networking class so mind my ignorance.我在介绍神经网络 class 所以请注意我的无知。 Also my first SO post.
也是我的第一个 SO 帖子。
I'm trying to resize some very highly resolved images within a dataset into 80x80p grayscale images in a new dataset.我正在尝试将数据集中一些非常高分辨率的图像调整为新数据集中的 80x80p 灰度图像。 However, when I do this, I'd like to keep the filenames of each new image the same as the original image.
但是,当我这样做时,我想保持每个新图像的文件名与原始图像相同。 The only way I know how to resave images into a new file is through a str(count) which isn't what I want.
我知道如何将图像重新保存到新文件中的唯一方法是通过 str(count) ,这不是我想要的。 The filenames are important in creating a.csv file for my dataset later.
文件名对于稍后为我的数据集创建 .csv 文件很重要。
The only SO post I can find that is related is this:我能找到的唯一相关的SO帖子是:
Use original file name to save image 使用原始文件名保存图像
But the code suggested there didn't work - wasn't sure if I was going about it the wrong way.但是建议的代码不起作用-不确定我是否以错误的方式进行操作。
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
img_resized.save(path2 + str(count) + '.png')
count +=1
pass
pass
Reuse the original filename that you get from the for loop ie file
and, split it into filename and extension using os.path.splitext()
like below:重用从 for 循环中获得的原始文件名,即
file
,并使用os.path.splitext()
将其拆分为文件名和扩展名,如下所示:
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
# splitting the original filename to remove extension
img_filename = os.path.splitext(file)[0]
img_resized.save(path2 + img_filename + '.png')
count +=1
pass
Another option, we can use python str
's built-in split
method to split the original filename by .
另一种选择,我们可以使用 python
str
的内置split
方法将原始文件名拆分为.
and discard the extension.并丢弃扩展名。
import os
from PIL import Image
import imghdr
count=0
path1 = "/Users/..."
path2 = "/Users/..."
listing = os.listdir(path1)
for file in listing:
type = imghdr.what((path1 + file))
if type == "jpeg":
img = Image.open("/Users/..." +file).convert('LA')
img_resized = img.resize((80,80))
# splitting the original filename to remove extension
img_filename = file.split(".")[0]
img_resized.save(path2 + img_filename + '.png')
count +=1
pass
So, if an image has a name such as some_image.jpeg
then, the img_filename
will have a value some_image
as we splitted by.因此,如果图像具有诸如
some_image.jpeg
之类的名称,那么img_filename
将具有我们分割的值some_image
。 and discarded .jpeg
part of it.并丢弃了
.jpeg
的一部分。
NOTE: This option assumes the original_filename will not contain any .
注意:此选项假定 original_filename 不包含任何
.
other than the extension.除了扩展名。
I assume that image name is on path1.我假设图像名称在 path1 上。 If so you can grap image name from there in this way:
如果是这样,您可以通过以下方式从那里获取图像名称:
x=path1.rsplit('/',1)[1]
We are splitting path1 on last slash and taking image name string via indexing.我们在最后一个斜杠上拆分 path1 并通过索引获取图像名称字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.