Rotate image causes black bars and corruption

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Rotate image causes black bars and corruption

Post by caliguian »

I just moved from/to the following:

ImageMagick-nox11: 6.9.2.3,1 -> 6.9.4.3,1
pecl-imagick: 3.1.2_2 -> 3.4.1

Full version info:
Version: ImageMagick 6.9.4-3 Q16 amd64 2016-05-24 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib fontconfig fpx jbig jng jp2 jpeg lcms ltdl png xml zlib


Using PHP, in the past I have always been able to use

Code: Select all

convert -auto-orient 'file' 'newfile'
to automatically rotate my images without any trouble. After this update the images still automatically rotate, but they also end up with giant black bars on them. I thought, alright maybe there is a problem with the convert -auto-orient functionality, so I switched to the Imagick method instead, and tried using the following:

Code: Select all

$imagick = new Imagick();
//Read in the image
$imagick->readImage($fileLocation);
//Rotate the Image according to its EXIF info
$imagick = AutoRotate($imagick);
//Save the image
$imagick->writeImage();
$imagick->clear();

function AutoRotate(Imagick $image)
{
	switch ($image->getImageOrientation()) {
	case Imagick::ORIENTATION_TOPLEFT:
		break;
	case Imagick::ORIENTATION_TOPRIGHT:
		$image->flopImage();
		break;
	case Imagick::ORIENTATION_BOTTOMRIGHT:
		$image->rotateImage("#000", 180);
		break;
	case Imagick::ORIENTATION_BOTTOMLEFT:
		$image->flopImage();
		$image->rotateImage("#000", 180);
		break;
	case Imagick::ORIENTATION_LEFTTOP:
		$image->flopImage();
		$image->rotateImage("#000", -90);
		break;
	case Imagick::ORIENTATION_RIGHTTOP:
		$image->rotateImage("#000", 90);
		break;
	case Imagick::ORIENTATION_RIGHTBOTTOM:
		$image->flopImage();
		$image->rotateImage("#000", 90);
		break;
	case Imagick::ORIENTATION_LEFTBOTTOM:
		$image->rotateImage("#000", -90);
		break;
	default: // Invalid orientation
		break;
	}
	$image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);
	return $image;
}
But that results in the exact same thing (black bars across the images). So then I thought, "alright, what if I manually rotate one by 90 degrees?", and I did the following:

Code: Select all

convert -rotate 90 'file' 'newFile'
But that also resulted in the same black bars. So now I am at a loss and either need some help or need to report it as a bug.

Here is an example image and the results:
Original image:
Image
After rotation:
Image

Thanks!
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

Oh, and this same thing has happened to hundreds of images that it has been tested on. The black bars always appear in the same place too, regardless of the image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image causes black bars and corruption

Post by fmw42 »

Looks like you are running out of memory or tmp space. Where is your TMPDIR? Mine is at /tmp. If the same check that directory for any really large files.

Code: Select all

cd /tmp
ls -al
What do you get from

Code: Select all

convert -list resource
For some reason I do not have a working copy of 6.9.4.3. Perhaps it had a bug. Have you tried upgrading to the latest 6.9.4.4.

Your command works fine for me on IM 6.9.4.4 Q16 Mac OSX.

Your delegates are a little few. You may want to install freetype, TIFF, Ghostscript. But I do not think those are the issue.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image causes black bars and corruption

Post by snibgo »

The commands are in the wrong order. They should be: read input, do any operations, write the output, eg:

Code: Select all

convert in.jpg -auto-orient out.jpg

convert in.jpg -rotate 90 out.jpg
However, I doubt that is the problem.

It works fine for me (v6.9.2-5 on Windows 8.1).

What is your output format? JPG? Does the problem occur with other output formats? Does the problem occur with built-in images, eg

Code: Select all

convert rose: -rotate 90 out.jpg
It may be a problem with out-of-memory, or a library.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image causes black bars and corruption

Post by fmw42 »

snibgo has a good idea. Try converting to PNG without the rotate. Does that work? Then try converting the PNG to JPG. Does that work? If both converts work, then put in the rotate again and see if those work.
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

Thank you for the suggestions. The files are jpg images and the output is jpg.

The results for

Code: Select all

convert -list resource
are:

Resource limits:
Width: 214.7MP
Height: 214.7MP
Area: 64.317GP
Memory: 29.95GiB
Map: 59.899GiB
Disk: unlimited
File: 18750
Thread: 24
Throttle: 0
Time: unlimited

I'm not sure what converting to png then back to jpg would prove with this, since I wouldn't generally want to do that to all of my images. But, I will try rotating a png and see if it has the same issue.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image causes black bars and corruption

Post by snibgo »

It's a question of pinning down where the problem lies. It could be reading the input, doing the processing, or writing the output.

If it happens for all input and output file types, then the problem is probably in the processing (eg a memory issue).

But the problem may be restricted to JPG formats, suggesting the coder (or library) is the problem.

Knowing when the problem doesn't occur helps narrow the diagnosis.
snibgo's IM pages: im.snibgo.com
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

snibgo wrote:It's a question of pinning down where the problem lies. It could be reading the input, doing the processing, or writing the output.

If it happens for all input and output file types, then the problem is probably in the processing (eg a memory issue).

But the problem may be restricted to JPG formats, suggesting the coder (or library) is the problem.

Knowing when the problem doesn't occur helps narrow the diagnosis.
Thanks; I'll give it a shot and get back to you all again!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image causes black bars and corruption

Post by fmw42 »

You have 24 processors and OpenMP enabled. Another thing you could try is to disable Openmp or try using one thread.

Code: Select all

convert -limit thread 1 ...
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

fmw42 wrote:snibgo has a good idea. Try converting to PNG without the rotate. Does that work? Then try converting the PNG to JPG. Does that work? If both converts work, then put in the rotate again and see if those work.
Alright, so the results were:
Converting to PNG without the rotate -> works, looks good without rotation
Then try converting the PNG to JPG -> works, looks good without rotation
If both converts work, then put in the rotate again and see if those work -> black bars when the jpg to png to jpg file is rotated, same placement of the black bars on the image.

:(
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

fmw42 wrote:You have 24 processors and OpenMP enabled. Another thing you could try is to disable Openmp or try using one thread.

Code: Select all

convert -limit thread 1 ...
Woah! I think you may have hit on something here. When I run it with

Code: Select all

convert -limit thread 1 ...
[/quote] there are no black bars! This has never been a problem before, and I am concerned that it will impact the speed in which I can process the files, but it's a start!

However, I have no idea where to go from here. Try more cores?
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

Woah! I think you may have hit on something here. When I run it with

Code: Select all

convert -limit thread 1 ...
there are no black bars! This has never been a problem before, and I am concerned that it will impact the speed in which I can process the files, but it's a start!

However, I have no idea where to go from here. Try more cores?
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

Alright, I tried with 1, 2, 3, and four threads (using convert -limit thread 1 ..., convert -limit thread 2 ..., etc.) and the only one without the black bars is the one with the single thread. ??? Any idea why would it not be able to handle more threads, like it has in the past?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotate image causes black bars and corruption

Post by fmw42 »

Unfortunately, I do not know. I had just heard that some Linux systems had this problem with OpenMP. Did you change your OS?
caliguian
Posts: 33
Joined: 2012-08-10T12:15:13-07:00
Authentication code: 67789

Re: Rotate image causes black bars and corruption

Post by caliguian »

fmw42 wrote:Unfortunately, I do not know. I had just heard that some Linux systems had this problem with OpenMP. Did you change your OS?
No, no change to the OS. I appreciate your help in getting this far with this though. Thank you!
Post Reply