3
$\begingroup$

I posted this on BA but I'm posting it here as well.

I'm looking into manipulating images with Pillow in custom node trees. If I install Pillow and copy the PIL directory from

C:\Python34\Lib\site-packages

to

\bin\2.73\python\lib\site-packages

then I can import PIL just fine. I can save .png image files, but saving to any other image format type crashes Blender.

But if I run Python 3.4 in a command prompt, I can import PIL (the one from my C:\Python34 installation), and read, modify and save to any of the supported image formats.

The problematic code:

import bpy
import os
from extensions_framework import util as efutil
from PIL import Image, ImageEnhance, ImageFile

def realpath( path):
    return os.path.realpath( efutil.filesystem_path( path))

brightness = 2.0
# Use an image loaded into Blender.
img = bpy.data.images['preview.jpg']
orig = Image.open( realpath( img.filepath))
ImageFile.MAXBLOCK = max( orig.size) ** 2

# Brighten the image.
enhancer = ImageEnhance.Brightness(orig)
bright = enhancer.enhance(brightness)

# Save the new image.
img_path = ( os.path.sep).join( realpath( img.filepath).split( os.path.sep)[:-1])
img_name = realpath( img.filepath).split( os.path.sep)[-1][:-4]  # Remove '.jpg' from end
new_img = os.path.join( (os.path.sep).join( realpath( img.filepath).split( os.path.sep)))

# The following line crashes Blender.
# But if the string is changed to "_mod.png", it works.
bright.save( os.path.join( img_path, img_name + "_mod.jpg"))

What could the problem be? Why can't I do this from within Blender without it crashing?

$\endgroup$
  • 1
    $\begingroup$ related: blender.stackexchange.com/questions/5287/… and make sure blender and pillow are using same python version. If this doesn't solve your issue, please add your code to the question, otherwise it is just guessing. $\endgroup$ – p2or Mar 17 '15 at 14:10
  • $\begingroup$ @poor This doesn't work, unfortunately. I've tried using the system Python 3.4 by removing the Blender installation's python directory, but the error persists. I'll update my post with the problematic code I'm using. $\endgroup$ – swingsoneto Mar 17 '15 at 18:06
  • $\begingroup$ @swingsoneto Any news on this? I am having the same problem. I need to use pillow in blender for creating and saving images (tga files). I can open and save pngs but it crashes when working with tga's. $\endgroup$ – Hamburml Jun 1 '15 at 18:12
  • $\begingroup$ @Michael no, not really. I'd resigned myself to only saving to png. It's better than jpg's, and certainly better than nothing. :) $\endgroup$ – swingsoneto Jun 2 '15 at 18:58
  • $\begingroup$ @swingsoneto There is a workaround. stackoverflow.com/a/21479241/3069477 Works for me :) (with format = "tga") $\endgroup$ – Hamburml Jun 3 '15 at 22:05
2
$\begingroup$

I am using Pillow successfully on MacOSX but maybe not in the cleanest way.

In a nutshell, I'm using Blender's own python34 and I add a path to the proper version of Pillow in my script

  1. Download Pillow for python34 and extract it in a folder – on MacOS, macport & pip automatically take care of that for me
  2. Take the path to Pillow within that folder
  3. In your python script before you import Pillow, add the code below

Python

import sys
path_to_pillow = 'C:/somewhere_on_your_disk/pillow/'
sys.path.append(path_to_pillow)
from PIL import Image

I'll try to run your code and see if it works on my side.

Could you copy the error you're getting when using PIL with Blender ?

$\endgroup$
1
$\begingroup$

I had the same problem. For me, the issue was that I had installed pillow with the package manager of my linux distribution, however I had downloaded blender from the website.

I assume that the problem is a different libpng version that pillow was using, and that blender was linked against.

To fix the issue, I downloaded blender from my linux distribution instead. Maybe you can try the same?

$\endgroup$
  • $\begingroup$ I also figured it was an issue of disparate libjpeg versions (and several other libs as well). But if that's the case then that's certainly a solution for personal use, but not for an addon intended to be distributed, since I won't be able to predict what version of Blender the user is going to be using and where they will have obtained it. $\endgroup$ – swingsoneto Jun 2 '15 at 18:54
1
$\begingroup$

If you use pycharm you can point at blenders python binary \2.79\python\bin\python.exe

After that In the pycharm terminal run this:

"pip install --target=C:\blender_location\2.79\python\lib Pillow"

enter image description here.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.