How does one decylinderize an image?

I need to decylinderize an image. How is this accomplished? (Gimp, ImageMagick... doesn't matter.)

• Do you require that the resulting "to this" image be accurate to the original cylinder? Sep 2, 2022 at 21:10
• To be perfectly honest with you, if this is the image you are actually working with, it would be simpler to just redraw it. I wouldn't use GIMP though. Just redraw in vectors, Inkscape would be better. Stretching and resampling a raster image isn't going to look good. Sep 3, 2022 at 21:00
• You can probably project from view on a cylinder in some 3D application and export the resulting UV texture... maybe better results than on image editors Sep 5, 2022 at 9:51

The question is not at all clear. Actually we do not know what is that cylinderization you want to remove. I guess you have a photo of a cylindrical surface which contains another image. Let's call that other image "Label". Now you want to straighten the photo so that Label could be seen as it was before it was wrapped around the cylinder.

The name "Label" is taken from the possible case where one has a photo of a bottle and he wants to straighten the label glued on the bottle.

The next example has some text placed on a cylinder. It's only a half-cylinder, because only one half can be seen at a time:

There's also the original planar text shown as a reference. I guess you have a straight on the face photo which is shot from far enough so that there's no perspective. A close shot with perspective cannot be handled with the elementary math I know.

The no-perspective straight on the face photo of my label is this:

We can analyze the imaging with the next illustration:

The blue half-circle is the label. The distortion happens only in X-direction. Y stays as is, so one dimensional math is enough.

Parallel vertical rays project the label on the red line (remember, no perspective, the projection is parallel). The photo is on the red line between 0 and W, where W is the number of the rightmost pixel of the photo.

The point on the label at horizontal original label coordinate S from the left edge is on the photo at coordinate X.

The blue half-circle is longer. The width of the straightened label is about 157% of the width of the photo. That's the width of the photo multiplied by Pi/2.

The photo may be for ex. 2000 px wide i.e. number X can vary from 0....1999. At the same time coordinate S varies from 0 to 3140 or 3141 depending on the direction of the rounding.

You may create for the label a blank image which is for ex. 3142 pixels wide and colorize each pixel with a color taken from the photo. To do it I have written formula

X = (W/2)(1-cos(2S/W)). It says from where in the photo one should take the RGB values for pixels which have horizontal coordinate = S. Y stays intact. In case the calculated X is non-integer, one must either take from the nearest pixel or interpolate.

The formula is exact in pure math, but there's a practical limitation which radically reduce the usefulness of it:

• The blue circle is nearly vertical close the edges of the label. One pixel in the photo covers a large area in the label near the edges. In our numeric example the leftmost pixel will be stretched to 32 pixels wide in the label image. The edges of the label become heavily pixelated. Using interpolation instead of "take the nearest" doesn't fix it, it only makes the label edges fuzzy.

My formula is not bad, the photo of the label simply has a reduced amount of information of the actual label and the information is especially sparse near the edges.

I made some tests in Krita, actually in its free add-on named G'MIC filter collection. There deformation filter "Cartesian Transform" allows one to write the shown equation (use X instead of S and write Y for Y). The result with nearest neighbour resampling:

It's far from perfect. About 10% at the edges is badly pixelated. In the middle the label looks OK. Using linear interpolation instead of the nearest neighbour for non-integer pixels doesn't make it better, the pixelation at the edges only changes to fuzziness.

The left and right edges look clearly different. I have not still found if my "photo" is made inaccurately or is it caused by too inaccurate math in the filter.

You wrote you could use ImageMagick. That program obviously is perfect for the job, but it needs an user who can think like a computer programmer. You may be one, but I must skip it.

GIMP has distortion filter Spherize which can be used in horizontal mode and with negative strength. I guess (cannot prove) it makes the same as my formula. The result is harmfully squeezed to the same width as the photo, but you can compensate it easily by scaling the width to 157% before applying the spherize. Here's a screenshot from GIMP. The photo width was not scaled to 157%, so the result is squeezed:

A suggestion: If it happens that you really are straightening a real label on a real bottle and it's not possible to do it mechanically you can take more photos from different directions. Then the label edges can also be in their turns in the middle and you can split & paste together a perfect label.

Image stitching programs (Photoshop has it, too) can make the straightening and combining automatically. Only have generous overlaps and crop off the low information edge areas.