How to save image?

username: 1414b35e42c77e0a57dd
cooked: <pre><code class="lang-auto">for i in train_loader:
     images.shape = [64,3,28,28]
</code></pre>
<p>I can save images but 64 images are drawed.<br>
How can I save only one image from them?</p>

username: Tahir
cooked: <p>Are you trying to save an image from a tensor? if yes, then you need to specifiy the correct index.</p>
<p>e.g.</p>
<p>tensor1 has shape [64,3,28,28]</p>
<p>then to get one/first image out of it (the batch of 64), you need</p>
<p><code>image1_tensor= tensor1[0]</code></p>
<p>now you need to swap the axis to convert it into numpy</p>
<p><code>image1_numpy = image1_tensor.numpy()</code></p>
<p>Now if you save image1_numpy, you will get one image.</p>

username: 1414b35e42c77e0a57dd
cooked: <pre><code class="lang-auto">from torchvision.utils import save_image

images.shape #torch.Size([64,3,28,28])
img1 = images[0] #torch.Size([3,28,28]
# img1 = img1.numpy() # TypeError: tensor or list of tensors expected, got &lt;class 'numpy.ndarray'&gt;
save_image(img1, 'img1.png')
</code></pre>
<p><code>Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)</code></p>
<p>In this situation, What should I do?</p>

username: Tahir
cooked: <p>I tested, this works :</p>
<pre><code class="lang-auto">from torchvision.utils import save_image
import torch
import torchvision

rand_tensor= torch.rand(64, 3,28,28) 

img1 = rand_tensor[0]
# img1 = img1.numpy() # TypeError: tensor or list of tensors expected, got &lt;class 'numpy.ndarray'&gt;
save_image(img1, 'img1.png')
</code></pre>

username: LiUzHiAn
cooked: <p>In my case, I always visualize the images in 2 ways, there are really common I guess:</p>
<p>Suppose our torch tensor follows the <code>NCHW</code> format.</p>
<ol>
<li>save tensor into image directly<br>
In this situation, you can use</li>
</ol>
<pre><code class="lang-auto">save_image(make_grid(postprocess_image(batch_tensor), nrow=8),"XXX.jpg")` 
</code></pre>
<p>where the <code>postprocess_image()</code> executes some post processing operation, say, unnormalizing.</p>
<ol start="2">
<li>save tensor into summayWriter<br>
In this case, you can use:</li>
</ol>
<pre><code class="lang-auto">SummaryWriter.add_image("image", make_grid(postprocess_image(batch_tensor), nrow=8), step)
</code></pre>
<p>Yep! Note that <code>add_image()</code> assume the input tensor is a 3D tensor with CHW format, for other formations, check the docs out!</p>
<p>Hope this helps. <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:"></p>

username: nalin_thoummala
cooked: <p>hi, what if I would like to save image into paritcular folder?<br>
what should be added in the save_image function?</p>

username: somuSan
cooked: <pre><code class="lang-auto">save_image(img1, 'img1.png')
</code></pre>
<p>Here, you are saving the <code>img1</code> as <code>img1.png</code> in your current working directory, that argument is actually <code>the path where you want to save your file</code>, you can pass anything like <code>folder1/img1.png</code> in that function as an argument if your file structure looks like this, then the <code>img1.png</code> will get saved in the <code>folder1</code>.</p>
<pre><code class="lang-auto">current_working_directory
|__folder1
|__file.py
</code></pre>