1

I want to convert a jpg image to tiff.

I tried to do it with opencv with python.

import cv2
image = cv2.imread("image.jpg")

retval, buf = cv2.imencode(".tiff", image)
cv2.imwrite("out.tiff")

And I have this:
Process finished with exit code 136 (interrupted by signal 8: SIGFPE) I referred this link

But I couldn't make it work. Any help would be appreciated.

Community
  • 1
  • 1
Elif
  • 21
  • 1
  • 3
  • 1
    `imencode` will give you the bytes of an image encoded as tiff (usually you write this directly to a file or maybe via network to another app), and `imwrite` will encode and write it to disk, in your code you are missing the image parameter in this function. – api55 Aug 29 '19 at 09:56

1 Answers1

2

For me this version worked without errors:

import cv2
image = cv2.imread("image.jpg")

cv2.imwrite("out.tiff", image)

Why do you need the imencode? Using that gives the same resulting file for me, just creates a temporary memory buffered, already TIFF-compressed version of the image:

retval, buf = cv2.imencode(".tiff", image)
with open('out2.tiff', 'wb') as fout:
    fout.write(buf.tostring())
Andris
  • 921
  • 7
  • 14
  • This creates a tiff image but when I try to use this code `naip_ndvi = (naip_data[3] - naip_data[0]) / (naip_data[3] + naip_data[0])` it won't work. – Elif Aug 29 '19 at 12:05
  • I assume you're doing something similar to [this](https://www.earthdatascience.org/courses/earth-analytics-python/multispectral-remote-sensing-in-python/vegetation-indices-NDVI-in-python/). When you try to do image manipulations e.g. subtract one from the other you don't perform that on the compressed image (imencode result), instead you need to perform it on the original `image` which contains all the pixels nicely in an array. – Andris Aug 29 '19 at 15:55
  • Also please note that for the subtract to work properly check whether OpenCV's `cv2.subtract(img1, img2)` or the simple numpy `img1 - img2` is better for you as they give different results (`cv2.add` also exist). The pixel data is 0..255 and thus a single-pixel color component 0 in one image minus 50 in the other is 206 in numpy (adds 256 to -50 due to uint8) and `cv2.subtract` will result in 0 (the 'blackness' saturates for the given pixel). You may see the different results in [this question](https://stackoverflow.com/q/45817037/501814). – Andris Aug 29 '19 at 16:23