Loop over numpy array to save them as tif files












0















I am trying to save a numpy array (train_images) with shape (625, 256, 256, 4) as tif images in a folder using a for loop. That is, 625 RGBN images of 256 x 256 pixels. Currently my code looks as follows:



path = str(os.getcwd) + "/data/train_images"

for i in train_images:
num = 0
i.save(num + '.tif')
num +=1


It is however not possible to save a numpy array as a tif file like this. In the end I would like to have 625 saved (RGBN) tif images in the folder named 0.tif, 1.tif etc.










share|improve this question

























  • train_images is not defined.

    – Rocky Li
    Nov 19 '18 at 19:52











  • I'm sorry, train_images is the numpy array of shape (625, 256, 256, 4).

    – Eeuwigestudent1
    Nov 19 '18 at 19:53






  • 1





    Maybe you want to look into the tifffile module pypi.org/project/tifffile because numpy.save does "Save an array to a binary file in NumPy .npy format". Keep in mind that a TIFF file contains a header.

    – Daniel F
    Nov 19 '18 at 20:07













  • What's the error you're getting?

    – TeeKea
    Nov 19 '18 at 20:09











  • @TeeKea "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

    – Eeuwigestudent1
    Nov 19 '18 at 20:12


















0















I am trying to save a numpy array (train_images) with shape (625, 256, 256, 4) as tif images in a folder using a for loop. That is, 625 RGBN images of 256 x 256 pixels. Currently my code looks as follows:



path = str(os.getcwd) + "/data/train_images"

for i in train_images:
num = 0
i.save(num + '.tif')
num +=1


It is however not possible to save a numpy array as a tif file like this. In the end I would like to have 625 saved (RGBN) tif images in the folder named 0.tif, 1.tif etc.










share|improve this question

























  • train_images is not defined.

    – Rocky Li
    Nov 19 '18 at 19:52











  • I'm sorry, train_images is the numpy array of shape (625, 256, 256, 4).

    – Eeuwigestudent1
    Nov 19 '18 at 19:53






  • 1





    Maybe you want to look into the tifffile module pypi.org/project/tifffile because numpy.save does "Save an array to a binary file in NumPy .npy format". Keep in mind that a TIFF file contains a header.

    – Daniel F
    Nov 19 '18 at 20:07













  • What's the error you're getting?

    – TeeKea
    Nov 19 '18 at 20:09











  • @TeeKea "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

    – Eeuwigestudent1
    Nov 19 '18 at 20:12
















0












0








0








I am trying to save a numpy array (train_images) with shape (625, 256, 256, 4) as tif images in a folder using a for loop. That is, 625 RGBN images of 256 x 256 pixels. Currently my code looks as follows:



path = str(os.getcwd) + "/data/train_images"

for i in train_images:
num = 0
i.save(num + '.tif')
num +=1


It is however not possible to save a numpy array as a tif file like this. In the end I would like to have 625 saved (RGBN) tif images in the folder named 0.tif, 1.tif etc.










share|improve this question
















I am trying to save a numpy array (train_images) with shape (625, 256, 256, 4) as tif images in a folder using a for loop. That is, 625 RGBN images of 256 x 256 pixels. Currently my code looks as follows:



path = str(os.getcwd) + "/data/train_images"

for i in train_images:
num = 0
i.save(num + '.tif')
num +=1


It is however not possible to save a numpy array as a tif file like this. In the end I would like to have 625 saved (RGBN) tif images in the folder named 0.tif, 1.tif etc.







python loops numpy tiff






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 19:56







Eeuwigestudent1

















asked Nov 19 '18 at 19:50









Eeuwigestudent1Eeuwigestudent1

417




417













  • train_images is not defined.

    – Rocky Li
    Nov 19 '18 at 19:52











  • I'm sorry, train_images is the numpy array of shape (625, 256, 256, 4).

    – Eeuwigestudent1
    Nov 19 '18 at 19:53






  • 1





    Maybe you want to look into the tifffile module pypi.org/project/tifffile because numpy.save does "Save an array to a binary file in NumPy .npy format". Keep in mind that a TIFF file contains a header.

    – Daniel F
    Nov 19 '18 at 20:07













  • What's the error you're getting?

    – TeeKea
    Nov 19 '18 at 20:09











  • @TeeKea "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

    – Eeuwigestudent1
    Nov 19 '18 at 20:12





















  • train_images is not defined.

    – Rocky Li
    Nov 19 '18 at 19:52











  • I'm sorry, train_images is the numpy array of shape (625, 256, 256, 4).

    – Eeuwigestudent1
    Nov 19 '18 at 19:53






  • 1





    Maybe you want to look into the tifffile module pypi.org/project/tifffile because numpy.save does "Save an array to a binary file in NumPy .npy format". Keep in mind that a TIFF file contains a header.

    – Daniel F
    Nov 19 '18 at 20:07













  • What's the error you're getting?

    – TeeKea
    Nov 19 '18 at 20:09











  • @TeeKea "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

    – Eeuwigestudent1
    Nov 19 '18 at 20:12



















train_images is not defined.

– Rocky Li
Nov 19 '18 at 19:52





train_images is not defined.

– Rocky Li
Nov 19 '18 at 19:52













I'm sorry, train_images is the numpy array of shape (625, 256, 256, 4).

– Eeuwigestudent1
Nov 19 '18 at 19:53





I'm sorry, train_images is the numpy array of shape (625, 256, 256, 4).

– Eeuwigestudent1
Nov 19 '18 at 19:53




1




1





Maybe you want to look into the tifffile module pypi.org/project/tifffile because numpy.save does "Save an array to a binary file in NumPy .npy format". Keep in mind that a TIFF file contains a header.

– Daniel F
Nov 19 '18 at 20:07







Maybe you want to look into the tifffile module pypi.org/project/tifffile because numpy.save does "Save an array to a binary file in NumPy .npy format". Keep in mind that a TIFF file contains a header.

– Daniel F
Nov 19 '18 at 20:07















What's the error you're getting?

– TeeKea
Nov 19 '18 at 20:09





What's the error you're getting?

– TeeKea
Nov 19 '18 at 20:09













@TeeKea "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

– Eeuwigestudent1
Nov 19 '18 at 20:12







@TeeKea "AttributeError: 'numpy.ndarray' object has no attribute 'save'"

– Eeuwigestudent1
Nov 19 '18 at 20:12














1 Answer
1






active

oldest

votes


















1














Try imsave in scipy.misc, as follows:



path = str(os.getcwd) + "/data/train_images/"

num = 0
for img in train_images:
import scipy.misc
scipy.misc.imsave(path + str(num) + '.tif', img)
num +=1


To read an image from a file, you would need to use the following:



import imageio
im = imageio.imread('0.tif')


Hope it helps.






share|improve this answer


























  • Yes it worked!!!! Thanks a lot!

    – Eeuwigestudent1
    Nov 19 '18 at 20:24











  • Glad to hear that. Please Tick mark the answer as Accepted.

    – TeeKea
    Nov 19 '18 at 20:36













  • Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

    – Eeuwigestudent1
    Nov 19 '18 at 20:41











  • No worries. Good luck.

    – TeeKea
    Nov 19 '18 at 20:41











  • There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

    – Eeuwigestudent1
    Nov 19 '18 at 21:14











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381680%2floop-over-numpy-array-to-save-them-as-tif-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try imsave in scipy.misc, as follows:



path = str(os.getcwd) + "/data/train_images/"

num = 0
for img in train_images:
import scipy.misc
scipy.misc.imsave(path + str(num) + '.tif', img)
num +=1


To read an image from a file, you would need to use the following:



import imageio
im = imageio.imread('0.tif')


Hope it helps.






share|improve this answer


























  • Yes it worked!!!! Thanks a lot!

    – Eeuwigestudent1
    Nov 19 '18 at 20:24











  • Glad to hear that. Please Tick mark the answer as Accepted.

    – TeeKea
    Nov 19 '18 at 20:36













  • Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

    – Eeuwigestudent1
    Nov 19 '18 at 20:41











  • No worries. Good luck.

    – TeeKea
    Nov 19 '18 at 20:41











  • There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

    – Eeuwigestudent1
    Nov 19 '18 at 21:14
















1














Try imsave in scipy.misc, as follows:



path = str(os.getcwd) + "/data/train_images/"

num = 0
for img in train_images:
import scipy.misc
scipy.misc.imsave(path + str(num) + '.tif', img)
num +=1


To read an image from a file, you would need to use the following:



import imageio
im = imageio.imread('0.tif')


Hope it helps.






share|improve this answer


























  • Yes it worked!!!! Thanks a lot!

    – Eeuwigestudent1
    Nov 19 '18 at 20:24











  • Glad to hear that. Please Tick mark the answer as Accepted.

    – TeeKea
    Nov 19 '18 at 20:36













  • Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

    – Eeuwigestudent1
    Nov 19 '18 at 20:41











  • No worries. Good luck.

    – TeeKea
    Nov 19 '18 at 20:41











  • There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

    – Eeuwigestudent1
    Nov 19 '18 at 21:14














1












1








1







Try imsave in scipy.misc, as follows:



path = str(os.getcwd) + "/data/train_images/"

num = 0
for img in train_images:
import scipy.misc
scipy.misc.imsave(path + str(num) + '.tif', img)
num +=1


To read an image from a file, you would need to use the following:



import imageio
im = imageio.imread('0.tif')


Hope it helps.






share|improve this answer















Try imsave in scipy.misc, as follows:



path = str(os.getcwd) + "/data/train_images/"

num = 0
for img in train_images:
import scipy.misc
scipy.misc.imsave(path + str(num) + '.tif', img)
num +=1


To read an image from a file, you would need to use the following:



import imageio
im = imageio.imread('0.tif')


Hope it helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 19 '18 at 23:17

























answered Nov 19 '18 at 20:14









TeeKeaTeeKea

3,20841730




3,20841730













  • Yes it worked!!!! Thanks a lot!

    – Eeuwigestudent1
    Nov 19 '18 at 20:24











  • Glad to hear that. Please Tick mark the answer as Accepted.

    – TeeKea
    Nov 19 '18 at 20:36













  • Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

    – Eeuwigestudent1
    Nov 19 '18 at 20:41











  • No worries. Good luck.

    – TeeKea
    Nov 19 '18 at 20:41











  • There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

    – Eeuwigestudent1
    Nov 19 '18 at 21:14



















  • Yes it worked!!!! Thanks a lot!

    – Eeuwigestudent1
    Nov 19 '18 at 20:24











  • Glad to hear that. Please Tick mark the answer as Accepted.

    – TeeKea
    Nov 19 '18 at 20:36













  • Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

    – Eeuwigestudent1
    Nov 19 '18 at 20:41











  • No worries. Good luck.

    – TeeKea
    Nov 19 '18 at 20:41











  • There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

    – Eeuwigestudent1
    Nov 19 '18 at 21:14

















Yes it worked!!!! Thanks a lot!

– Eeuwigestudent1
Nov 19 '18 at 20:24





Yes it worked!!!! Thanks a lot!

– Eeuwigestudent1
Nov 19 '18 at 20:24













Glad to hear that. Please Tick mark the answer as Accepted.

– TeeKea
Nov 19 '18 at 20:36







Glad to hear that. Please Tick mark the answer as Accepted.

– TeeKea
Nov 19 '18 at 20:36















Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

– Eeuwigestudent1
Nov 19 '18 at 20:41





Just did, tried to upvote you but don't have enough reputation (yet). Thanks again.

– Eeuwigestudent1
Nov 19 '18 at 20:41













No worries. Good luck.

– TeeKea
Nov 19 '18 at 20:41





No worries. Good luck.

– TeeKea
Nov 19 '18 at 20:41













There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

– Eeuwigestudent1
Nov 19 '18 at 21:14





There is one problem though, if I read the image in again the shape has changed. When we loop over the train images then "train_image" has shape (625,256,256,4), and when i read the saved images they have shape (25, 256, 256, 4). Where does this first 25 come from?

– Eeuwigestudent1
Nov 19 '18 at 21:14


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381680%2floop-over-numpy-array-to-save-them-as-tif-files%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?