-1

I have a file with a lot of .tif images.

Part 1. Preview of TIFF images

When I try to opreview them by clicking on them in the jupyter folder (which look like this one the Jupyter folder), I get the following message :

Error ! D:...\image.tif is not UTF-8 encoded

On the opposite, if I click on a png in the Jupyter folder, Jupyter does display an image.

How could I fix my images, knowing that I have more than 1000 of them in my folder ?

Nonetheless, if I write :

sph = cv2.imread('A1.tif',-1)
plt.imshow(sph)
plt.show()

I do get the image : image of 'A1.tif'.

Now I also checked :

import chardet
chardet.detect('A1.det')
--> {'confidence': 1.0, 'encoding': 'ascii', 'language': ''}  # result

So apparently I it is encoded in ascii. Is it the same as utf-8 or should I convert them ?

Edit : Answer : In one of the comments, @FabienP answers that "According the official documentation, Jupyter lab does not support TIFF format for image preview (as of now)", which answers this question.

Part 2 : writing a video out of TIFF images

I have another question and I don't know if both questions are connected.

I want to make a video out of them.

import cv2
import os
image_folder = 'A549_A1'
video_name = 'video.avi'

images = [img for img in os.listdir(image_folder) if img.endswith(".tif")]
frame = cv2.imread(os.path.join(image_folder, images[0]))
height, width, layers = frame.shape

video = cv2.VideoWriter(video_name, 0, 1, (width,height))

for image in images:
     video.write(cv2.imread(os.path.join(image_folder, image)))

cv2.destroyAllWindows()
video.release()

But instead of getting the expected video, I get a strange one with many images at one step : caption of the video. You can compare it to the image above to check that it's not normal.

How can I fix that ?

JosephAc
  • 5
  • 2
  • This is *very* confused. UTF-8 is a text encoding; it makes absolutely no sense to require it for data which isn't text at all. – tripleee Feb 26 '19 at 16:27
  • 1
    If you show your code, as required by the **Minimal, Complete, Verifiable Example**, you will be more likely to get an answer https://stackoverflow.com/help/mcve – Mark Setchell Feb 26 '19 at 17:04
  • @tripleee Do you mean my question is confused or the error message is confused ? – JosephAc Feb 26 '19 at 21:15
  • Your images are fine - I presume you can see them in the normal Windows File Explorer (*"My Computer"* thing)? They do not need to be UTF-8. Nor ASCII. Nor decoded. Nor encoded. Maybe take a step back and say what you are trying to achieve here... – Mark Setchell Feb 26 '19 at 21:29
  • 1
    According the official documentation, Jupyter lab [does not support TIFF](https://jupyterlab.readthedocs.io/en/stable/user/file_formats.html#images) format for image preview (as of now). – FabienP Feb 26 '19 at 21:43
  • 1
    If, as suggested, TIFFs are not supported, you could use **ImageMagick** to make PNG versions of all your TIFF files using this command in **Command Prompt** `magick mogrify -format PNG *.tif` Though, if you actually want to read them with OpenCV, this is not necessary because it understands TIFFs anyway. – Mark Setchell Feb 26 '19 at 21:55
  • @MarkSetchell Thx for your answers. What you're saying regarding cv2 is half right, since there is an issue with cv2.VideoWriter . Besides that, is there a possibility to write a command that would convert all those TIFF files to PNG ? Because that it's quite long to convert all of them one by one. – JosephAc Feb 26 '19 at 22:01
  • Personally, I would be looking at using `ffmpeg`... https://stackoverflow.com/a/24966617/2836621 – Mark Setchell Feb 26 '19 at 22:13
  • The **ImageMagick** command I suggested will make PNG versions of all your TIFFs with the one, single command. – Mark Setchell Feb 26 '19 at 22:14
  • @MarkSetchell Great I will have a look at ffmpeg. Regarding ImageMagick, where do I enter the path in the command ? – JosephAc Feb 26 '19 at 22:24
  • The command assumes you have used `CD` to change directory to the one containing your images. – Mark Setchell Feb 26 '19 at 22:31
  • The CV2 documentation indicates that the `cv2.VideoWriter` will run `ffmpeg` (or Linux and possibly Windows) behind the scenes anyway. – tripleee Feb 27 '19 at 05:38

1 Answers1

1

Converting the bytes in an image from ASCII to UTF-8 makes only slightly more sense than converting them from Fahrenheit to Celsius, or transposing them to B♭ major. If you can find a way to do it technically, all it will do is wreck the image. Indeed, this is completely a red herring, and has absolutely nothing to do with your video conversion problem.

Text encodings like ASCII and UTF-8 describe how characters map between code points or glyphs and computer representations. There is no text in an image file; it is just a bunch of pixels. Maybe see also the seminal 2003 blog post The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Moreover, because UTF-8 is ASCII compatible, every ASCII file is already trivially a UTF-8 file. There is no transformation you can apply to make it "more UTF-8".

Binary formats, on the other hand, typically have an internal structure which is quite different. For just an image, a trivial format might simply encode each black pixel as a 1 bit and each white pixel as a 0 bit. (In fact, the very first version of TIFF did exactly this, with a few additional frills.) You can add a constant to each byte, for example, but this will simply transform it into a jumble which no longer contains a valid picture. Examine what happens if you add one to a number like 63 which has a lot of 1 bits in the lower half in its binary representation:

 63    0011 1111      ..XX XXXX  <- sequence of black pixels
+ 1  + 0000 0001      .... ...X
---- -----------     ----------
 64    0100 0000      .X.. ....  <- one black pixel, lots of white

Modern binary formats are quite a bit more complex, and often contain header sequences which indicate how many bytes of data follow or where to look for a particular feature to populate a data structure in memory. Replacing these values with other values will almost certainly create a stream which is simply corrupted unless you know exactly what you are doing.


Comparing against https://stackoverflow.com/a/34555939/874188 and googling for a bit suggests that passing 0 as the fourcc parameter might be the source of your problems.

tripleee
  • 139,311
  • 24
  • 207
  • 268
  • https://stackoverflow.com/a/31401095/874188 suggests `cv2.VideoWriter_fourcc(list("MJPG"))`, though another answer on the same question alleges that it used to work poorly at one point, and has many other options you could try. – tripleee Feb 27 '19 at 14:45